55using StarWars . Interface ;
66using StarWars . Model ;
77using StarWars . Model . ViewModels ;
8+ using System ;
89using System . Threading . Tasks ;
910
1011namespace StarWars . Api . Controllers
1112{
1213 [ ApiController ]
1314 [ Route ( "api/[controller]" ) ]
14- public class MovieController : ControllerBase
15+ public class MoviesController : ControllerBase
1516 {
1617 private readonly IMovieService service ;
1718
18- public MovieController ( IMovieService service )
19+ public MoviesController ( IMovieService service )
1920 {
2021 this . service = service ;
2122 }
2223
23- [ HttpGet ( ) ]
24+ [ HttpGet ]
2425 [ ProducesResponseType ( StatusCodes . Status200OK ) ]
2526 public async Task < IActionResult > AllAsync ( )
2627 {
@@ -45,8 +46,29 @@ public async Task<IActionResult> AllProtectedAsync()
4546 [ ProducesResponseType ( StatusCodes . Status403Forbidden ) ]
4647 public async Task < IActionResult > AllSecuredAsync ( )
4748 {
48- var items = await service . All ( ) ;
49- return new OkObjectResult ( items ) ;
49+ var data = await Task . FromResult ( new {
50+ statusCode = 200 ,
51+ message = "This is a secured endpoint data which is only available to authenticated users" ,
52+ timestamp = DateTime . Now ,
53+ path = "/api/movie/all/secured" ,
54+ data = new [ ] {
55+ new {
56+ id = 1 ,
57+ title = "Inception" ,
58+ poster = "https://picsum.photos/id/102/640/480" ,
59+ year = 2010 ,
60+ price = 12.99
61+ } ,
62+ new {
63+ id = 2 ,
64+ title = "The Matrix" ,
65+ poster = "https://picsum.photos/id/104/640/480" ,
66+ year = 1999 ,
67+ price = 9.99
68+ }
69+ }
70+ } ) ;
71+ return new OkObjectResult ( data ) ;
5072 }
5173
5274 [ HttpGet ( "{id}" ) ]
@@ -69,6 +91,18 @@ public async Task<IActionResult> Create([FromBody] MovieView movieView)
6991 return new OkObjectResult ( item ) ;
7092 }
7193
94+ [ Authorize ]
95+ [ HttpPut ( "{id}" ) ]
96+ public async Task < IActionResult > Update ( [ FromRoute ] string id , [ FromBody ] MovieView movieView )
97+ {
98+ var existingItem = await service . Get ( id ) ;
99+ if ( existingItem == null ) return new NotFoundObjectResult ( id ) ;
100+ var movie = Mapper . Map < Movie > ( movieView ) ;
101+ var item = await service . Update ( id , movie ) ;
102+ if ( item == null ) return new BadRequestObjectResult ( $ "Movie with ID '{ id } ' could not be updated") ;
103+ return new OkObjectResult ( item ) ;
104+ }
105+
72106 [ Authorize ]
73107 [ HttpDelete ( "{id}" ) ]
74108 public async Task < IActionResult > Delete ( [ FromRoute ] string id )
0 commit comments