public async Task <IActionResult> PutBankAccount(int id, WrapperForAPIRequest updatedAccount) { if (id != updatedAccount.person.PersonId) { return(BadRequest()); } _context.Entry(updatedAccount.person).State = EntityState.Modified; _context.Entry(updatedAccount.bankAccount).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PostBankAccount(WrapperForAPIRequest newAccount) { var exists = await _context.Persons.Where(x => x.Email == newAccount.person.Email).FirstOrDefaultAsync(); if (exists != null && exists.PersonId > 0) { //TODO: person for the given name, exists in DB //return error' return(StatusCode(StatusCodes.Status500InternalServerError, "Customer already exists in database")); } else { _context.Persons.Add(newAccount.person); newAccount.bankAccount.PersonId = newAccount.person.PersonId; newAccount.person.BankAccounts = new List <BankAccount>() { }; newAccount.person.BankAccounts.Add(newAccount.bankAccount); //_context.BankAccounts.Add(newAccount.bankAccount); await _context.SaveChangesAsync(); return(CreatedAtAction("GetBankAccount", new { id = newAccount.person.PersonId }, newAccount.person)); } }
public async Task <ActionResult <WrapperForAPIRequest> > GetBankAccount(int id) { var acct = new WrapperForAPIRequest(); acct.person = await _context.Persons .SingleAsync(x => x.PersonId == id); acct.bankAccount = await _context.BankAccounts .SingleAsync(x => x.PersonId == id); if (acct == null) { return(NotFound()); } return(acct); }