public IHttpActionResult AddTransaction(Guid id, [FromBody] Transaction transaction) { using (RefractorContext entities = new RefractorContext()) { using (DbContextTransaction dbtransaction = entities.Database.BeginTransaction()) { try { transaction.Id = Guid.NewGuid(); transaction.AccountId = id; transaction.Date = DateTime.Now; entities.Transactions.Add(transaction); var clientAccount = entities.Accounts.FirstOrDefault(account => account.Id == id); if (clientAccount == null) { return(Content(HttpStatusCode.NotFound, "Account ID not found")); } clientAccount.Amount = clientAccount.Amount + transaction.Amount; entities.SaveChanges(); dbtransaction.Commit(); return(Content(HttpStatusCode.OK, clientAccount)); } catch (Exception ex) { dbtransaction.Rollback(); return(Content(HttpStatusCode.BadRequest, ex.Message)); } } } }
public IHttpActionResult Update(Guid id, [FromBody] Account account) { try { using (RefractorContext entities = new RefractorContext()) { var existingAccount = entities.Accounts.FirstOrDefault(acc => acc.Id == id); if (existingAccount == null) { return(Content(HttpStatusCode.NotFound, "Account with ID: " + id.ToString() + " could not be found to update")); } else { existingAccount.Name = account.Name; existingAccount.Amount = account.Amount; existingAccount.Number = account.Number; entities.SaveChanges(); return(Content(HttpStatusCode.OK, existingAccount)); } } } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex.Message)); } }
public IHttpActionResult Delete(Guid id) { try { using (RefractorContext entities = new RefractorContext()) { var existingAccount = entities.Accounts.FirstOrDefault(acc => acc.Id == id); if (existingAccount == null) { return(Content(HttpStatusCode.NotFound, "Account with ID: " + id.ToString() + " could not be found to delete")); } else { entities.Accounts.Remove(existingAccount); entities.SaveChanges(); return(Content(HttpStatusCode.OK, "Account with ID: " + id.ToString() + " has been deleted")); } } } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex.Message)); } }
public IHttpActionResult Add([FromBody] Account account) { try { using (RefractorContext entities = new RefractorContext()) { { account.Id = Guid.NewGuid(); entities.Accounts.Add(account); entities.SaveChanges(); return(Content(HttpStatusCode.Created, account)); } } } catch (Exception ex) { return(Content(HttpStatusCode.BadRequest, ex.Message)); } }