public IActionResult CreateFood(CreateFoodViewModel model)
        {
            var food = new Matratt()
            {
                MatrattNamn = model.Food.MatrattNamn,
                MatrattTyp  = model.Food.MatrattTyp,
                Beskrivning = model.Food.Beskrivning,
                Pris        = model.Food.Pris
            };

            _context.Matratts.Add(food);
            _context.SaveChanges();


            foreach (var ingredient in model.IngredientsChecked)
            {
                var foodIngredient = new MatrattProdukt()
                {
                    MatrattId = food.MatrattId,
                    ProduktId = ingredient
                };

                _context.MatrattProdukts.Add(foodIngredient);
            }
            _context.SaveChanges();


            return(View("CreateFoodConfirmation"));
        }
Exemplo n.º 2
0
        // GET: User/Foods/Create
        public IActionResult Create()
        {
            var date = new Date();

            DayOfWeek monday  = DayOfWeek.Monday;
            DayOfWeek tuesday = DayOfWeek.Tuesday;
            DayOfWeek today   = DateTime.Today.DayOfWeek;

            if (date.GetWeekOfYear() % 2 == 0 && DateTime.Now.Hour >= 8 && monday <= today && today <= tuesday && DateTime.Now.Hour <= 15)
            {
                var model = new CreateFoodViewModel();

                var list = model.FoodSuggestionsList = new List <SelectListItem>();

                foreach (var item in _context.FoodSuggestions.Where(acs => !_context.Foods.Select(a => a.FoodName.ToLower()).Contains(acs.FoodSuggestionName.ToLower())).OrderBy(o => o.FoodSuggestionName))
                {
                    list.Add(new SelectListItem {
                        Value = item.FoodSuggestionName, Text = item.FoodSuggestionName
                    });
                }

                return(View(model));
            }
            else
            {
                return(RedirectToAction(nameof(Index)));
            }
        }
Exemplo n.º 3
0
        public static CreateFoodViewModel GetFoodDetails(Guid foodId)
        {
            var food = Get(foodId);

            if (food == null)
            {
                return(null);
            }

            var returnedModel = new CreateFoodViewModel()
            {
                Id            = food.Id,
                Calories      = food.Calories.Value,
                Carbohydrates = food.Carbohidrates.Value,
                Description   = food.Description,
                Fats          = food.Fats.Value,
                Fibre         = food.Fiber.Value,
                Grams         = food.Grams,
                Name          = food.Name,
                Photo         = food.PictureUrl,
                Proteins      = food.Proteins.Value,
                Sugar         = food.Sugar.Value
            };

            return(returnedModel);
        }
Exemplo n.º 4
0
        public Response Create(CreateFoodViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                return(null);
            }

            var file = Request.Files["Photo"];
            var url  = AzureHelper.Upload(file, "Photo", Guid.NewGuid());

            var createdFood = new Food()
            {
                Name          = model.Name,
                Carbohidrates = model.Carbohydrates,
                Description   = model.Description,
                Fats          = model.Fats,
                Fiber         = model.Fibre,
                Proteins      = model.Proteins,
                Sugar         = model.Sugar,
                Grams         = model.Grams,
                Calories      = model.Calories,
                PictureUrl    = url,
                Status        = (int)EntityStatus.ACTIVE
            };

            var foodCreationResponse = FoodCore.Create(createdFood);

            if (foodCreationResponse == null)
            {
                return(ResponseFactory.ErrorReponse);
            }

            return(ResponseFactory.SuccessResponse);
        }
Exemplo n.º 5
0
        public IActionResult AddFood()
        {
            CreateFoodViewModel inputFoods = new CreateFoodViewModel();
            List <Food>         allFoods   = _db.Foods.OrderBy(f => f).ToList();

            inputFoods.allFoods    = allFoods;
            inputFoods.uniqueFoods = allFoods.Select(f => f.Name).Distinct().OrderBy(f => f).ToList();
            return(View("FoodCreate", inputFoods));
        }
        public IActionResult CreateFood()
        {
            var model = new CreateFoodViewModel()
            {
                FoodTypes   = _context.MatrattTyps.ToList(),
                Ingredients = _context.Produkts.ToList()
            };

            return(View(model));
        }
