Пример #1
0
        public ActionResult AddPortionFood([Bind(Include = "IdFood, Amount")] PortionFood portion)
        {
            int idDish = int.Parse(RouteData.Values["Id"].ToString());

            if (ModelState.IsValid)
            {
                portion.IdDish = idDish;

                ValidationResult result = PortionFoodService.Add(portion);
                if (!result.IsSuccess)
                {
                    var errorDetails = result.GetAllErrors();
                    throw new HttpException(404, "Model you add is incorrect. " + errorDetails);
                }
            }

            List <int> usedInDishFoodIds = DishService.Get(idDish).PortionFood.
                                           Where(p => !p.IsDeleted).Select(pf => pf.Food.Id).ToList();

            ViewBag.IdFood = new SelectList(
                FoodService.Get().Where(x => !usedInDishFoodIds.Contains(x.Id)),
                "Id", "Name");
            ViewBag.IdDish = idDish;

            return(View());
        }
Пример #2
0
        public ValidationResult Update(int foodId, int dishId, PortionFood item)
        {
            try
            {
                ValidationResult valResult = IsCorrectItem(item);
                if (valResult.IsSuccess == false)
                {
                    return(valResult);
                }
                PortionFood portion = context.PortionFood.Find(foodId, dishId);
                if (portion == null)
                {
                    return(new ValidationResult(false, "PortionFood was not found"));
                }
                portion.Amount           = item.Amount;
                portion.IdDish           = item.IdDish;
                portion.IdFood           = item.IdFood;
                portion.DateModification = DateTime.Now;
                context.SaveChanges();

                return(new ValidationResult(true));
            }
            catch (DbEntityValidationException ex)
            {
                //logging
                throw new BHPException("Some data was damaged, and changes was not saved", ex);
            }
            catch (Exception ex)
            {
                //logging
                throw new BHPException("Internal server error", ex);
            }
        }
