Пример #1
0
        public async Task <IActionResult> Create(CreateGoodModel createGoodModel)
        {
            if (ModelState.IsValid)
            {
                var name       = User.Identity.Name;
                var loggedUser = await _adminRepository.GetByEmailAsync(name);

                string pathToPhoto = null;
                if (createGoodModel.UploadedFile != null)
                {
                    pathToPhoto = await SaveFile(createGoodModel.UploadedFile);
                }
                else
                {
                    ModelState.AddModelError("UploadedFile", "Не указан файл");
                }

                if (ModelState.IsValid)
                {
                    await _goodsRepository.AddAsync(new DomainLayer.Entities.Good
                    {
                        Title       = createGoodModel.Title,
                        AdminId     = loggedUser.Id,
                        CreatedAt   = DateTime.UtcNow,
                        CategoryId  = createGoodModel.CategoryId,
                        Cost        = createGoodModel.Cost,
                        RealCost    = createGoodModel.RealCost,
                        ImagePath   = pathToPhoto,
                        Description = createGoodModel.Description
                    });

                    return(RedirectToAction("GoodIndex", "Shop"));
                }

                var items = await CreateCategoryItems();

                createGoodModel.Categories = items;
            }

            return(View(createGoodModel));
        }