public async Task <IActionResult> Create([FromBody] CreateInvoiceInput createInvoiceInput) { var apiResponse = new ApiResponse(); if (!this.ModelState.IsValid) { apiResponse.IsSuccessful = false; return(this.BadRequest(apiResponse)); } try { createInvoiceInput.CreatedBy = HttpContext.User.Claims.FirstOrDefault(x => x.Type == "fullname")?.Value; apiResponse = await this.invoiceService.CreateInvoice(createInvoiceInput); if (apiResponse.IsSuccessful) { return(this.Ok(apiResponse)); } return(this.BadRequest(apiResponse)); } catch (Exception exception) { return(this.StatusCode( 500, new ApiResponse { IsSuccessful = false, Message = exception.InnerException.Message })); } }
/// <inheritdoc /> /// <summary> /// The create invoice. /// </summary> /// <param name="createInvoiceInput"> /// The create invoice input. /// </param> /// <returns> /// The <see cref="T:System.Threading.Tasks.Task" />. /// </returns> public async Task <ApiResponse> CreateInvoice(CreateInvoiceInput createInvoiceInput) { Recipient recipient; if (string.IsNullOrEmpty(createInvoiceInput.RecipientNumber)) { // map recipient recipient = new Recipient { RecipientNumber = $"RNo.{this.recipientRepository.GetRecipientCount() + 1}", Address = createInvoiceInput.Address, Name = createInvoiceInput.Name }; this.recipientRepository.Add(recipient); } else { recipient = await this.recipientRepository.FindById(createInvoiceInput.RecipientNumber); if (recipient == null) { return(new ApiResponse { IsSuccessful = false, Message = "Recipient not found" }); } } // map invoice var invoice = new Invoice { Approved = false, CreatedOn = DateTime.Now, CreatedBy = createInvoiceInput.CreatedBy, Title = createInvoiceInput.Title, Status = true, InvoiceNo = $"INo.{this.invoiceRepository.GetInvoiceCount() + 1}" }; // map authorities var authorities = createInvoiceInput.Authorities.Select(x => new Authority { Name = x.Name, Department = x.Department, Approved = false }); // map invoice items var invoiceItems = createInvoiceInput.InvoiceItems.Select(x => new InvoiceItem { Name = x.Name, Amount = x.Amount, Description = x.Description }); // assign and add to database invoice.InvoiceItems = invoiceItems.ToList(); invoice.Authorities = authorities.ToList(); recipient.Invoices.Add(invoice); await this.recipientRepository.SaveChanges(); return(new ApiResponse { IsSuccessful = true, Message = "Invoice Created" }); }