public HttpResponseMessage Put(MovieDTO movieDTO) { if (ModelState.IsValid) { var existingParent = context.Movies.Where(m => m.Id == movieDTO.Id).Include(m => m.ActorMovies).FirstOrDefault(); if (existingParent != null) { var movie = new Movie { Id = movieDTO.Id, Title = movieDTO.Title, Year = movieDTO.Year, Runtime = movieDTO.Runtime, ReleaseDate = movieDTO.ReleaseDate, AudienceRating = movieDTO.AudienceRating, MPAARating = movieDTO.MPAARating, PosterImg = movieDTO.PosterImg, Thumbnail = movieDTO.Thumbnail, Synopsis = movieDTO.Synopsis }; foreach(var mc in movieDTO.MovieCharacters) { int actorID = context.Actors.Where(a => a.FirstName == mc.FirstName && a.LastName == mc.LastName).FirstOrDefault().Id; //reuse ActorID var am = new ActorMovie { ActorID = actorID, MovieID = movie.Id, CharacterInMovie = mc.CharacterInMovie }; movie.ActorMovies.Add(am); } context.Entry(existingParent).State = EntityState.Modified; context.Entry(existingParent).CurrentValues.SetValues(movie); //existing movie with new ActorMovie collection foreach (var existingChild in existingParent.ActorMovies.ToList()) { if (!movie.ActorMovies.Any(am => am.ActorID == existingChild.ActorID && am.MovieID == existingChild.MovieID)) { Debug.WriteLine("deleting an item: actorid = " + existingChild.ActorID + " | movieid = " + existingChild.MovieID + " | chacater: " + existingChild.CharacterInMovie); context.ActorMovies.Remove(existingChild); } } foreach (var child in movie.ActorMovies) { var existingChild = existingParent.ActorMovies.Where(am => am.ActorID == child.ActorID && am.MovieID == child.MovieID) .FirstOrDefault(); if (existingChild != null) { Debug.WriteLine("updating an exsting item: " + child.ActorID + " | movieid = " + existingChild.MovieID + " | chacater: " + existingChild.CharacterInMovie); context.Entry(existingChild).State = EntityState.Modified; existingChild.MovieID = movie.Id; existingChild.CharacterInMovie = child.CharacterInMovie; } else { //get ActorID int newActorID = movie.ActorMovies.Where(am => am.MovieID == child.MovieID && am.CharacterInMovie == child.CharacterInMovie).FirstOrDefault().ActorID; child.ActorID = newActorID; var newChild = child; existingParent.ActorMovies.Add(newChild); } } } else { return Request.CreateResponse(HttpStatusCode.NoContent,movieDTO); } try { context.SaveChanges(); return Request.CreateResponse(HttpStatusCode.OK, "{success: 'true', verb:'PUT'}"); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.PreconditionFailed, ex); } catch(Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); } } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
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); }
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); } }
private void setupDataForParentChildCRUD() { updateActorMovie1 = new ActorMovie { ID = 1, CharacterInMovie = "update actor movie1", ActorID = 1, MovieID = 1 }; insertNewActorMovie4 = new ActorMovie { ID = 4, CharacterInMovie = "insert actor movie 4 for actor 1", ActorID = 1, MovieID = 1 }; List<ActorMovie> cast = new List<ActorMovie>(); cast.Add(updateActorMovie1); cast.Add(insertNewActorMovie4); updateMovie1 = new Movie { Id = 1, Title = "Update movie 1", ActorMovies = cast }; }