/// <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(Domain.Model.Review review, Domain.Model.Restaurant restaurant = null)
        {
            if (restaurant.Id != 0)
            {
                _logger.LogWarning("Review to be added has an ID ({reviewId}) already: ignoring.", review.Id);
            }

            _logger.LogInformation("Adding review to restaurant with ID {restaurantId}", 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);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Maps a restaurant business model to a DAO for Entity Framework,
 /// including all reviews if present.
 /// </summary>
 /// <param name="restaurant">The restaurant business model.</param>
 /// <returns>The restaurant DAO.</returns>
 public static Model.Restaurant MapRestaurantWithReviews(Domain.Model.Restaurant restaurant)
 {
     return(new Model.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(Domain.Model.Restaurant restaurant)
        {
            _logger.LogInformation("Updating restaurant with ID {restaurantId}", 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(Domain.Model.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.MapRestaurantWithReviews(restaurant);

            entity.Id = 0;
            _dbContext.Add(entity);
        }