From a134a5e37600969bba40ce230146e0090887ad9a Mon Sep 17 00:00:00 2001 From: Sindre Kjelsrud Date: Tue, 26 Sep 2023 09:47:41 +0200 Subject: [PATCH] :sparkles: POST-endpoint in the box Co-authored-by: haraldnilsen --- backend/Controllers/MovieController.cs | 37 ++++++++++++++++++-------- backend/Models/db/MovieDB.cs | 8 ++++++ 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/backend/Controllers/MovieController.cs b/backend/Controllers/MovieController.cs index c720c66..90c835a 100644 --- a/backend/Controllers/MovieController.cs +++ b/backend/Controllers/MovieController.cs @@ -76,15 +76,30 @@ public class MovieController: ControllerBase } } - // [HttpPost(Name = "PostMovie")] - // public ActionResult<> Post([FromQuery] string movieTitle) { - // try - // { - - // } - // catch (Exception ex) - // { - - // } - // } + [HttpPost(Name = "PostMovie")] + public ActionResult Post( + [FromQuery] string s, + [FromQuery] string y, + [FromQuery] string imdbID, + [FromQuery] string type, + [FromQuery] string poster + ) { + try + { + MovieDB newMovie = new(s, y, imdbID, type, poster); + var movies = _context.Movies.Where(m => m.imdbID.ToLower().Contains(imdbID.ToLower())); + if (movies.Count() == 0) { + _context.Add(newMovie); + _context.SaveChanges(); + return Ok("Successfully added new movie"); + } else { + return StatusCode(409, "Conflict, imdbID already exists"); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Error posting movie"); + return StatusCode(500, "Internal server error"); + } + } } \ No newline at end of file diff --git a/backend/Models/db/MovieDB.cs b/backend/Models/db/MovieDB.cs index 5a3bd95..f8df7af 100644 --- a/backend/Models/db/MovieDB.cs +++ b/backend/Models/db/MovieDB.cs @@ -18,4 +18,12 @@ public class MovieDB public string Type { get; set; } public string Poster { get; set; } + + public MovieDB(string Title, string Year, string imdbID, string Type, string Poster) { + this.Title = Title; + this.Year = Year; + this.imdbID = imdbID; + this.Type = Type; + this.Poster = Poster; + } } \ No newline at end of file