/// <summary> /// Add a review and associate it with a restaurant. /// </summary> /// <param name="review">The review</param> /// <param name="restaurant">The restaurant</param> public void AddReview(Library.Models.Review review, Library.Models.Restaurant restaurant = null) { if (restaurant.Id != 0) { s_logger.Warn($"Review to be added has an ID ({review.Id}) already: ignoring."); } s_logger.Info($"Adding review to restaurant with ID {restaurant.Id}"); if (restaurant != null) { // get the db's version of that restaurant // (can't use Find with Include) Restaurant restaurantEntity = _dbContext.Restaurant .Include(r => r.Review).First(r => r.Id == restaurant.Id); Review newEntity = Mapper.Map(review); restaurantEntity.Review.Add(newEntity); // also, modify the parameters restaurant.Reviews.Add(review); } else { Review newEntity = Mapper.Map(review); _dbContext.Add(newEntity); } }
/// <summary> /// Maps a restaurant business model to an entity for Entity Framework, /// including all reviews if present. /// </summary> /// <param name="restaurant">The restaurant business model.</param> /// <returns>The restaurant entity.</returns> public static Entities.Restaurant MapRestaurantWithReviews(Library.Models.Restaurant restaurant) { return(new Entities.Restaurant { Id = restaurant.Id, Name = restaurant.Name, Review = restaurant.Reviews.Select(Map).ToList() }); }
/// <summary> /// Update a restaurant as well as its reviews. /// </summary> /// <param name="restaurant">The restaurant with changed values</param> public void UpdateRestaurant(Library.Models.Restaurant restaurant) { s_logger.Info($"Updating restaurant with ID {restaurant.Id}"); // calling Update would mark every property as Modified. // this way will only mark the changed properties as Modified. Restaurant currentEntity = _dbContext.Restaurant.Find(restaurant.Id); Restaurant newEntity = Mapper.MapRestaurantWithReviews(restaurant); _dbContext.Entry(currentEntity).CurrentValues.SetValues(newEntity); }
/// <summary> /// Add a restaurant, including any associated reviews. /// </summary> /// <param name="restaurant">The restaurant</param> public void AddRestaurant(Library.Models.Restaurant restaurant) { if (restaurant.Id != 0) { s_logger.Warn($"Restaurant to be added has an ID ({restaurant.Id}) already: ignoring."); } s_logger.Info($"Adding restaurant"); Restaurant entity = Mapper.MapRestaurantWithReviews(restaurant); entity.Id = 0; _dbContext.Add(entity); }
/// <summary> /// Add a restaurant, including any associated reviews. /// </summary> /// <param name="restaurant">The restaurant</param> public void AddRestaurant(Library.Models.Restaurant restaurant) { if (restaurant.Id != 0) { _logger.LogWarning("Restaurant to be added has an ID ({restaurantId}) already: ignoring.", restaurant.Id); } _logger.LogInformation($"Adding restaurant"); Restaurant entity = Mapper.Map(restaurant); entity.Id = 0; _dbContext.Add(entity); }
// GET: Ingredient/Details/5 public ActionResult Details(int id) { restID = id; IEnumerable <RestIng> libRestIng = Repo.GetAllIngredientsDB(restID).ToList(); Library.Models.Restaurant libRest = Repo.GetRestaurantById(restID); IEnumerable <Models.Ingredient> webRests = libRestIng.Select(x => new Models.Ingredient { Id = x.ingredientId, name = Repo.GetIngredientById(x.ingredientId).Name, QtyToStock = x.resIngQty, Cost = Repo.GetIngredientById(x.ingredientId).Cost, Restaurant = libRest.Location }); return(View(webRests)); }
/// <summary> /// Add a restaurant, including any associated reviews. /// </summary> /// <param name="restaurant">The restaurant</param> public void AddRestaurant(Library.Models.Restaurant restaurant) { if (restaurant.Id != 0) { s_logger.Warn($"Restaurant to be added has an ID ({restaurant.Id}) already: ignoring."); } s_logger.Info($"Adding restaurant"); // ID left at default 0 var entity = new Restaurant { Name = restaurant.Name, Review = restaurant.Reviews.Select(r => new Review { Id = r.Id, ReviewerName = r.ReviewerName, Score = r.Score ?? throw new ArgumentException("review score cannot be null.", nameof(restaurant)), Text = r.Text }).ToList()
public static Restaurant Map(Library.Models.Restaurant restaurant) => new Restaurant { RestaurantId = restaurant.Id, RestLocation = restaurant.Location, RestPhone = restaurant.Phone };
public static Entities.Restaurant Map(Library.Models.Restaurant restaurant) => new Entities.Restaurant { Id = restaurant.Id, Name = restaurant.Name, Review = restaurant.Reviews.Select(Map).ToList() };