public async Task <IActionResult> PutLoyaltyTransaction(int id, LoyaltyTransaction loyaltyTransaction) { if (id != loyaltyTransaction.Id) { return(BadRequest()); } _context.Entry(loyaltyTransaction).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!LoyaltyTransactionExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <LoyaltyTransaction> > PostLoyaltyTransaction(LoyaltyTransaction loyaltyTransaction) { if (loyaltyTransaction.CardNumber == 0) { return(BadRequest($"A card with the given number {loyaltyTransaction.CardNumber} does not exist")); } var card = await _context.LoyaltyCards.SingleOrDefaultAsync(c => c.Number == loyaltyTransaction.CardNumber); if (card is null) { return(BadRequest($"A card with the given number {loyaltyTransaction.CardNumber} does not exist")); } loyaltyTransaction.ClientId = card.ClientId; card.LoyaltyTransactions.Add(loyaltyTransaction); _context.Entry(card).State = EntityState.Modified; _context.LoyaltyBalanceTransactions.Add(loyaltyTransaction); await _context.SaveChangesAsync(); return(CreatedAtAction("PostLoyaltyTransaction", new { id = loyaltyTransaction.Id }, loyaltyTransaction)); }