private async Task PrepareModelToReturn(int categoryId, SupplementFormViewModel model)
        {
            model.BestBeforeDate = DateTime.UtcNow.AddDays(1);
            model.Subcategories  = await this.GetSubcategoriesSelectListItems(categoryId);

            model.Manufacturers = await this.GetManufacturersSelectListItems();
        }
        public async Task <IActionResult> Edit(int id)
        {
            bool isSuplementExistingById = await this.supplementService.IsSupplementExistingById(id, false);

            if (!isSuplementExistingById)
            {
                TempData.AddErrorMessage(string.Format(EntityNotFound, SupplementEntity));

                return(this.RedirectToSupplementsIndex(false));
            }

            SupplementServiceModel supplement = await this.managerSupplementService.GetEditModelAsync(id);

            SupplementFormViewModel model = new SupplementFormViewModel()
            {
                Name           = supplement.Name,
                Description    = supplement.Description,
                Quantity       = supplement.Quantity,
                Price          = supplement.Price,
                BestBeforeDate = supplement.BestBeforeDate,

                SubcategoryId  = supplement.SubcategoryId,
                ManufacturerId = supplement.ManufacturerId,
            };

            int categoryId = await this.subcategoryService.GetCategoryIdBySubcategoryId(supplement.SubcategoryId);

            await PrepareModelToReturn(categoryId, model);

            ViewData["CategoryId"] = categoryId;

            return(View(model));
        }
        public async Task <IActionResult> Create(int categoryId)
        {
            SupplementFormViewModel model = new SupplementFormViewModel();

            await PrepareModelToReturn(categoryId, model);

            ViewData["CategoryId"] = categoryId;

            return(View(model));
        }
        public async Task <IActionResult> FinishCreate(int categoryId, SupplementFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                await PrepareModelToReturn(categoryId, model);

                ViewData["CategoryId"] = categoryId;

                return(View(nameof(Create), model));
            }

            bool isSupplementExistingByName = await this.supplementService.IsSupplementExistingByName(model.Name);

            if (isSupplementExistingByName)
            {
                TempData.AddErrorMessage(string.Format(EntityExists, SupplementEntity));

                await PrepareModelToReturn(categoryId, model);

                ViewData["CategoryId"] = categoryId;

                return(View(nameof(Create), model));
            }

            bool isSubcategoryExistingById = await this.subcategoryService.IsSubcategoryExistingById(model.SubcategoryId, false);

            if (!isSubcategoryExistingById)
            {
                TempData.AddErrorMessage(string.Format(EntityNotFound, SubcategoryEntity));

                await PrepareModelToReturn(categoryId, model);

                return(View(model));
            }

            bool isManufacturerExistingById = await this.manufacturerService.IsManufacturerExistingById(model.ManufacturerId, false);

            if (!isManufacturerExistingById)
            {
                TempData.AddErrorMessage(string.Format(EntityNotFound, ManufacturerEntity));

                await PrepareModelToReturn(categoryId, model);

                return(View(model));
            }

            byte[] picture = await model.Picture.ToByteArray();

            await this.managerSupplementService.CreateAsync(model.Name, model.Description, model.Quantity, model.Price, picture, model.BestBeforeDate, model.SubcategoryId, model.ManufacturerId);

            TempData.AddSuccessMessage(string.Format(EntityCreated, SupplementEntity));

            return(this.RedirectToSupplementsIndex(false));
        }