Exemplo n.º 1
0
        public ActionResult <Restaurant> UpdateRestaurant([FromBody] Restaurant restaurant, long id)
        {
            var updatedRestaurant = _restaurantFacade.UpdateRestaurant(restaurant, id);

            if (updatedRestaurant == null)
            {
                return(NotFound());
            }
            return(updatedRestaurant);
        }
        public IHttpActionResult UpdateRestaurant()
        {
            var restaurant    = new JavaScriptSerializer().Deserialize <RestaurantModel>(HttpContext.Current.Request.Form.Get(0));
            var restaurantDto = Mapper.Map <RestaurantDTO>(restaurant);

            if (restaurant.IsLogoChange)
            {
                if (!HttpContext.Current.Request.Files.AllKeys.Any())
                {
                    throw new ValidationException(ErrorCodes.EmptyRestaurantLogo);
                }
                var httpPostedFile = HttpContext.Current.Request.Files[0];


                if (httpPostedFile == null)
                {
                    throw new ValidationException(ErrorCodes.EmptyRestaurantLogo);
                }

                if (httpPostedFile.ContentLength > 2 * 1024 * 1000)
                {
                    throw new ValidationException(ErrorCodes.ImageExceedSize);
                }


                if (Path.GetExtension(httpPostedFile.FileName).ToLower() != ".jpg" &&
                    Path.GetExtension(httpPostedFile.FileName).ToLower() != ".png" &&
                    Path.GetExtension(httpPostedFile.FileName).ToLower() != ".jpeg")
                {
                    throw new ValidationException(ErrorCodes.InvalidImageType);
                }
                restaurantDto.Image = new MemoryStream();
                httpPostedFile.InputStream.CopyTo(restaurantDto.Image);
            }

            _restaurantFacade.UpdateRestaurant(restaurantDto, HostingEnvironment.MapPath("~/Images/"), UserId);
            return(Ok());
        }