public IActionResult CreateRestaurant([FromBody] RestaurantForCreationDto 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 = _mapper.Map <Restaurant>(restaurant);

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

                var createdRestaurant = _mapper.Map <RestaurantDto>(restaurantEntity);

                return(CreatedAtRoute("RestaurantById", new { id = createdRestaurant.RestaurantId }, createdRestaurant));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateRestaurant action: {ex.InnerException.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
        public IActionResult CreationRestaurant(int cityId, [FromBody] RestaurantForCreationDto 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 maxRestaurantId = CitiesDataStore.Current.Cities.SelectMany(c => c.Restaurants).Max(r => r.Id);

            var finalRestaurant = new RestaurantDto
            {
                Id          = ++maxRestaurantId,
                Name        = restaurant.Name,
                Description = restaurant.Description
            };

            city.Restaurants.Add(finalRestaurant);

            return(CreatedAtRoute(
                       "GetRestaurantById",
                       new { cityId, id = finalRestaurant.Id },
                       finalRestaurant));
        }
        public async Task <IActionResult> CreateRestaurant([FromBody] RestaurantForCreationDto 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 = _mapper.Map <Restaurant>(restaurant);


                _repository.Restaurant.CreateRestaurant(restaurantEntity);
                await _repository.Save();

                var createdRestaurant = _mapper.Map <RestaurantDto>(restaurantEntity);

                return(CreatedAtRoute("RestaurantById", new { id = createdRestaurant.Id }, createdRestaurant));
            }
            catch
            {
                throw new Exception("Exception while creating restaurant");
            }
        }
Beispiel #4
0
        public IActionResult PartiallyUpdateRestaurant([FromRoute] int restaurantId,
                                                       [FromBody] RestaurantForCreationDto patchRestaurant)
        {
            if (patchRestaurant == null)
            {
                return(BadRequest());
            }

            var RestaurantEntity = _restaurantInfoRepository.GetRestaurant(restaurantId);

            if (RestaurantEntity == null)
            {
                return(StatusCode(500, "The restaurant you requested is not in the database"));
            }


            if (patchRestaurant.Name != null)
            {
                if (patchRestaurant.Name.Length > 50)
                {
                    return(StatusCode(500, "THe name is too long!"));
                }
                RestaurantEntity.Name = patchRestaurant.Name;
            }

            if (patchRestaurant.Adress != null)
            {
                RestaurantEntity.Adress = patchRestaurant.Adress;
            }



            if (patchRestaurant.foods != null)
            {
                RestaurantEntity.foods = patchRestaurant.foods;
            }

            if (patchRestaurant.CreatedAtDate != null)
            {
                RestaurantEntity.CreatedAtDate = patchRestaurant.CreatedAtDate;
            }

            RestaurantEntity.UpdatedAtDate = DateTime.Now;


            if (!_restaurantInfoRepository.Save())
            {
                return(StatusCode(500, "There was a problem handling your request"));
            }


            return(Ok(RestaurantEntity));
        }
        public ActionResult <RestaurantDto> CreateRestaurant(
            [FromBody] RestaurantForCreationDto restaurant)
        {
            var restaurantFromRepo = _mapper.Map <Restaurant>(restaurant);

            _restaurantLibraryRepository.AddRestaurant(restaurantFromRepo);
            _restaurantLibraryRepository.Save();

            var restaurantToReturn = _mapper.Map <RestaurantDto>(restaurantFromRepo);

            return(CreatedAtRoute("GetRestaurant",
                                  new { restaurantId = restaurantToReturn.Id }, restaurantToReturn));
        }
        public async Task <ActionResult <RestaurantDto> > PostRestaurant(RestaurantForCreationDto restaurant)
        {
            var newRestaurant = _mapper.Map <Restaurant>(restaurant);

            await _restaurantsRepository.AddAsync(newRestaurant);

            if (await _restaurantsRepository.SaveChangesAsync())
            {
                return(CreatedAtAction("GetRestaurant", new { id = newRestaurant.Id }, _mapper.Map <RestaurantDto>(newRestaurant)));
            }

            return(BadRequest());
        }
        public async Task <IActionResult> CreateRestaurant(RestaurantForCreationDto restaurant)
        {
            if (restaurant == null)
            {
                return(BadRequest());
            }

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

            _knockRepository.AddRestaurant(restaurantEntity);
            await _knockRepository.SaveChangesAsync();

            var mappedRestaurant = _mapper.Map <RestaurantDto>(restaurantEntity);

            return(CreatedAtAction(nameof(GetRestaurant),
                                   new { restaurantId = mappedRestaurant.Id }, restaurant));
        }
Beispiel #8
0
        public IActionResult UpdateRestaurant([FromRoute] int restaurantId, [FromBody] RestaurantForCreationDto restaurantInfo)
        {
            if (restaurantInfo == null)
            {
                return(BadRequest());
            }

            var restaurantEntity = _restaurantInfoRepository.GetRestaurant(restaurantId);

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

            if (restaurantInfo.Name != null)
            {
                restaurantEntity.Name = restaurantInfo.Name;
            }
            if (restaurantEntity.Name.Length > 50)
            {
                return(StatusCode(500, "The name is too long!"));
            }



            if (restaurantInfo.Adress != null)
            {
                restaurantEntity.Adress = restaurantInfo.Adress;
            }
            if (restaurantEntity.Adress == null || restaurantEntity.Adress.Length > 50)
            {
                return(StatusCode(500, "The adress is too long or is null"));
            }

            if (restaurantInfo.CreatedAtDate != null)
            {
                restaurantEntity.CreatedAtDate = restaurantInfo.CreatedAtDate;
            }

            restaurantEntity.UpdatedAtDate = DateTime.Now;

            return(Ok(restaurantEntity));
        }
Beispiel #9
0
        public IActionResult CreateRestaurant([FromBody] RestaurantForCreationDto restaurantInfo)
        {
            var        restaurantError = "Please look at the data and make sure it's not empty, incorrect, or has values that are the same!";
            Restaurant restaurant      = new Restaurant();

            if (restaurantInfo == null)
            {
                return(BadRequest(restaurantError));
            }


            restaurant.Name = restaurantInfo.Name;
            if (restaurantInfo.Name == null || restaurantInfo.Name.Length > 50)
            {
                return(StatusCode(500, "The name is either null or too long"));
            }


            restaurant.Adress = restaurantInfo.Adress;
            if (restaurantInfo.Adress == null || restaurantInfo.Adress.Length > 50)
            {
                return(StatusCode(500, "The adress is null or too long"));
            }



            restaurant.foods = restaurantInfo.foods;

            restaurant.CreatedAtDate = DateTime.Now;
            restaurant.UpdatedAtDate = DateTime.Now;



            _restaurantInfoRepository.AddRestaurant(restaurant);

            if (!_restaurantInfoRepository.Save())
            {
                return(StatusCode(500, "SOmething wEnT WrOnG wHilE HanDlIng yOuR ReQUesT."));
            }

            return(Ok(restaurant));
        }