public async Task <int> AddAsync(ProductDTO product)
        {
            this.AddCommonProcess(product);
            var productModel = this._mapper.Map <Product>(product);
            int id           = await _productsRepository.AddAsync(productModel);

            return(id);
        }
Пример #2
0
        public async Task <IActionResult> CreateAsync(/*[FromBody]*/ Product product, IFormFile file)
        {
            if (product == null || product.UserName != User.Identity.Name)
            {
                return(BadRequest());
            }

            if (file != null)
            {
                product.ImageMimeType = file.ContentType;

                using (var memoryStream = new MemoryStream()) {
                    await file.CopyToAsync(memoryStream);

                    product.ImageFile = memoryStream.ToArray();
                }

                //product.ImageFile = new byte[file.Length];
                //await file.OpenReadStream().ReadAsync(product.ImageFile, 0, (int)file.Length);
            }

            await _ProductsRepository.AddAsync(product);

            return(CreatedAtRoute("GetProduct", new { id = product.Id }, product));
        }
Пример #3
0
        public async Task HandleAsync(CreateProductCommand command)
        {
            var product = new Product(command.Id, command.Name,
                                      command.Description, command.Vendor,
                                      command.Price, command.Quantity);

            await _productsRepository.AddAsync(product);
        }
Пример #4
0
 public async Task HandleAsync(CreateProduct command)
 {
     if (await _repository.ExistsAsync(command.ProductId))
     {
         throw new ProductAlreadyExistsException(command.ProductId);
     }
     var product = Product.Create(command.ProductId, command.Name, command.Price);
     await _repository.AddAsync(product);
 }
Пример #5
0
        public async Task <IActionResult> CreateAsync([FromBody] Product product)
        {
            if (product == null || product.UserName != User.Identity.Name)
            {
                return(BadRequest());
            }

            await _ProductsRepository.AddAsync(product);

            return(CreatedAtRoute("GetProduct", new { id = product.Id }, product));
        }
Пример #6
0
        public async Task HandleAsync(CreateProduct command, ICorrelationContext context)
        {
            if (await _productsRepository.ExistsAsync(command.Name))
            {
                throw new DistributedEStoreException("product_already_exists",
                                                     $"Product: '{command.Name}' already exists.");
            }

            var product = new DomainEntities.Product(command.Id, command.Name, command.Description,
                                                     command.Price, command.Category, command.ImageURLs, command.Colors);
            await _productsRepository.AddAsync(product);
        }
        public async Task <IActionResult> Create([Bind("ProductName,SupplierId,CategoryId,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued")] Products product)
        {
            if (ModelState.IsValid)
            {
                await _productsRepository.AddAsync(product);

                return(RedirectToAction(nameof(Index)));
            }

            BuildCreateAndEditFormsViewDataDictionaries(product.CategoryId, product.SupplierId);
            return(View(product));
        }
        public async Task HandleAsync(AddProduct command)
        {
            if (command.Name == "invalid")
            {
                throw new ArgumentException("Invalid product name.", nameof(command.Name));
            }

            var product = new Product(command.Id, command.Name, command.Category, command.Description, command.Price);
            await _productsRepository.AddAsync(product);

            // eventDispatcher.PublishAsync(new ProductCreated(product))
        }
Пример #9
0
        public async Task HandleAsync(CreateProduct command, ICorrelationContext context)
        {
            if (await _productsRepository.ExistsAsync(command.Name))
            {
                throw new DShopException("product_already_exists",
                                         $"Product: '{command.Name}' already exists.");
            }

            var product = new Product(command.Id, command.Name);
            await _productsRepository.AddAsync(product);

            await _busPublisher.PublishAsync(new ProductCreated(command.Id, command.Name), context);
        }
Пример #10
0
        public async Task<object> PostAsync(CreateProduct request)
        {
            var existingProduct = await _productsRepository.GetByIdsAsync(new int[] { request.CatalogNumber });
            if(existingProduct.FirstOrDefault() != null)
            {
                throw HttpError.Conflict($"Product with Catalog Number: {request.CatalogNumber} already exists.");
            }

            var product = request.ConvertTo<Product>();
            await _productsRepository.AddAsync(product);

            return new CreateProductResponse();
        }
