コード例 #1
0
        public IActionResult UpdateRestaurant(int id, [FromBody] RestaurantForUpdateDto restaurant)
        {
            try
            {
                if (restaurant == null)
                {
                    _logger.LogError("restaurant object sent from client is null.");
                    return(BadRequest("restaurant object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid restaurant object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var restaurantEntity = _repository.Restaurant.GetRestaurantById(id);
                if (restaurantEntity == null)
                {
                    _logger.LogError($"restaurant with id: {id}, hasn't been found in db.");
                    return(NotFound());
                }

                _mapper.Map(restaurant, restaurantEntity);

                _repository.Restaurant.UpdateRestaurant(restaurantEntity);
                _repository.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateRestaurant action: {ex.InnerException.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
コード例 #2
0
        public IActionResult UpdatedRestaurant(int cityId, int id,
                                               [FromBody] RestaurantForUpdateDto restaurant)
        {
            if (restaurant.Description == restaurant.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var restaurantFromStore = city.Restaurants.FirstOrDefault(r => r.Id == id);

            if (restaurantFromStore == null)
            {
                return(NotFound());
            }

            restaurantFromStore.Name        = restaurant.Name;
            restaurantFromStore.Description = restaurant.Description;

            return(NoContent());//most common for update
        }
コード例 #3
0
        public async Task <IActionResult> UpdateRestaurant(int id, [FromBody] RestaurantForUpdateDto restaurant)
        {
            try
            {
                if (restaurant == null)
                {
                    _logger.LogError("Restaurant object sent from client is null.");
                    return(BadRequest("Restaurant object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid restaurant object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var restaurantEntity = await _repository.Restaurant.GetRestaurantById(id);

                if (restaurantEntity == null)
                {
                    _logger.LogError($"Restaurant with id: {id}, has not been found in the db.");
                    return(NotFound());
                }

                _mapper.Map(restaurant, restaurantEntity);
                _repository.Restaurant.UpdateRestaurant(restaurantEntity);
                await _repository.Save();

                return(NoContent());
            }
            catch
            {
                throw new Exception("Exception while updating restaurant from db");
            }
        }
コード例 #4
0
        public async Task <IActionResult> UpdateRestaurant(Guid restaurantId,
                                                           RestaurantForUpdateDto restaurant)
        {
            var restaurantFromRepo = await _knockRepository.GetRestaurantAsync(restaurantId);

            if (restaurantFromRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(restaurant, restaurantFromRepo);

            // save changes
            await _knockRepository.SaveChangesAsync();

            return(NoContent());
        }
コード例 #5
0
        public IActionResult PartiallyUpdateRestaurant(int cityId, int id,
                                                       [FromBody] JsonPatchDocument <RestaurantForUpdateDto> patchDoc)
        {
            var city = CitiesDataStore.Current.Cities.FirstOrDefault(c => c.Id == cityId);

            if (city == null)
            {
                return(NotFound());
            }

            var restaurantFromStore = city.Restaurants.FirstOrDefault(r => r.Id == id);

            if (restaurantFromStore == null)
            {
                return(NotFound());
            }

            var restaurantToPatch = new RestaurantForUpdateDto
            {
                Name        = restaurantFromStore.Name,
                Description = restaurantFromStore.Description
            };

            patchDoc.ApplyTo(restaurantToPatch, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (restaurantToPatch.Description == restaurantToPatch.Name)
            {
                ModelState.AddModelError("Description", "The provided description should be different from the name");
            }

            if (!TryValidateModel(restaurantToPatch))
            {
                return(BadRequest(ModelState));
            }

            restaurantFromStore.Name        = restaurantToPatch.Name;
            restaurantFromStore.Description = restaurantToPatch.Description;

            return(NoContent());//most common for update
        }
コード例 #6
0
        public async Task <IActionResult> PutRestaurant(int id, RestaurantForUpdateDto restaurant)
        {
            var restaurantFromRepo = await _restaurantsRepository.GetAsync(id);

            if (restaurantFromRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(restaurant, restaurantFromRepo);

            _restaurantsRepository.Update(restaurantFromRepo);

            if (await _restaurantsRepository.SaveChangesAsync())
            {
                return(NoContent());
            }

            return(BadRequest());
        }
コード例 #7
0
        public async Task <IActionResult> CreateRestaurant(int hotelId, RestaurantForUpdateDto restaurantForUpdateDto)
        {
            if (restaurantForUpdateDto == null)
            {
                return(BadRequest());
            }

            var restaurantEntity = _mapper.Map <Restaurant>(restaurantForUpdateDto);

            restaurantEntity.HotelId = hotelId;

            _repo.Add(restaurantEntity);

            if (await _unitOfWork.CompleteAsync())
            {
                var restaurantToReturn = _mapper.Map <RestaurantForUpdateDto>(restaurantEntity);
                return(CreatedAtRoute("GetRestaurant", new { id = restaurantEntity.Id }, restaurantToReturn));
            }

            throw new Exception("Creating the restaurant failed on save");
        }
コード例 #8
0
        public async Task <IActionResult> Update(int id, RestaurantForUpdateDto restaurantForUpdateDto)
        {
            if (restaurantForUpdateDto == null)
            {
                return(BadRequest());
            }

            var restaurantFromRepo = await _repo.GetRestaurant(id);

            if (restaurantFromRepo == null)
            {
                return(NotFound($"Could not find restaurant with an ID of {id}"));
            }

            _mapper.Map(restaurantForUpdateDto, restaurantFromRepo);

            if (await _unitOfWork.CompleteAsync())
            {
                return(NoContent());
            }

            throw new Exception($"Updating restaurant {id} failed on save");
        }