Skip to content

Commit 4798bbc

Browse files
committed
Movie update endpoit implemented
1 parent 2fac2af commit 4798bbc

3 files changed

Lines changed: 46 additions & 5 deletions

File tree

StarWars.API/Controllers/MovieController.cs renamed to StarWars.API/Controllers/MoviesController.cs

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,23 @@
55
using StarWars.Interface;
66
using StarWars.Model;
77
using StarWars.Model.ViewModels;
8+
using System;
89
using System.Threading.Tasks;
910

1011
namespace 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)

StarWars.API/Services/MovieService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public async Task<MovieView> Create(Movie movie)
3939
return Mapper.Map<MovieView>(item);
4040
}
4141

42+
public async Task<MovieView> Update(string id, Movie movie)
43+
{
44+
var item = await repo.Update(id, movie);
45+
return Mapper.Map<MovieView>(item);
46+
}
47+
4248
public async Task<MovieView> Delete(string id)
4349
{
4450
var item = await repo.Delete(id);

StarWars.Interface/IMovieService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IMovieService
99
Task<MovieView[]> All();
1010
Task<MovieView> Get(string id);
1111
Task<MovieView> Create(Movie movie);
12+
Task<MovieView> Update(string id, Movie movie);
1213
Task<MovieView> Delete(string id);
1314
}
1415
}

0 commit comments

Comments
 (0)