Ejemplo n.º 1
0
            public async Task <Response <int> > Handle(DeleteProductByIdCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                await _productRepository.DeleteAsync(product);

                return(new Response <int>(product.Id));
            }
Ejemplo n.º 2
0
            public async Task <Response <int> > Handle(UploadProductImageCommand request, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(request.ProductId);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                var productImage = _mapper.Map <ProductImage>(request);
                await _productImageRepository.AddAsync(productImage);

                return(new Response <int>(productImage.Id));
            }
Ejemplo n.º 3
0
        public async Task <Response <DeleteProductDto> > Handle(DeleteProductCommand command, CancellationToken cancellationToken)
        {
            var product = await _productRepository.GetByIdAsync(command.Id);

            if (product == null)
            {
                throw new ApiException($"Product Not Found.");
            }
            await _productRepository.RemoveAsync(product);

            var response = _mapper.Map <DeleteProductDto>(product);

            return(new Response <DeleteProductDto>(_mapper.Map <DeleteProductDto>(product), "Product remove successful"));
        }
            public async Task <Response <Guid> > Handle(DeleteProductByIdCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id).ConfigureAwait(false);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                await _productRepository.DeleteAsync(product).ConfigureAwait(false);

                await _unitOfWork.Commit <Guid>(cancellationToken).ConfigureAwait(false);

                return(new Response <Guid>(product.Id));
            }
Ejemplo n.º 5
0
            public async Task <Result <int> > Handle(DeleteProductByIdCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id);

                if (product == null)
                {
                    return(Result <int> .Failure($"Product with Id {command.Id} does not exist."));
                }
                else
                {
                    //TODO - Only Admins should be able to delete
                    await _productRepository.DeleteAsync(product);

                    return(Result <int> .Success($"Product with Id {command.Id} deleted.", command.Id));
                }
            }
Ejemplo n.º 6
0
            public async Task <Response <ProductsViewModel> > Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(query.Id);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                var productViewModel = _mapper.Map <ProductsViewModel>(product);
                var productImages    = await _productImageRepository.GetProductImagesAsync(query.Id);

                if (productImages != null && productImages.Count > 0)
                {
                    productViewModel.ImageNames = productImages.Select(x => x.ImageName).ToList();
                }
                return(new Response <ProductsViewModel>(productViewModel));
            }
Ejemplo n.º 7
0
            public async Task <Response <int> > Handle(UpdateProductCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                else
                {
                    product.Name        = command.Name;
                    product.Rate        = command.Rate;
                    product.Description = command.Description;
                    await _productRepository.UpdateAsync(product);

                    return(new Response <int>(product.Id));
                }
            }
Ejemplo n.º 8
0
            public async Task <Result <int> > Handle(UpdateProductCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id);

                if (product == null)
                {
                    return(Result <int> .Failure($"Product with Id {command.Id} does not exist."));
                }
                else
                {
                    product.Rate     = command.Rate;
                    product.IsActive = command.IsActive;
                    product.Name     = command.Name;
                    await _productRepository.UpdateAsync(product);

                    return(Result <int> .Success(product.Id));
                }
            }
            public async Task <Response <int> > Handle(DeleteProductByIdCommand command, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(command.Id);

                if (product == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                if (command.Physical)
                {
                    await _productRepository.DeleteAsync(product);
                }
                else
                {
                    product.IsDeleted = true;
                    await _productRepository.UpdateAsync(product);
                }
                await _unitOfWork.Commit(cancellationToken);

                return(new Response <int>(product.Id));
            }
Ejemplo n.º 10
0
        public async Task <Response <UpdateProductDto> > Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var command = _mapper.Map <UpdateProductRequest>(request);
            var product = await _ProductRepository.GetByIdAsync(command.Id);

            if (product == null)
            {
                throw new ApiException($"Product Not Found.");
            }
            else
            {
                product.Name        = command.Name;
                product.Description = command.Description;
                product.Price       = command.Price;
                product.Stock       = command.Stock;
                product.ImageUrl    = command.ImageUrl;

                await _ProductRepository.UpdateAsync(product);

                return(new Response <UpdateProductDto>(_mapper.Map <UpdateProductDto>(product), "Product Update Successufull"));
            }
        }
Ejemplo n.º 11
0
            public async Task <Result <int> > Handle(CreateInvoiceCommand command, CancellationToken cancellationToken)
            {
                decimal taxRate = 0.14m;
                //Check if customer is valid
                var customer = await _personRepository.GetByIdAsync(command.invoice.CustomerId);

                if (customer == null)
                {
                    return(Result <int> .Failure($"Customer with ID {command.invoice.CustomerId} is either not active or not registered yet."));
                }
                //Create Invoice Detail List Model
                var invoiceDetails = new List <Entity.InvoiceDetail>();

                foreach (var item in command.invoice.InvoiceDetails)
                {
                    var detail = new InvoiceDetail
                    {
                        ProductId = item.ProductId,
                        Quantity  = item.QuantityInCart
                    };

                    //Get Information about this product
                    var thisProduct = await _productRepository.GetByIdAsync(detail.ProductId);

                    //Check if Products are valid
                    if (thisProduct == null)
                    {
                        return(Result <int> .Failure($"Product with ID {detail.ProductId} is either not active or not registered yet."));
                    }
                    detail.Rate     = thisProduct.Rate;
                    detail.SubTotal = (detail.Rate * detail.Quantity);
                    //TODO: Get Tax Rate
                    if (thisProduct.IsTaxable)
                    {
                        detail.Tax   = taxRate * detail.SubTotal;
                        detail.Total = (detail.SubTotal + detail.Tax);
                    }
                    else
                    {
                        detail.Total = detail.SubTotal;
                    }

                    invoiceDetails.Add(detail);
                }
                //Create Invoice Model
                var invoice = new Entity.Invoice
                {
                    SubTotal = invoiceDetails.Sum(z => z.SubTotal),
                    Tax      = invoiceDetails.Sum(z => z.Tax)
                };

                invoice.Total      = invoice.SubTotal + invoice.Tax;
                invoice.CustomerId = command.invoice.CustomerId;
                invoice.Due        = invoice.Total;

                //Save Invoice and Get ID
                var newInvoice = await _invoiceRepository.AddAsync(invoice);

                //Save Invoice Details
                foreach (var item in invoiceDetails)
                {
                    item.InvoiceId = newInvoice.Id;
                    await _invoiceDetailRepository.AddAsync(item);
                }
                //Email
                //_emailScheduler.Schedule(customer.Email, "Invoice Created!", $"Your Invoice has been generated!. Your Due Amount is '{ invoice.Total}'.");
                return(Result <int> .Success(newInvoice.Id));
            }
Ejemplo n.º 12
0
            public async Task <Response <Product> > Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
            {
                var product = await _productRepository.GetByIdAsync(query.Id);

                return(new Response <Product>(product));
            }