Exemplo n.º 7
0
        public async Task <IActionResult> Create(CreateFoodViewModel model)
        {
            var list = model.FoodSuggestionsList = new List <SelectListItem>();

            if (ModelState.IsValid)
            {
                if (ActivityExists(model.FoodName))
                {
                    ModelState.AddModelError("", "Der eksister allerrede en ret med det navn");

                    foreach (var item in _context.FoodSuggestions.Where(acs => !_context.Foods.Select(a => a.FoodName.ToLower()).Contains(acs.FoodSuggestionName.ToLower())).OrderBy(o => o.FoodSuggestionName))
                    {
                        list.Add(new SelectListItem {
                            Value = item.FoodSuggestionName, Text = item.FoodSuggestionName
                        });
                    }

                    return(View(model));
                }

                if (model.FoodName.Length > 70)
                {
                    ModelState.AddModelError("", "Der kan max være 70 tegn");

                    foreach (var item in _context.FoodSuggestions.Where(acs => !_context.Foods.Select(a => a.FoodName.ToLower()).Contains(acs.FoodSuggestionName.ToLower())).OrderBy(o => o.FoodSuggestionName))
                    {
                        list.Add(new SelectListItem {
                            Value = item.FoodSuggestionName, Text = item.FoodSuggestionName
                        });
                    }

                    return(View(model));
                }

                var food = new Food
                {
                    FoodName     = model.FoodName,
                    CreationDate = DateTime.Now
                };

                _context.Add(food);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            foreach (var item in _context.FoodSuggestions.Where(acs => !_context.Foods.Select(a => a.FoodName.ToLower()).Contains(acs.FoodSuggestionName.ToLower())).OrderBy(o => o.FoodSuggestionName))
            {
                list.Add(new SelectListItem {
                    Value = item.FoodSuggestionName, Text = item.FoodSuggestionName
                });
            }

            return(View(model));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create(CreateFoodViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var food = _mapper.Map <Food>(model);

            _db.Foods.Add(food);
            await _db.SaveChangesAsync();

            return(CreatedAtAction(nameof(Get), new { id = food.Id }, food));
        }
Exemplo n.º 9
0
        public JsonResult Save(CreateFoodViewModel model)
        {
            var response = ResponseFactory.ErrorReponse;

            if (model.Id == Guid.Empty)
            {
                response = Create(model);
            }
            else
            {
                response = Update(model);
            }

            return(Json(response, JsonRequestBehavior.AllowGet));
        }
Exemplo n.º 10
0
        public IActionResult UpdateDetails(CreateFoodViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newFoodDetails = new Food();
                newFoodDetails.Name        = model.Name;
                newFoodDetails.Description = model.Description;
                newFoodDetails.Price       = model.Price;
                newFoodDetails.Id          = model.Id;

                var updateDetails = _foodData.UpdateFood(newFoodDetails);
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(View());
            }
        }
Exemplo n.º 11
0
        public async Task <IActionResult> Create(CreateFoodViewModel model)
        {
            if (ModelState.IsValid)
            {
                Food newFood = new Food()
                {
                    FoodId   = Guid.NewGuid().ToString(),
                    IsActive = model.IsActive,
                    Name     = model.FoodName,
                    Price    = model.Price
                };
                string           folderPath = Path.Combine(path1: _webEnv.WebRootPath, path2: "images", path3: "foods");
                string           fileName   = String.Empty;
                string           filePath   = String.Empty;
                List <FoodImage> images     = new List <FoodImage>();
                if (model.Images.Count > 0)
                {
                    foreach (IFormFile img in model.Images)
                    {
                        fileName = $"{Guid.NewGuid()}_{img.FileName}";
                        filePath = Path.Combine(path1: folderPath, path2: fileName);
                        using (FileStream fs = new FileStream(filePath, FileMode.Create))
                        {
                            await img.CopyToAsync(fs);

                            FoodImage image = new FoodImage()
                            {
                                ImagesId = Guid.NewGuid().ToString(),
                                FoodId   = newFood.FoodId,
                                FileName = fileName,
                            };
                            images.Add(image);
                        }
                    }
                    newFood.FoodImages = new List <FoodImage>(images);
                }
                if (await _foodServices.CreateNewFood(newFood))
                {
                    return(RedirectToAction(actionName: "Manage", controllerName: "Food"));
                }
                ModelState.AddModelError("", "Something was wrong, please try again later");
            }
            return(View(model));
        }
Exemplo n.º 12
0
        public Response Update(CreateFoodViewModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                return(null);
            }

            var foodItem = FoodCore.Get(model.Id);

            if (foodItem == null)
            {
                return(ResponseFactory.ErrorReponse);
            }

            if (!string.IsNullOrWhiteSpace(model.Photo) && model.Photo != "undefined")
            {
                var file = Request.Files["Photo"];
                var url  = AzureHelper.Upload(file, "Photo", Guid.NewGuid());
                foodItem.PictureUrl = url;
            }

            foodItem.Name          = model.Name;
            foodItem.Carbohidrates = model.Carbohydrates;
            foodItem.Description   = model.Description;
            foodItem.Fats          = model.Fats;
            foodItem.Fiber         = model.Fibre;
            foodItem.Proteins      = model.Proteins;
            foodItem.Sugar         = model.Sugar;
            foodItem.Grams         = model.Grams;
            foodItem.Calories      = model.Calories;
            foodItem.Status        = (int)EntityStatus.ACTIVE;


            var foodCreationResponse = FoodCore.Update(foodItem);

            if (foodCreationResponse == null)
            {
                return(ResponseFactory.ErrorReponse);
            }

            return(ResponseFactory.SuccessResponse);
        }
Exemplo n.º 13
0
        public IActionResult Create(CreateFoodViewModel model)
        {
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;
                if (model.Photo != null)
                {
                    string[] permittedExtensions = { ".jpg", ".png", ",jpeg" };
                    string   uploadsFolder       = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName;
                    var ext = Path.GetExtension(uniqueFileName).ToLowerInvariant();
                    if (string.IsNullOrEmpty(ext) || !permittedExtensions.Contains(ext))
                    {
                        return(View());
                    }
                    else
                    {
                        string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                        model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                    }
                }
                var newFood = new Food();
                newFood.Name        = model.Name;
                newFood.Description = model.Description;
                newFood.Price       = model.Price;
                newFood.Picture     = uniqueFileName;

                var result = _foodData.SaveFood(newFood);

                return(RedirectToAction("Index"));
            }
            else
            {
                return(View());
            }
        }
Exemplo n.º 14
0
        public async Task <IActionResult> Create(CreateFoodViewModel foodViewModel)
        {
            await Mediator.Send(foodViewModel.Command);

            return(RedirectToAction("Index", "Home"));
        }