예제 #1
0
        private MovieInfo UpdateMovieInfo(MovieRequestDTO movie)
        {
            var movieInfo = dbContext.MovieInfoes.Where(m => m.MovieId == movie.MovieId).FirstOrDefault();

            if (movieInfo != null)
            {
                using (var transaction = dbContext.Database.BeginTransaction())
                {
                    //update movie details.
                    try
                    {
                        movieInfo.Name          = movie.MovieName;
                        movieInfo.Plot          = movie.Plot;
                        movieInfo.Poster        = movie.PosterImage;
                        movieInfo.ProducedBy    = movie.ProducedBy;
                        movieInfo.Status        = true;
                        movieInfo.YearOfRelease = movie.YearOfRelease;
                        movieInfo.ModifiedDate  = DateTime.Now;
                        dbContext.SaveChanges();

                        var mappedActors = dbContext.ActorInMovies.Where(m => m.MovieId == movie.MovieId).ToList();
                        if (mappedActors.IsNotNullOrEmpty())
                        {
                            dbContext.ActorInMovies.RemoveRange(mappedActors);
                            dbContext.SaveChanges();
                        }

                        if (movie.Actors.Length > 0)
                        {
                            var actorinMovie = movie.Actors.Distinct().Select(a => new ActorInMovie()
                            {
                                ActorId = a,
                                MovieId = movieInfo.MovieId
                            }).ToList();
                            dbContext.ActorInMovies.AddRange(actorinMovie);
                            dbContext.SaveChanges();
                        }

                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }

                return(movieInfo);
            }
            throw new Exception("No such movie found by your search to update the details.");
        }
        public ProducerInfo SaveProducerInfo(ProducerInfoRequestDTO request)
        {
            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    DateTime dateOfBirth = new DateTime(request.DOB.Year, request.DOB.Month, request.DOB.Day, 0, 0, 0);
                    if (dbContext.ProducerInfoes.Where(a => (a.Name == request.ProducerName && a.DOB == dateOfBirth && a.Sex == request.Sex)).FirstOrDefault() != null)
                    {
                        if (!request.NameConfirm)
                        {
                            return(null);
                        }
                    }

                    ProducerInfo producerInfo = new ProducerInfo()
                    {
                        Bio         = request.Bio,
                        CreatedDate = DateTime.Now,
                        DOB         = request.DOB,
                        Name        = request.ProducerName,
                        Sex         = request.Sex,
                        Status      = true
                    };

                    dbContext.ProducerInfoes.Add(producerInfo);
                    dbContext.SaveChanges();
                    transaction.Commit();
                    return(producerInfo);
                }
                catch (Exception)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }