示例#1
0
        private bool CheckIfProductExists(CreateProductBindingModel model)
        {
            var product = this.dbContext.Products
                          .Where(x => x.Brand.IsDeleted == false && x.Category.IsDeleted == false)
                          .FirstOrDefault(x => x.Name == model.Name);

            if (product != null)
            {
                return(true);
            }

            return(false);
        }
示例#2
0
        private Product CreateProductByModelBrandAndCategory(CreateProductBindingModel model, Brand brand, Category category)
        {
            Product product = new Product()
            {
                Category = category,
                Price    = decimal.Parse(model.Price),
                Name     = model.Name,
                Brand    = brand,
                Image    = this.ParseToImgDataURL(model.Image) ?? null,
            };

            return(product);
        }
        public async Task CreateAsync(CreateProductBindingModel model)
        {
            Validator.ThrowIfNull(model);

            var product = this.Mapper.Map <Product>(model);

            product.CategoryId = model.CategoryId;
            product.CreatedAt  = DateTime.Now;

            await this.Repository.AddAsync(product);

            await this.Repository.SaveChangesAsync();
        }
        public async Task <bool> AddProduct(CreateProductBindingModel model)
        {
            var product = AutoMapper.Mapper.Map <Product>(model);

            string uniqueFileName = await this.cloudinaryService.CreateImage(model.FormImage, model.Name);

            product.ImageName = uniqueFileName;

            await this.context.Products.AddAsync(product);

            var result = await this.context.SaveChangesAsync();

            return(result > 0);
        }
        public async Task <IActionResult> Create(CreateProductBindingModel model)
        {
            var subCategories = await this.subCategoryService.GetSubCategories();

            if (!this.ModelState.IsValid)
            {
                return(this.View(model));
            }

            await this.productService.AddProduct(model);

            this.TempData["Success"] = $"Successully created {model.Name}";

            return(this.RedirectToAction("Create"));
        }
示例#6
0
        public IActionResult Create(CreateProductBindingModel createProduct)
        {
            if (!ModelState.IsValid)
            {
                return(Redirect("/"));
            }
            string pictureUrl = this._cloudinaryService.UploadPictureAsync(
                createProduct.Picture,
                createProduct.Title);

            productServices.Create(createProduct.Title, createProduct.ProductTypes, createProduct.Price,
                                   createProduct.Quantity, createProduct.Description, createProduct.Author, createProduct.Publishing,
                                   createProduct.YearOfPublishing, pictureUrl, createProduct.YouTubeLink, createProduct.GenreId);
            return(this.Redirect("/"));
        }
示例#7
0
        public IActionResult Create()
        {
            var genres = _genreService.All().Select(x => new GenreViewModels()
            {
                Id   = x.Id,
                Name = x.Name
            }).ToList();

            CreateProductBindingModel createProduct = new CreateProductBindingModel()
            {
                GenresViewModel = genres
            };

            return(View(createProduct));
        }
示例#8
0
        public async Task <IActionResult> CreateProduct(CreateProductBindingModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            foreach (var image in model.Images)
            {
                if (image.ContentType != "image/png")
                {
                    ModelState.AddModelError("ImageTypeMismatch", "One of your images extension is incorrect.");
                    return(View());
                }
            }

            await _adminManager.CreateProductAsync(model);

            TempData["Success"] = "You successfully created new product.";
            return(RedirectToAction("CreateProduct"));
        }
        public IActionResult Delete(CreateProductBindingModel model)
        {
            if (!this.User.IsInRole("1"))
            {
                return this.RedirectToAction("/");
            }

            using (this.Context)
            {
                var product = this.Context.Products.Find(model.Id);

                if (product == null)
                {
                    return this.RedirectToAction("/");
                }

                product.IsDeleted = true;
                this.Context.SaveChanges();
            }

            return this.RedirectToAction("/");
        }
