Esempio n. 1
0
        public ActionResult AddFood(Food food, HttpPostedFileBase ImageFile)
        {
            if (ImageFile != null && ImageFile.ContentLength != 0)
            {
                var path     = Server.MapPath("/Content/Uploads/");
                var filename = ImageFile.FileName;
                ImageFile.SaveAs(path + filename);
                food.ImageURL = filename;

                ViewBag.UploadedImage = ImageFile.FileName;
            }

            if (ModelState.IsValid) //checks if the model is valid
            {
                string userId = User.Identity.GetUserId();
                food.ApplicationUserId = userId;

                foodService.Create(food); //Add


                return(RedirectToAction("Index", "Food")); //Go to Home
            }

            //We stil need DropDownList so we keep this here as the
            //List<Restaurant> restaurant = _uw.restRep.GetAll();
            string strUserId = User.Identity.GetUserId();
            IEnumerable <Restaurant> restaurant = restaurantService.GetAll().Where(x => x.ApplicationUserId == strUserId).ToList();
            var restaurantList = restaurant.Select(x => new SelectListItem()
            {
                Text  = x.RestaurantName,
                Value = x.Id.ToString()
            });

            ViewBag.RestaurantList = restaurantList;

            ViewBag.foodTypes = new SelectList(Enum.GetValues(typeof(FoodType))
                                               .OfType <Enum>()
                                               .Select(x => new SelectListItem
            {
                Text  = Enum.GetName(typeof(FoodType), x),
                Value = (Convert.ToInt32(x)).ToString()
            }), "Value", "Text");

            return(View(food));
        }
 public async Task <ActionResult <Food> > Add([FromBody] Food model)
 {
     try
     {
         return(await _foodService.Create(model));
     }
     catch (LaprTrackrException ex)
     {
         _logger.LogDebug(ex.Message);
         return(ex.GetActionResult());
     }
     catch (Exception ex)
     {
         const string message = "Failed to add food";
         _logger.LogError(ex, message);
         return(new LaprTrackrException(LaprTrackrStatusCodes.ServiceUnavailable, message).GetActionResult());
     }
 }
Esempio n. 3
0
        public async Task <IActionResult> OnPost()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            await _foodService.Create(new Application.Foods.Models.FoodAddInfo
            {
                Price       = PriceAmount,
                Description = Description,
                FoodType    = FoodType,
                Name        = Name
            });



            TempData["message"] = "New food created";
            return(RedirectToPage("./index"));
        }
Esempio n. 4
0
        public async Task <ActionResult <Food_FoodDTO> > Create([FromBody] Food_FoodDTO Food_FoodDTO)
        {
            if (!ModelState.IsValid)
            {
                throw new BindException(ModelState);
            }

            Food Food = ConvertDTOToEntity(Food_FoodDTO);

            Food = await FoodService.Create(Food);

            Food_FoodDTO = new Food_FoodDTO(Food);
            if (Food.IsValidated)
            {
                return(Food_FoodDTO);
            }
            else
            {
                return(BadRequest(Food_FoodDTO));
            }
        }
Esempio n. 5
0
        public async Task <IActionResult> Create(IFormFile photo, [Bind("Id,RestaurantId,CategoryId,Name,Description,Price,Images,Status,CreatedAt,UpdatedAt")] Food food)
        {
            if (photo != null)
            {
                var fileName = System.Guid.NewGuid().ToString().Replace("-", "");
                var ext      = photo.ContentType.Split(new char[] { '/' })[1];
                var path     = Path.Combine(webHostEnvironment.WebRootPath, "Foods", fileName + "." + ext);
                await using (var file = new FileStream(path, FileMode.Create))
                {
                    await photo.CopyToAsync(file);
                }
                food.Images = fileName + "." + ext;
            }
            if (!ModelState.IsValid)
            {
                return(View("create", food));
            }
            food.CreatedAt = DateTime.Now;
            food.UpdatedAt = DateTime.Now;
            food.Status    = 1;
            await iFoodService.Create(food);

            return(RedirectToAction(nameof(Index)));
        }
        public async Task <ActionResult <FoodItem> > Create([FromBody] FoodItem model)
        {
            await _service.Create(model);

            return(CreatedAtAction(nameof(GetAll), new { id = model.ID }, model));
        }
 public IActionResult Create(Food food)
 {
     return(Ok(service.Create(food)));
 }