コード例 #1
0
        public async Task <IActionResult> AddProduct(
            [Bind("Name", "FormFile", "Price", "Details", "CategoryId", "Categories", "IsFeatured", "IsNewArrival")] AddProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                Product product = new Product();
                //Create fileName for Photo by using SaveAsync() extension method
                var fileName = await model.FormFile.SaveAsync(webHost.WebRootPath, "ProductImages");

                model.PhotoPath = fileName;
                // Map properties of viewModel to product
                mapper.Map(model, product);

                //After mapping create product
                var result = await productRepo.Create(product);

                if (result != null)
                {
                    return(RedirectToAction("ManageProducts", "Products"));
                }
            }
            model.Categories = await categoryRepo.GetAll();

            return(View(model));
        }
コード例 #2
0
        public OperationResult Create(CreateProduct command)
        {
            OperationResult operationResult = new OperationResult();

            if (_productRepo.Exists(c => c.Name == command.Name))
            {
                return(operationResult.Failed(ApplicationMessage.duplicated));
            }
            if (_productRepo.Exists(c => c.Code == command.Code))
            {
                return(operationResult.Failed(ApplicationMessage.duplicated));
            }


            var slug = GenerateSlug.Slugify(command.Slug);

            var categorySlug = _productCategoryRepo.GetcategorySlugeby(command.CategoryId);
            var path         = $"{categorySlug}/{slug}";
            var pictuepath   = _fileUploader.Upload(command.picture, path);


            var Product = new Product(command.Name, command.Code, command.ShortDescription, command.Description,
                                      pictuepath, command.pictureAlt, command.pictureTitle, command.KeyWords,
                                      command.MetaDescription, slug, command.CategoryId);

            _productRepo.Create(Product);
            _productRepo.Save();
            return(operationResult.Succeeded());
        }
コード例 #3
0
        public Product Create(ProductViewModel product)
        {
            Product newProduct = new Product()
            {
                ProductType = product.ProductType,
                Number      = product.Number,
                Name        = product.Name,
                Description = product.Description,
                Price       = product.Price
            };

            return(_productRepo.Create(newProduct));
        }
コード例 #4
0
        public async Task CollectData(IList <ProductCreateDto> products)
        {
            var productModals = _mapper.Map <IList <Product> >(products);

            await productModals.ForEachAsync(async product =>
            {
                var avg         = await _repository.GetAvgPriceById(product.ProductId);
                var diff        = avg == -999 ? 0 : (Convert.ToDecimal(product.Price) - avg) / avg;
                product.Compare = Convert.ToDouble(diff);
                await _repository.Create(product);
            });

            await _repository.SaveChange();
        }
        public async Task <ActionResult <Product> > CreateProduct(Product product)
        {
            try
            {
                if (product != null)
                {
                    product.Id = Guid.NewGuid().ToString();
                    var createdProduct = await repo.Create(product);

                    if (createdProduct != null)
                    {
                        return(CreatedAtAction(nameof(GetProduct), new { id = createdProduct.Id },
                                               createdProduct));
                    }
                }
                return(BadRequest());
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  $"Error retrieving data from the database. Error message:{e.Message}"));
            }
        }
コード例 #6
0
        public async Task <ActionResult <ProductDto> > CreateProduct([FromForm] ProductCreateDto productCreateDto)
        {
            var product = mapper.Map <Product>(productCreateDto);

            if (productCreateDto.Image != null)
            {
                using (var memoryStream = new MemoryStream())
                {
                    await productCreateDto.Image.CopyToAsync(memoryStream);

                    var content   = memoryStream;
                    var extension = Path.GetExtension(productCreateDto.Image.FileName);
                    product.Image =
                        await fileStorage.SaveFile(content, extension, containerName);
                }
            }

            repository.Create(product);
            repository.SaveChangesAsync();
            var productDto = mapper.Map <ProductDto>(product);

            return(CreatedAtRoute(nameof(GetProductById), new { productDto.Id }, productDto));
        }
コード例 #7
0
 public ActionResult Create(ProductVM pro)
 {
     try
     {
         if (!ModelState.IsValid)
         {
             return(View(pro));
         }
         var product   = _mapper.Map <Product>(pro);
         var isSuccess = _repo.Create(product);
         if (!isSuccess)
         {
             ModelState.AddModelError("", "Check for Information");
             return(View(pro));
         }
         return(RedirectToAction(nameof(Index)));
     }
     catch
     {
         ModelState.AddModelError("", "Check for Information");
         return(View());
     }
 }
コード例 #8
0
        public async Task <ActionResult <Product> > CreateProduct([FromBody] Product product)
        {
            await _repo.Create(product);

            return(CreatedAtRoute("GetProduct", new{ id = product.Id }, product));
        }
コード例 #9
0
 public Product CreateProduct(Product product)
 {
     return(_productRepo.Create(product));
 }
コード例 #10
0
ファイル: DataAccessService.cs プロジェクト: CanTuylu/AdoNet
 public int AddProduct(Product product)
 {
     return(proRepo.Create(product));
 }