Пример #11
0
        public async Task <IActionResult> CreateProduct([FromBody] Product product)
        {
            try
            {
                await _productsRepository.AddAsync(product);

                return(CreatedAtAction(nameof(GetProductById), new { id = product.Id }, product));
            }
            catch (System.Exception ex)
            {
                _logger.LogError($"Error creating new catalog produtct: {product}.");
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IActionResult> Create([Bind("Title,Price,Currency")] Product model, IFormFile file)
        {
            if (!ModelState.IsValid || file == null)
            {
                return(BadRequest(ModelState));
            }
            var filePath = await _imageService.SaveImage(file, "products");

            model.ImageName = filePath;
            await _productsRepository.AddAsync(model);

            //return Ok();
            return(RedirectToAction(nameof(Index)));
        }
Пример #13
0
        public async Task <ProductsResponse> AddAsync(Products products)
        {
            try
            {
                await _productsRepository.AddAsync(products);

                await _unitOfWork.CompleteAsync();

                return(new ProductsResponse(products));
            }
            catch (Exception e)
            {
                return(new ProductsResponse($"error: {e.Message}"));
            }
        }
Пример #14
0
        public async Task AddProduct(string name, decimal cost, string category)
        {
            var product = await _productsRepository.Find(x => x.Name == name && x.Category == category);

            if (product.Any())
            {
                throw new ServiceException(ErrorCodes.ProductAlreadyExists, $"Product with Name: '{name}' and Category: '{category}' already exists.");
            }

            var productToAdd = new Product
            {
                Name     = name,
                Cost     = cost,
                Category = category
            };
            await _productsRepository.AddAsync(productToAdd);
        }
Пример #15
0
        public async Task HandleAsync(CreateProduct command, ICorrelationContext context)
        {
            if (command.Quantity < 0)
            {
                throw new DShopException("invalid_product_quantity",
                                         "Product quantity cannot be negative.");
            }

            if (await _productsRepository.ExistsAsync(command.Name))
            {
                throw new DShopException("product_already_exists",
                                         $"Product: '{command.Name}' already exists.");
            }

            var product = new Product(command.Id, command.Name,
                                      command.Description, command.Vendor, command.Price, command.Quantity);
            await _productsRepository.AddAsync(product);

            await _busPublisher.PublishAsync(new ProductCreated(command.Id, command.Name,
                                                                command.Description, command.Vendor, command.Price, command.Quantity), context);
        }
        public async Task <ProductStatusResponse> SaveAsync(ProductSaveRequest request, Product product)
        {
            try {
                if ((await categoryRepository.FindByIdAsync(request.CategoryId)) == null)
                {
                    return(new ProductStatusResponse("Invald Category Id."));
                }

                for (int i = 0; i < request.Images.Count; i++)
                {
                    product.Images.Add(new ImagePath {
                        Filename = await fileService.StoreImage(config.GetSection("ProductsImages").Value, request.Images[i])
                    });
                }

                Product newAddedProduct = await productsRepository.AddAsync(product);

                return(new ProductStatusResponse(newAddedProduct));
            } catch (Exception e) {
                return(new ProductStatusResponse(e.Message));
            }
        }
 public async Task HandleAsync(CreateProduct command, ICorrelationContext context)
 => await _handler
 .Handle(async() =>
 {
     if (await _productsRepository.ExistsAsync(command.Name))
     {
         throw new DShopException("product_already_exists",
                                  $"Product: '{command.Name}' already exists.");
     }
     var product = new Product(command.Id, command.Name,
                               command.Description, command.Vendor, command.Price);
     await _productsRepository.AddAsync(product);
 })
 .OnSuccess(async() => await _busPublisher.PublishAsync(
                new ProductCreated(command.Id, command.Name, command.Description, command.Vendor, command.Price),
                context)
            )
 .OnCustomError(async ex => await _busPublisher.PublishAsync(
                    new CreateProductRejected(command.Id, ex.Message, ex.Code), context)
                )
 .OnError(async ex => await _busPublisher.PublishAsync(
              new CreateProductRejected(command.Id, ex.Message, "create_product_failed"), context)
          )
 .ExecuteAsync();
Пример #18
0
 public async Task CreateAsync(ProductDetailsDto dto)
 {
     var product = new Product(dto.Id, dto.Name, dto.Category, dto.Description, dto.Price);
     await _productsRepository.AddAsync(product);
 }
Пример #19
0
 public async Task <IActionResult> AddAsync([FromBody] Product product)
 {
     return(Ok(await _repository.AddAsync(product)));
 }
 public async Task HandleAsync(ProductCreatedEvent @event, ICorrelationContext context)
 {
     var product = new Product(@event.Id, @event.Name, @event.Price, @event.Quantity);
     await _productsRepository.AddAsync(product);
 }
 public async Task HandleAsync(CreateProduct command)
 {
     var id      = command.Id == Guid.Empty ? Guid.NewGuid() : command.Id;
     var product = new Product(id, command.Name, command.Price, command.Description);
     await _productsRepository.AddAsync(product);
 }
Пример #22
0
        public async Task <IActionResult> Add(Product product)
        {
            await productsRepository.AddAsync(product);

            return(RedirectToAction("Index"));
        }
Пример #23
0
        public async Task <ActionResult> Add(NewProduct product, CancellationToken cancellationToken)
        {
            await _repository.AddAsync(product, cancellationToken);

            return(Ok());
        }