public int AddOrder(Models.User user, List <Models.Movie> movies) { MovieManager movieManager = new MovieManager(); Data.User userFind = db.Users.FirstOrDefault(x => x.Id == user.Id); Data.Order newOrder = new Data.Order(); newOrder.OrderDate = DateTime.UtcNow; newOrder.PaymentReceipt = true; newOrder.ShipDate = DateTime.UtcNow; newOrder.User = userFind; db.Orders.Add(newOrder); db.SaveChanges(); int orderId = newOrder.Id; foreach (var item in movies) { Data.Movie movie = db.Movies.SingleOrDefault(d => d.Id == item.Id); Data.OrderItem newOrderItem = new Data.OrderItem(); newOrderItem.Order = newOrder; newOrderItem.Movie = movie; newOrderItem.Quantity = 1; db.OrderItems.Add(newOrderItem); } db.SaveChanges(); return(orderId); }
public int Add(Models.Movie movieModel) { if (movieModel.Format == null) { throw new ArgumentNullException("Format", "The Format property must not be null."); } if (movieModel.Director == null) { throw new ArgumentNullException("Director", "The Director property must not be null."); } Data.Movie newRow = new Data.Movie(); newRow.Cost = movieModel.Cost; newRow.Description = movieModel.Description; newRow.Title = movieModel.Title; newRow.ImagePath = movieModel.ImagePath; Data.Rating existingRatings = db.Ratings.SingleOrDefault(x => x.Id == movieModel.Rating.Id); newRow.Rating = existingRatings ?? throw new ArgumentException("The associated rating cannot be found."); Data.Director existingDirectors = db.Directors.SingleOrDefault(x => x.Id == movieModel.Director.Id); newRow.Director = existingDirectors ?? throw new ArgumentException("The associated Director cannot be found."); Data.Format existingFormats = db.Formats.SingleOrDefault(x => x.Id == movieModel.Format.Id); newRow.Format = existingFormats ?? throw new ArgumentException("The associated format cannot be found."); db.Movies.Add(newRow); db.SaveChanges(); return(newRow.Id); }