Пример #1
0
            public async Task <Response <Person> > Handle(GetProductByIdQuery request, CancellationToken cancellationToken)
            {
                var person = await repositoryAsync.GetByIdAsync(request.Id);

                if (person == null)
                {
                    throw new ApiException($"Product Not Found.");
                }
                return(new Response <Person>(person));
            }
Пример #2
0
            public async Task <Response <int> > Handle(DeletePersonByIdCommand request, CancellationToken cancellationToken)
            {
                var person = await _personRepositoryAsync.GetByIdAsync(request.Id);

                if (person == null)
                {
                    throw new ApiException($"Not found this id: {request.Id}");
                }
                else
                {
                    await _personRepositoryAsync.DeleteAsync(person);

                    return(new Response <int>(person.Id));
                }
            }
Пример #3
0
            public async Task <Response <int> > Handle(UpdatePersonCommand request, CancellationToken cancellationToken)
            {
                var person = _personRepositoryAsync.GetByIdAsync(request.Id);

                if (person == null)
                {
                    throw new ApiException($"Not found this id: {request.Id}");
                }
                else
                {
                    var updatePerson = _mapper.Map <Person>(request);
                    await _personRepositoryAsync.UpdateAsync(updatePerson);

                    return(new Response <int>(person.Id));
                }
            }
Пример #4
0
 public Task <Person> GetByIdAsync(int personId)
 {
     return(personRepository.GetByIdAsync(personId));
 }
Пример #5
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));
            }