public HttpResponseMessage Post([FromBody] MovieDTO movieDTO) { try { if (ModelState.IsValid) { int currMaxId = context.Movies.Max(m => m.Id) + 1; movieDTO.Id = currMaxId; var newMovie = new Movie { Id = movieDTO.Id, Title = movieDTO.Title, ReleaseDate = movieDTO.ReleaseDate, Year = movieDTO.Year, Runtime = movieDTO.Runtime, AudienceRating = movieDTO.AudienceRating, MPAARating = movieDTO.MPAARating, PosterImg = movieDTO.PosterImg, Thumbnail = movieDTO.Thumbnail, Synopsis = movieDTO.Synopsis }; context.Movies.Add(newMovie); context.SaveChanges(); var movieChars = movieDTO.MovieCharacters; if(movieChars != null) { foreach(var mc in movieChars) { Actor newActor; ActorMovie newActorMovie; if ( (context.Actors.FirstOrDefault(a => a.FirstName == mc.FirstName) == null || context.Actors.FirstOrDefault(a => a.LastName != mc.LastName) == null)) //non existing Actor, add new actor, then set movie the actor plays role { newActor = new Actor { FirstName = mc.FirstName, LastName = mc.LastName }; context.Actors.Add(newActor); context.SaveChanges(); newActorMovie = new ActorMovie { CharacterInMovie = mc.CharacterInMovie, ActorID = newActor.Id, MovieID = newMovie.Id }; context.ActorMovies.Add(newActorMovie); context.SaveChanges(); } else //an existing actor, just set roles the actor plays in new movie { var f = context.Actors.FirstOrDefault(a => a.FirstName == mc.FirstName); var l = context.Actors.FirstOrDefault(a => a.LastName != mc.LastName); newActor = context.Actors.FirstOrDefault(a => a.FirstName == mc.FirstName && a.LastName == mc.LastName); newActorMovie = new ActorMovie { CharacterInMovie = mc.CharacterInMovie, ActorID = newActor.Id, MovieID = newMovie.Id }; context.ActorMovies.Add(newActorMovie); context.SaveChanges(); } } } HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.Created, newMovie); result.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = newMovie.Id })); return result; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } catch(Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } }
public void Setup() { connection = Effort.DbConnectionFactory.CreateTransient(); using (var context = new MovieTheaterRatingContext(connection)) { actorMovie1 = new ActorMovie { ID = 1, ActorID = 1, CharacterInMovie = "Actor 1: First character", MovieID = 1 }; actorMovie2 = new ActorMovie { ID = 2, ActorID = 1, CharacterInMovie = "Actor 1: Second character", MovieID = 1 }; actorMovie3 = new ActorMovie { ID = 3, ActorID = 2, CharacterInMovie = "Actor 2: First character", MovieID = 1 }; context.ActorMovies.Add(actorMovie1); context.ActorMovies.Add(actorMovie2); List<ActorMovie> moviePlayedByActor1 = new List<ActorMovie>(); moviePlayedByActor1.Add(actorMovie1); moviePlayedByActor1.Add(actorMovie2); List<ActorMovie> moviePlayedByActor2 = new List<ActorMovie>(); moviePlayedByActor2.Add(actorMovie3); actor1 = new Actor { Id = 1, FirstName = "First1", LastName = "Last1", ActorMovies = moviePlayedByActor1 }; actor2 = new Actor { Id = 2, FirstName ="First2", LastName = "Last2", ActorMovies = moviePlayedByActor2 }; context.Actors.Add(actor1); context.Actors.Add(actor2); List<ActorMovie> cast = new List<ActorMovie>(); cast.Add(actorMovie1); cast.Add(actorMovie2); cast.Add(actorMovie3); movie = new Movie { Id = 1, Title = "Movie 1", ActorMovies = cast }; context.Movies.Add(movie); context.SaveChanges(); } _context = new MovieTheaterRatingContext(connection); _context.Database.Initialize(true); }