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 });
            }
        }
Example #2
0
        public async Task <bool> CreateAsync(CreateShipperCommand req)
        {
            var shippingTypes = new List <OnlineShop.Domain.Entities.ShippingType>();

            foreach (var id in req.ShippingTypes)
            {
                var sT = await _dbContext.ShippingTypes.FindAsync(id);

                if (sT != null)
                {
                    shippingTypes.Add(sT);
                }
            }

            var shipper = new OnlineShop.Domain.Entities.Shipper
            {
                FirstName     = req.FirstName,
                LastName      = req.LastName,
                Address       = req.Address,
                City          = req.City,
                State         = req.State,
                ShippingTypes = shippingTypes
            };

            try
            {
                await _dbContext.Shippers.AddAsync(shipper);

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

                if (result)
                {
                    await _cacheService
                    .DeleteKeyAsync(RedisDefaultKeys.GetAllShippers);
                }

                return(result);
            }
            catch (Exception ex)
            {
                //return ex.Message;
                return(false);
            }
        }
Example #3
0
        public async Task <bool> DeleteAsync(DeleteOrderCommand command)
        {
            var orderToDelete = await _dbContext.Orders.FindAsync(command.Id);

            if (orderToDelete == null)
            {
                return(false);
            }

            _dbContext.Orders.Remove(orderToDelete);

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

            if (result)
            {
                await _cacheService
                .DeleteKeyAsync(RedisDefaultKeys.GetAllOrders);
            }

            return(result);
        }
Example #4
0
        public async Task <bool> Handle(DeleteVendorCommand request, CancellationToken cancellationToken)
        {
            var deleteVendor = await dbContext.Vendors.FindAsync(request.Id);

            if (deleteVendor == null)
            {
                throw new ArgumentNullException("Id", "There is no vendor with this id");
            }

            dbContext.Vendors.Remove(deleteVendor);

            return(await dbContext.SaveChangesAsync() == 1 ? true : false);
        }
Example #5
0
        public async Task <string> Handle(CreateVendorCommand request, CancellationToken cancellationToken)
        {
            var vendor = new OnlineShop.Domain.Entities.Vendor
            {
                FirstName = request.FirstName,
                LastName  = request.LastName,
                Address   = request.Address,
                City      = request.City,
                State     = request.State,
                TaxId     = request.TaxId,
                Discount  = request.Discount
            };

            await dbContext.Vendors.AddAsync(vendor);

            var result = await dbContext.SaveChangesAsync();

            return(result == 1 ? "Succesfuly created new vendor" : "There was a problem while creating new vendor");
        }
        public async Task <bool> Handle(UpdateVendorCommand request, CancellationToken cancellationToken)
        {
            var updateVendor = await dbContext.Vendors.FindAsync(request.Id);

            if (updateVendor == null)
            {
                throw new ArgumentNullException("Id", "There is no vendor with this id");
            }

            updateVendor.Address  = request.Address;
            updateVendor.City     = request.City;
            updateVendor.State    = request.State;
            updateVendor.Discount = request.Discount;
            updateVendor.TaxId    = request.TaxId;

            dbContext.Vendors.Update(updateVendor);

            return(await dbContext.SaveChangesAsync() == 1 ? true : false);
        }
Example #7
0
        public async Task <IResponse> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var productDelete = await _dbContext.Products.FindAsync(request.Id);

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

            _dbContext.Products.Remove(productDelete);

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

            if (result)
            {
                return(new OkResponse <string>("Succesfuly removed product"));
            }
            else
            {
                return(new BadResponse("Something went wrong while removing product"));
            }
        }
Example #8
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"));
            }
        }