public async Task <IActionResult> AddOrUpdateTransaction(AddOrUpdateTransactionVm addOrUpdateTransaction) { ServiceResponse <Transaction> response = await _service.AddOrUpdateTransaction(addOrUpdateTransaction); if (response.Data == null) { return(NotFound()); } return(Ok(response)); }
// add or update transaction public async Task <ServiceResponse <Transaction> > AddOrUpdateTransaction(AddOrUpdateTransactionVm addOrUpdateTransaction) { ServiceResponse <Transaction> serviceResponse = new ServiceResponse <Transaction>(); var transaction = await _context.Transactions.FirstOrDefaultAsync(t => t.Id == addOrUpdateTransaction.Id); try { if (transaction == null) { Transaction newTransaction = new Transaction { Status = addOrUpdateTransaction.Status, Type = addOrUpdateTransaction.Type, ClientName = addOrUpdateTransaction.ClientName, Amount = addOrUpdateTransaction.Amount }; await _context.Transactions.AddAsync(newTransaction); await _context.SaveChangesAsync(); serviceResponse.Data = newTransaction; serviceResponse.Message = "You're transactions has been added"; } else { transaction.Status = addOrUpdateTransaction.Status; transaction.Type = addOrUpdateTransaction.Type; transaction.ClientName = addOrUpdateTransaction.ClientName; transaction.Amount = addOrUpdateTransaction.Amount; _context.Transactions.Update(transaction); await _context.SaveChangesAsync(); serviceResponse.Data = transaction; serviceResponse.Message = "You're transaction has been updated"; } } catch (Exception ex) { serviceResponse.Success = false; serviceResponse.Message = ex.Message; } return(serviceResponse); }