public async Task <IActionResult> PutInvoice(int id, Invoice invoice)
        {
            if (id != invoice.Id)
            {
                return(BadRequest());
            }
            RecalculateProperties(invoice);
            _context.Entry(invoice).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InvoiceExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutClient(int id, Client client)
        {
            if (id != client.Id)
            {
                return(BadRequest());
            }

            _context.Entry(client).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClientExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #3
0
        public async Task <IActionResult> ReplaceInvoiceRows(int invoiceId, IEnumerable <InvoiceRow> invoiceRow)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                var receivedInvoiceRows = invoiceRow.Where(x => x.InvoiceId == invoiceId);
                var originalInvoiceRows = _context.InvoiceRow.Where(x => x.InvoiceId == invoiceId);
                var originalIds         = originalInvoiceRows
                                          .Select(x => x.Id)
                                          .ToList();
                var newItems = receivedInvoiceRows
                               .Where(x => x.Id == default(int));
                var deleteItems = originalIds
                                  .Where(x => !receivedInvoiceRows.Select(y => y.Id).Contains(x));
                var toUpdateItems = receivedInvoiceRows.Where(x => originalIds.Contains(x.Id));

                foreach (var id in deleteItems)
                {
                    var row = await _context.InvoiceRow.FindAsync(id);

                    if (row == null)
                    {
                        return(NotFound());
                    }

                    _context.InvoiceRow.Remove(row);
                }
                foreach (var item in newItems)
                {
                    _context.InvoiceRow.Add(item);
                }
                foreach (var row in toUpdateItems)
                {
                    //var row = invoiceRow.First(x => x.Id == id);
                    _context.Entry(row).State = EntityState.Modified;
                }
                await _context.SaveChangesAsync();

                await transaction.CommitAsync();
            }
            return(Ok());
        }