Пример #3
0
        public ActionResult DetailsPortionFood(int?foodId, int?dishId)
        {
            if (foodId == null || dishId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PortionFood portionFood = PortionFoodService.Get(foodId.Value, dishId.Value);

            if (portionFood == null)
            {
                return(HttpNotFound());
            }
            return(View(portionFood));
        }
Пример #4
0
        public ValidationResult IsCorrectItem(PortionFood item)
        {
            ValidationResult valResult = new ValidationResult(true);

            if (item.Food == null && FoodService.Get(item.IdFood) == null)
            {
                valResult.AddErrorMessage("IdFood is incorrect");
            }
            if (item.Dish == null && DishService.Get(item.IdDish) == null)
            {
                valResult.AddErrorMessage("IdDish is incorrect");
            }
            return(valResult);
        }
Пример #5
0
        public ActionResult AddPortionFood(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            List <int> usedInDishFoodIds = DishService.Get(id.Value).PortionFood.
                                           Where(p => !p.IsDeleted).Select(pf => pf.Food.Id).ToList();

            ViewBag.IdFood = new SelectList(
                FoodService.Get().Where(x => !usedInDishFoodIds.Contains(x.Id)),
                "Id", "Name");
            ViewBag.IdDish = id;

            PortionFood portion = new PortionFood();

            return(View(portion));
        }
Пример #6
0
        public ValidationResult Add(PortionFood item)
        {
            try
            {
                ValidationResult valResult = IsCorrectItem(item);
                if (valResult.IsSuccess == false)
                {
                    return(valResult);
                }

                item.DateModification = DateTime.Now;

                PortionFood existingPortionFood = context.PortionFood.Find(item.IdFood, item.IdDish);
                if (existingPortionFood != null && existingPortionFood.IsDeleted)
                {
                    existingPortionFood.Amount           = item.Amount;
                    existingPortionFood.DateModification = item.DateModification;
                    existingPortionFood.IsDeleted        = false;
                }
                else
                {
                    if (context.PortionFood.Add(item) == null)
                    {
                        return(new ValidationResult(false, "Item was not added"));
                    }
                }


                context.SaveChanges();
                return(new ValidationResult(true));
            }
            catch (DbEntityValidationException ex)
            {
                //logging
                throw new BHPException("Added value is incorrect, and changes was not saved", ex);
            }
            catch (Exception ex)
            {
                //logging
                throw new BHPException("Internal server error", ex);
            }
        }
Пример #7
0
        public ActionResult EditPortionFood([Bind(Include = "IdFood,IdDish,Amount")] PortionFood portionFood)
        {
            if (ModelState.IsValid)
            {
                PortionFoodService.Update(portionFood.IdFood, portionFood.IdDish, portionFood);
                return(RedirectToAction("Edit", new RouteValueDictionary {
                    { "id", portionFood.IdDish }
                }));
            }

            List <int> usedInDishFoodIds = DishService.Get(portionFood.IdDish).PortionFood.
                                           Where(p => !p.IsDeleted).Select(pf => pf.Food.Id).ToList();

            ViewBag.IdFood = new SelectList(
                FoodService.Get().Where(x => !usedInDishFoodIds.Contains(x.Id)),
                "Id", "Name");
            ViewBag.IdDish = portionFood.IdDish;

            return(View(portionFood));
        }
Пример #8
0
        public ActionResult EditPortionFood(int?foodId, int?dishId)
        {
            if (foodId == null || dishId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            PortionFood portionFood = PortionFoodService.Get(foodId.Value, dishId.Value);

            if (portionFood == null)
            {
                return(HttpNotFound());
            }

            List <int> usedInDishFoodIds = DishService.Get(dishId.Value).PortionFood.
                                           Where(p => !p.IsDeleted).Select(pf => pf.Food.Id).ToList();

            ViewBag.IdFood = new SelectList(
                FoodService.Get().Where(f => !usedInDishFoodIds.Contains(f.Id) || f.Id == foodId.Value),
                "Id", "Name");
            ViewBag.IdDish = dishId.Value;
            return(View(portionFood));
        }
Пример #9
0
 public ValidationResult Remove(PortionFood item)
 {
     try
     {
         if (item == null || item.IsDeleted)
         {
             return(new ValidationResult(false, "PortionFood was not found"));
         }
         item.IsDeleted        = true;
         item.DateModification = DateTime.Now;
         context.SaveChanges();
         return(new ValidationResult(true));
     }
     catch (DbEntityValidationException ex)
     {
         //logging
         throw new BHPException("Some data was damaged, and changes was not saved", ex);
     }
     catch (Exception ex)
     {
         //logging
         throw new BHPException("Internal server error", ex);
     }
 }
Пример #10
0
        public ValidationResult Remove(int foodId, int dishId)
        {
            PortionFood portion = context.PortionFood.Find(foodId, dishId);

            return(Remove(portion));
        }
Пример #11
0
        protected override void Seed(ApplicationDbContext context)
        {
            var type1 = new FoodConsistencyType {
                Name = "Твердый"
            };
            var type2 = new FoodConsistencyType {
                Name = "Жидкий"
            };
            var foodConsistencyTypeService = new FoodConsistencyTypeService(context);

            foodConsistencyTypeService.Add(type1);
            foodConsistencyTypeService.Add(type2);

            var fakeFoodCategory = new FoodCategory
            {
                DateModification = DateTime.Now,
                Name             = "fake",
                Image            = new byte[] { 3, 3 }
            };
            var foodCategoryService = new FoodCategoryService(context);

            foodCategoryService.Add(fakeFoodCategory);

            var fakeDishCategory = new DishCategory
            {
                DateModification = DateTime.Now,
                Name             = "fake",
                Image            = new byte[] { 4 }
            };
            var dishCategoryService = new DishCategoryService(context);

            dishCategoryService.Add(fakeDishCategory);

            var fakeDish1 = new Dish
            {
                Name             = "fake1",
                DishCategory     = fakeDishCategory,
                TotalAmountWater = 1,
                TotalCarbs       = 1,
                TotalCcal        = 1,
                TotalFat         = 1,
                TotalProteins    = 1,
                TotalSugar       = 1
            };
            var fakeDish2 = new Dish
            {
                Name             = "fake2",
                DishCategory     = fakeDishCategory,
                TotalAmountWater = 2,
                TotalCarbs       = 2,
                TotalCcal        = 2,
                TotalFat         = 2,
                TotalProteins    = 2,
                TotalSugar       = 2,
                IsDeleted        = true
            };
            var fakeDish3 = new Dish
            {
                Name             = "fake3",
                DishCategory     = fakeDishCategory,
                TotalAmountWater = 3,
                TotalCarbs       = 3,
                TotalCcal        = 3,
                TotalFat         = 3,
                TotalProteins    = 3,
                TotalSugar       = 3
            };
            var fakeDish4 = new Dish
            {
                Name             = "fake4",
                DishCategory     = fakeDishCategory,
                TotalAmountWater = 4,
                TotalCarbs       = 4,
                TotalCcal        = 4,
                TotalFat         = 4,
                TotalProteins    = 4,
                TotalSugar       = 4
            };
            var fakeDish5 = new Dish
            {
                Name             = "fake5",
                DishCategory     = fakeDishCategory,
                TotalAmountWater = 5,
                TotalCarbs       = 5,
                TotalCcal        = 5,
                TotalFat         = 5,
                TotalProteins    = 5,
                TotalSugar       = 5
            };
            var dishService = new DishService(context);

            dishService.Add(fakeDish1);
            dishService.Add(fakeDish2);
            dishService.Add(fakeDish3);
            dishService.Add(fakeDish4);
            dishService.Add(fakeDish5);

            var fakeFood1 = new Food
            {
                Name                = "fake1",
                FoodCategory        = fakeFoodCategory,
                FoodConsistencyType = type1,
                Image               = new byte[] { 1 },
                AmountOfWater       = 1,
                Carbs               = 1,
                Ccal                = 1,
                Fat      = 1,
                Proteins = 1,
                Sugar    = 1
            };
            var fakeFood2 = new Food
            {
                Name                = "fake2",
                FoodCategory        = fakeFoodCategory,
                FoodConsistencyType = type1,
                Image               = new byte[] { 3 },
                AmountOfWater       = 2,
                Carbs               = 2,
                Ccal                = 2,
                Fat      = 2,
                Proteins = 2,
                Sugar    = 2
            };
            var foodService = new FoodService(context);

            foodService.Add(fakeFood1);
            foodService.Add(fakeFood2);

            var fakePortion1 = new PortionFood
            {
                Dish   = fakeDish1,
                Food   = fakeFood1,
                Amount = 3
            };
            var fakePortion2 = new PortionFood
            {
                Dish   = fakeDish1,
                Food   = fakeFood2,
                Amount = 5
            };
            var portionFoodService = new PortionFoodService(context);

            portionFoodService.Add(fakePortion1);
            portionFoodService.Add(fakePortion2);

            var fakeActivityType = new UserActivityType()
            {
                Description = "fake",
                Name        = "fake"
            };
            var fakeActivityTypeService = new UserActivityTypeService(context);

            fakeActivityTypeService.Add(fakeActivityType);

            RoleManager <IdentityRole> roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            ApplicationUserManager     userManager = new ApplicationUserManager(new UserStore <ApplicationUser>(context));

            roleManager.Create(new IdentityRole("Admin"));
            roleManager.Create(new IdentityRole("User"));
            string adminEmail = "*****@*****.**";

            if (userManager.Create(new ApplicationUser()
            {
                Email = adminEmail, UserName = "******"
            }, "bsuirhealthproject").Succeeded == true)
            {
                ApplicationUser user = userManager.FindByEmail(adminEmail);
                userManager.AddToRole(user.Id, "Admin");
                userManager.AddToRole(user.Id, "User");
                var userService = new UserService(context);
                userService.Add(new User
                {
                    DateOfBirth      = DateTime.Now,
                    FirstName        = "fake",
                    IdActivityType   = fakeActivityType.Id,
                    IdUserCredential = user.Id,
                    LastName         = "fake",
                    Sex = false
                });
            }
        }