public IActionResult UpdateSubAccount(int accountId, int id, [FromBody] SubAccountForEditingDto editedSubAccount) { var account = AccountsDataStore.Current.Accounts .FirstOrDefault(a => a.Id == accountId); if (account == null) { return(NotFound()); } var subAccount = account.SubAccounts.FirstOrDefault(sa => sa.Id == id); if (subAccount == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } subAccount.Name = editedSubAccount.Name; subAccount.Description = editedSubAccount.Description; return(NoContent()); }
public IActionResult CreateSubAccount(int accountId, [FromBody] SubAccountForEditingDto newSubAccount) { var accountFromStore = AccountsDataStore.Current.Accounts .FirstOrDefault(sa => sa.Id == accountId); if (accountFromStore == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //ToDo Change var maxId = AccountsDataStore.Current.Accounts .SelectMany(a => a.SubAccounts) .Max(sa => sa.Id); var finishedSubAccount = new SubAccountDto() { Id = ++maxId, Name = newSubAccount.Name, Description = newSubAccount.Description }; accountFromStore.SubAccounts.Add(finishedSubAccount); return(CreatedAtRoute("GetSubAccount", new { accountId = accountId, id = finishedSubAccount.Id }, finishedSubAccount)); }