public async Task <InvoiceSupplement> UpdateAsync(InvoiceSupplement invoiceSupplement) { var query = new QuerySet(); query.Include.Fields = new string[] { "invoice" }; var existingInvoiceSupplement = await _invoiceSupplementDataProvider.GetByIdAsync(invoiceSupplement.Id, query); if (invoiceSupplement.Id != existingInvoiceSupplement.Id) { throw new IllegalArgumentException("IllegalAttribute", "Invoice-supplement id cannot be updated."); } if (invoiceSupplement.SequenceNumber != existingInvoiceSupplement.SequenceNumber) { throw new IllegalArgumentException("IllegalAttribute", "Invoice-supplement sequence-number cannot be updated."); } await EmbedRelationsAsync(invoiceSupplement, existingInvoiceSupplement); if (invoiceSupplement.Invoice == null) { throw new IllegalArgumentException("IllegalAttribute", "Invoice is required."); } invoiceSupplement = await _invoiceSupplementDataProvider.UpdateAsync(invoiceSupplement); await _invoiceDataProvider.SyncAmountAndVatAsync(invoiceSupplement.Invoice.Id); return(invoiceSupplement); }
public async Task <InvoiceSupplement> CreateAsync(InvoiceSupplement invoiceSupplement) { var invoiceSupplementRecord = _mapper.Map <DataProvider.Models.InvoiceSupplement>(invoiceSupplement); invoiceSupplementRecord.Currency = "EUR"; invoiceSupplementRecord.SequenceNumber = 0; _context.InvoiceSupplements.Add(invoiceSupplementRecord); await _context.SaveChangesAsync(); return(_mapper.Map <InvoiceSupplement>(invoiceSupplementRecord)); }
public IEnumerable <IResource> CollectIncluded(InvoiceSupplement invoiceSupplement, IncludeQuery includeQuery) { ISet <IResource> included = new HashSet <IResource>(); // one-relations if (includeQuery.Contains("invoice") && invoiceSupplement.Invoice != null) { included.Add(_mapper.Map <InvoiceDto>(invoiceSupplement.Invoice)); } return(included); }
public async Task <InvoiceSupplement> UpdateAsync(InvoiceSupplement invoiceSupplement) { var invoiceSupplementRecord = await FindByIdAsync(invoiceSupplement.Id); _mapper.Map(invoiceSupplement, invoiceSupplementRecord); invoiceSupplementRecord.Currency = "EUR"; invoiceSupplementRecord.SequenceNumber = 0; _context.InvoiceSupplements.Update(invoiceSupplementRecord); await _context.SaveChangesAsync(); return(_mapper.Map <InvoiceSupplement>(invoiceSupplementRecord)); }
// Embed relations in invoice supplement resource: reuse old relation if there is one and it hasn't changed private async Task EmbedRelationsAsync(InvoiceSupplement invoiceSupplement, InvoiceSupplement oldInvoiceSupplement = null) { try { if (oldInvoiceSupplement != null && oldInvoiceSupplement.Invoice != null) { invoiceSupplement.Invoice = oldInvoiceSupplement.Invoice; // frontend doesn't always include the invoice in PATCH requests } else { invoiceSupplement.Invoice = await _invoiceDataProvider.GetByIdAsync(invoiceSupplement.Invoice.Id); } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }
public async Task <InvoiceSupplement> CreateAsync(InvoiceSupplement invoiceSupplement) { if (invoiceSupplement.Id != 0) { throw new IllegalArgumentException("IllegalAttribute", "Invoice-supplement cannot have an id on create."); } if (invoiceSupplement.SequenceNumber != 0) { throw new IllegalArgumentException("IllegalAttribute", "Invoice-supplement cannot have a sequence-number on create."); } if (invoiceSupplement.Invoice == null) { throw new IllegalArgumentException("IllegalAttribute", "Invoice is required on invoice supplement creation."); } await EmbedRelationsAsync(invoiceSupplement); invoiceSupplement = await _invoiceSupplementDataProvider.CreateAsync(invoiceSupplement); await _invoiceDataProvider.SyncAmountAndVatAsync(invoiceSupplement.Invoice.Id); return(invoiceSupplement); }
InvoiceSupplementRelationshipsDto ITypeConverter <InvoiceSupplement, InvoiceSupplementRelationshipsDto> .Convert(InvoiceSupplement source, InvoiceSupplementRelationshipsDto destination, ResolutionContext context) { var relationships = new InvoiceSupplementRelationshipsDto(); relationships.Invoice = GetOneRelationship <Invoice>("invoice-supplements", source.Id, "invoice", source.Invoice, context); return(relationships); }