예제 #1
0
 public IHttpActionResult Add([FromBody] Restaurant restaurant)
 {
     try
     {
         _logger.Info($"Add method called on the Restaurant controller.");
         var existingRestaurant = _facade.GetExistingRestaurant(restaurant);
         if (existingRestaurant != null)
         {
             _logger.Info("No restaurant was found matching the request. ");
             return(Conflict());
         }
         _facade.AddRestaurant(restaurant);
         return(Ok("Restaurant created successfully in the database. "));
     }
     catch (DbUpdateException ex)
     {
         _logger.Error(ex);
         return(BadRequest());
     }
     catch (Exception ex)
     {
         _logger.Error(ex);
         return(InternalServerError());
     }
 }
        public IHttpActionResult AddRestaurant()
        {
            if (!HttpContext.Current.Request.Files.AllKeys.Any())
            {
                throw new ValidationException(ErrorCodes.EmptyRestaurantLogo);
            }
            var httpPostedFile = HttpContext.Current.Request.Files[0];

            var restaurant = new JavaScriptSerializer().Deserialize <RestaurantModel>(HttpContext.Current.Request.Form.Get(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);
            }

            var restaurantDto = Mapper.Map <RestaurantDTO>(restaurant);

            //restaurantDto.Image = (MemoryStream) restaurant.Image.InputStream;
            restaurantDto.Image = new MemoryStream();
            httpPostedFile.InputStream.CopyTo(restaurantDto.Image);

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