Пример #1
0
        public async Task <OperationDetails> AddHookah(HookahDTO hookahDto)
        {
            try
            {
                Hookah hookah = new Hookah()
                {
                    ProductId   = Guid.NewGuid(),
                    Mark        = hookahDto.Mark.Trim(),
                    Model       = hookahDto.Model.Trim(),
                    Description = (hookahDto.Description == null || hookahDto.Description.Trim() == "") ? "Отсутствует" : hookahDto.Description.Trim(),
                    Country     = (hookahDto.Country == null || hookahDto.Country.Trim() == "") ? "Нет данных" : hookahDto.Country.Trim(),
                    Height      = hookahDto.Height,
                    Price       = hookahDto.Price,
                    //если файл не передан, то устанавливаем изображение по умолчанию
                    Image = (hookahDto.Image == null) ? "/Files/ProductImages/defaultImage.jpg" : hookahDto.Image
                };
                db.Hookahs.Add(hookah);
                await db.SaveAsync();

                return(new OperationDetails(true, "Товар успешно добавлен", ""));
            }
            catch
            {
                return(new OperationDetails(false, "При добавлении товара произошла ошибка", ""));
            }
        }
Пример #2
0
        public async Task <OperationDetails> EditHookah(HookahDTO hookahDto)
        {
            try
            {
                Hookah hookah = await db.Products.FindByIdAsync(hookahDto.ProductId) as Hookah;

                hookah.Mark        = hookahDto.Mark.Trim();
                hookah.Model       = hookahDto.Model.Trim();
                hookah.Description = (hookahDto.Description == null || hookahDto.Description.Trim() == "") ? "Отсутствует" : hookahDto.Description.Trim();
                hookah.Country     = (hookahDto.Country == null || hookahDto.Country.Trim() == "") ? "Нет данных" : hookahDto.Country.Trim();
                hookah.Height      = hookahDto.Height;
                hookah.Price       = hookahDto.Price;
                //если новое изображение не передано, то оставляем старое
                hookah.Image = (hookahDto.Image == null) ? hookah.Image : hookahDto.Image;

                db.Hookahs.Update(hookah);
                await db.SaveAsync();

                return(new OperationDetails(true, "Товар успешно обновлен", ""));
            }
            catch (DbUpdateConcurrencyException)
            {
                return(new OperationDetails(false, "Данный товар ранее уже был изменен", ""));
            }
            catch
            {
                return(new OperationDetails(false, "При изменении товара произошла ошибка", ""));
            }
        }
Пример #3
0
        public async Task <(ProductDTO, ProductType)> GetProductParamsAsync(Guid productId)
        {
            var product = await db.Products.FindByIdAsync(productId);

            if (product is Hookah)
            {
                //Mapper.Initialize(cfg => cfg.CreateMap<Hookah, HookahDTO>());
                Mapper.Initialize(cfg => cfg.AddProfile <AutomapperProfile>());
                HookahDTO hookah = Mapper.Map <Hookah, HookahDTO>(product as Hookah);
                return(hookah, ProductType.Hookah);
            }
            else
            {
                return(null, ProductType.HookahTobacco);
            }
        }
Пример #4
0
        public async Task <ActionResult> AddEditHookah(HookahViewModel hvm, HttpPostedFileBase uploadImage)
        {
            if (ModelState.IsValid)
            {
                //добавление изображения к товару
                string imagePath = null;
                if (uploadImage != null)
                {
                    if (!uploadImage.ContentType.StartsWith("image"))
                    {
                        ModelState.AddModelError("", "Недопустимый тип файла");
                    }
                    else
                    {
                        imagePath = "/Files/ProductImages/" + Path.GetFileName(uploadImage.FileName);
                        uploadImage.SaveAs(Server.MapPath("~" + imagePath));
                    }
                }

                //создание DTO из ViewModel
                Mapper.Initialize(cfg => cfg.CreateMap <HookahViewModel, HookahDTO>());
                HookahDTO hookahDto = Mapper.Map <HookahViewModel, HookahDTO>(hvm);
                hookahDto.Image = imagePath;

                if (hvm.ProductId == Guid.Empty) //добавление нового продукта
                {
                    await productService.AddHookah(hookahDto);

                    return(RedirectToAction("AddEditHookah"));
                }
                else //изменение существующего
                {
                    await productService.EditHookah(hookahDto);

                    return(RedirectToAction("Products"));
                }
            }
            else
            {
                return(View("AddEditHookah", hvm));
            }
        }
Пример #5
0
        public async Task <ActionResult> EditProduct(Guid?productId)
        {
            if (productId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var(product, productType) = await productService.GetProductParamsAsync((Guid)productId);

            if (productType == ProductType.Hookah)
            {
                HookahDTO       hookah = (HookahDTO)product;
                HookahViewModel hvm    = new HookahViewModel()
                {
                    ProductId = hookah.ProductId, Mark = hookah.Mark, Model = hookah.Model, Country = hookah.Country, Description = hookah.Description, Price = hookah.Price, Height = hookah.Height, Image = hookah.Image
                };
                return(View("AddEditHookah", hvm));
            }
            return(RedirectToAction("Products"));
        }