public async Task <object> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            string imagePath = string.Empty;

            if (request.Image != null)
            {
                imagePath = await _imageService.UploadAsync(request.Image);
            }

            var product = new OnlineShop.Domain.Entities.Product()
            {
                Name          = request.Name,
                Description   = request.Description,
                Price         = request.Price,
                MeasureUnitId = request.MeasureUnit,
                VendorId      = request.Vendor,
                Qty           = request.Qty,
                ImagePath     = imagePath,
                CategoryId    = request.Category,
                Active        = true
            };

            try
            {
                await dbContext.Products.AddAsync(product);

                await dbContext.SaveChangesAsync();

                return(new { message = "Succesfuly created product" });
            }
            catch (Exception ex)
            {
                return(new { error = ex.Message });
            }
        }
示例#2
0
        public async Task <IResponse> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var productUpdate = await _dbContext.Products.FindAsync(request.Id);

            if (productUpdate == null)
            {
                return(new NotFoundResponse());
            }

            string imagePath = string.Empty;

            if (request.Image != null)
            {
                imagePath = await _imageService.UploadAsync(request.Image);
            }

            productUpdate.Name          = request.Name;
            productUpdate.Description   = request.Description;
            productUpdate.MeasureUnitId = request.MeasureUnit;
            productUpdate.VendorId      = request.Vendor;
            productUpdate.Price         = request.Price;
            productUpdate.Qty           = request.Qty;
            productUpdate.ImagePath     = imagePath;

            _dbContext.Products.Update(productUpdate);

            var result = await _dbContext.SaveChangesAsync() > 0;

            if (result)
            {
                return(new OkResponse <string>("Succesfuly updated product"));
            }
            else
            {
                return(new BadResponse("Something went wrong"));
            }
        }