From 4461c9838fed6e30025f699c71222aee33cc22a7 Mon Sep 17 00:00:00 2001 From: Sindre Kjelsrud Date: Tue, 26 Sep 2023 10:13:32 +0200 Subject: [PATCH] :sparkles: DELETE-endpoint: 'Avada Kedavra' to movie! Co-authored-by: haraldnilsen --- backend/Controllers/MovieController.cs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/backend/Controllers/MovieController.cs b/backend/Controllers/MovieController.cs index 90c835a..b0cd81a 100644 --- a/backend/Controllers/MovieController.cs +++ b/backend/Controllers/MovieController.cs @@ -102,4 +102,26 @@ public class MovieController: ControllerBase return StatusCode(500, "Internal server error"); } } + + [HttpDelete(Name = "DeleteMovie")] + public ActionResult Delete([FromQuery] string imdbID) { + try + { + var movie = _context.Movies.AsEnumerable() + .Where(m => m.imdbID.ToLower().Contains(imdbID.ToLower())) + .ElementAtOrDefault(0); + if (movie != null) { + _context.Remove(movie); + _context.SaveChanges(); + return Ok("Successfully deleted movie"); + } else { + return StatusCode(409, "Conflict, imdbID doesn't exists"); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Error deleting movie"); + return StatusCode(500, "Internal server error"); + } + } } \ No newline at end of file