示例#10
0
        public async Task CreateProductAsync(CreateProductBindingModel model)
        {
            var imageList   = new List <Image>();
            var webRootPath = Path.Combine(_hostingEnvironment.WebRootPath, "images");

            foreach (var image in model.Images)
            {
                if (image.Length > 0)
                {
                    var imageName = Regex.Replace(string.Format("{0}-{1}.png", model.Name, Guid.NewGuid()), @"\s+", "-");

                    using (var fileStream = new FileStream(Path.Combine(webRootPath, imageName), FileMode.Create))
                    {
                        await image.CopyToAsync(fileStream);

                        imageList.Add(new Image()
                        {
                            Path = Path.Combine("images", imageName)
                        });
                    }
                }
            }

            await _db.Products
            .AddAsync(new Product()
            {
                Name       = model.Name,
                Price      = model.Price,
                Details    = model.Details,
                Color      = model.Color,
                Quantity   = 99,
                CategoryId = model.CategoryId,
                BrandId    = model.BrandId,
                Images     = imageList,
            });

            await _db.SaveChangesAsync();
        }
示例#11
0
        public ProductDTO Create(CreateProductBindingModel model, IFormFile image)
        {
            if (this.CheckIfProductExists(model))
            {
                var productExists = this.dbContext.Products.FirstOrDefault(x => x.Name == model.Name);
                if (productExists.IsDeleted == true)
                {
                    productExists.IsDeleted = false;
                    this.dbContext.Products.Update(productExists);
                    this.dbContext.SaveChanges();

                    var brandExists    = this.FindBrandByName(model.Brand);
                    var categoryExists = this.FindCategoryByName(model.Category);

                    var productToMap = this.CreateProductByModelBrandAndCategory(model, brandExists, categoryExists);

                    return(this.mapper.Map <ProductDTO>(productToMap));
                }

                return(null);
            }

            var brand    = this.FindBrandByName(model.Brand);
            var category = this.FindCategoryByName(model.Category);

            if (this.CheckIfCategoryOrBrandIsNull(category, brand))
            {
                return(null);
            }

            var product = this.CreateProductByModelBrandAndCategory(model, brand, category);

            this.dbContext.Products.Add(product);
            this.dbContext.SaveChanges();

            return(this.mapper.Map <ProductDTO>(product));
        }
示例#12
0
        public IActionResult Create(CreateProductBindingModel model)
        {
            if (!this.IsValidModel(model))
            {
                this.ViewData[ErrorKey] = InvalidProductCreationMessage;

                return(this.View());
            }
            using (this.Context)
            {
                bool validPrice = decimal.TryParse(model.Price, out decimal price) && (price >= ValidationConstants.ProductConstraints.MinPrice && price < ValidationConstants.ProductConstraints.MaxPrice);

                if (!validPrice)
                {
                    this.ViewData[ErrorKey] = InvalidProductCreationMessage;

                    return(this.View());
                }

                ProductType productType = this.Context.ProductTypes.FirstOrDefault(x => x.ProductTypeName == model.ProductType);

                Product product = new Product()
                {
                    Name          = model.Name,
                    Price         = price,
                    Description   = model.Description,
                    ProductTypeId = productType.Id,
                    ProductType   = productType
                };

                this.Context.Products.Add(product);
                this.Context.SaveChanges();

                return(RedirectToHome);
            }
        }
        public async Task <IActionResult> Create(CreateProductBindingModel model)
        {
            await _productService.CreateAsync(model);

            return(this.RedirectToAction(ActionConstants.Index, ControllerConstants.Products));
        }
示例#14
0
        private CreateProductBrandAndCategoryAndDataViewModel CreateProductBrandAndCategoryAndDataViewModel(CategoryBrandViewModel brandCategoryViewModel, CreateProductBindingModel createProductBindingModel)
        {
            var productBrandAndCategoryAndDataViewModel = new CreateProductBrandAndCategoryAndDataViewModel {
                BrandCategoryViewModel = brandCategoryViewModel, CreateProductBindingModel = createProductBindingModel
            };

            return(productBrandAndCategoryAndDataViewModel);
        }