Exemplo n.º 1
0
        public async Task <IActionResult> CreateInvoice([FromBody] ViewModel.CreateInvoiceRequest request)
        {
            try
            {
                // to enable idempotency.
                var invoice = await _invoiceRepository.GetInvoiceByTripAsync(request.TripId);

                if (invoice != null)
                {
                    return(CreatedAtAction(nameof(GetInvoice), new { invoiceId = invoice.InvoiceId }, invoice.InvoiceId));
                }

                invoice = new Domain.Invoice.Model.Invoice(
                    request.PaymentMethod.Id,
                    request.TripId,
                    request.Duration,
                    request.Distance,
                    request.TripStatus.Id);

                await _invoiceRepository.AddInvoiceAsync(invoice);

                // integration with external payment system.
                if (Equals(invoice.PaymentMethod, PaymentMethod.CreditCard) && invoice.Total > 0)
                {
                    await _paymentService.PerformPayment(invoice, request.UserId);
                }

                return(CreatedAtAction(nameof(GetInvoice), new { invoiceId = invoice.InvoiceId }, invoice.InvoiceId));
            }
            finally
            {
                _invoiceRepository.Dispose();
            }
        }
        public async Task <IActionResult> CreateTrip([FromBody] ViewModel.CreateInvoiceRequest request)
        {
            // to enable idempotency.
            var invoice = await _invoiceRepository.GetInvoiceByTripAsync(request.TripId);

            if (invoice != null)
            {
                return(Created(HttpContext.Request.GetUri().AbsoluteUri, invoice.InvoiceId));
            }

            invoice = new Domain.Invoice.Model.Invoice(
                request.PaymentMethod.Id,
                request.TripId,
                request.Duration,
                request.Distance,
                request.TripStatus.Id);

            await _invoiceRepository.AddInvoiceAsync(invoice);

            return(Created(HttpContext.Request.GetUri().AbsoluteUri, invoice.InvoiceId));
        }