public void UpdateClientAccountTest() { using (var context = new CarRentalDbContext(this.dbContextOptions)) { var service = new ClientAccountService(context); ClientAccountModificationParams parameters = new ClientAccountModificationParams { ClientId = 1, Email = "*****@*****.**", FullName = "Update Client", Phone = "+12345", }; var clientAccountModel = service.Update(parameters); Assert.AreEqual(parameters.Email, clientAccountModel.Email); Assert.AreEqual(parameters.Phone, clientAccountModel.Phone); Assert.AreEqual(parameters.FullName, clientAccountModel.FullName); Assert.AreEqual(parameters.ClientId, clientAccountModel.ClientId); try { service.Update(null); Assert.Fail(); } catch (InvalidParameterException) { } catch { Assert.Fail(); } try { service.Update(new ClientAccountModificationParams() { ClientId = 1000, Email = "*****@*****.**", FullName = "Update Client", Phone = "+12345", }); Assert.Fail(); } catch (NotFoundException) { } catch { Assert.Fail(); } } }
/// <summary> /// Updates a client account. /// </summary> /// <param name="parameters">Client account modification parameters. Contains all the client account information as well as the client identifier.</param> /// <returns>Client account model.</returns> public ClientAccountModel Update(ClientAccountModificationParams parameters) { this.ValidateParameters(parameters); var dbClientAccount = this.dbContext.ClientAccounts.SingleOrDefault(x => x.ClientId == parameters.ClientId); if (dbClientAccount == null) { throw new NotFoundException("Client account not found."); } dbClientAccount.Email = parameters.Email; dbClientAccount.FullName = parameters.FullName; dbClientAccount.Phone = parameters.Phone; this.dbContext.SaveChanges(); return(dbClientAccount.ToModel()); }