public async Task <IActionResult> Post(PaymentDto payment) { if (!ModelState.IsValid) { logger.LogDebug($"BadRequest: receiver: {payment.ReceiverId}, sender: {payment.SenderId} "); return(BadRequest(ModelState)); } Customer sender = await dbContext.Customers.FindAsync(payment.SenderId); Customer receiver = await dbContext.Customers.FindAsync(payment.ReceiverId); if (sender == null) { logger.LogDebug($"Sender not found for id: {sender?.Id}"); return(BadRequest("Sender isn't registered")); } if (receiver == null) { logger.LogDebug($"Receiver not found for id: {sender?.Id}"); return(BadRequest("Receiver isn't registered")); } if (sender.Balance < payment.Amount) { logger.LogDebug($"Sender has insufficient funds id: {sender?.Id}"); return(BadRequest("Insufficient funds")); } var dbPayment = new Payment() { Amount = payment.Amount }; dbContext.AttachRange(new[] { sender, receiver }); sender.Balance -= payment.Amount; receiver.Balance += payment.Amount; sender.SendPayments.Add(dbPayment); receiver.ReceivedPayments.Add(dbPayment); await dbContext.SaveChangesAsync(); logger.LogInformation($"Payment created with id: {dbPayment.Id}"); return(Created($"payment/{dbPayment.Id}", dbPayment)); }