public async Task <IActionResult> EditAccountAsync([FromBody] UserAccountResource userAccountResource) { // Verify email address has valid structure string normalizedAddress; if (!EmailExtension.TryNormalizeEmail(userAccountResource.Email, out normalizedAddress)) { return(BadRequest("Not a valid email address!")); } // How does this validate against the UserAccount model? if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var tokenOwnerEmail = User.FindFirst("Email").Value; // Retrieve ownerId account from database var account = await unitOfWork.UserAccountRepository .GetUserAccountAsync(tokenOwnerEmail); // No account matches ownerId if (account == null || account.IsDeleted) { return(NotFound("No account found matching that ownerId.")); } // Verify email is unchanged if (account.Email != normalizedAddress.Trim().ToLower()) { return(BadRequest("Cannot change email address.")); } // See UserTask PUT for issue of mapping back to UserAccountResource // Map account resource to model account.FirstName = userAccountResource.FirstName; account.LastName = userAccountResource.LastName; if (userAccountResource.IsDeleted) { await unitOfWork.UserAccountRepository.DeleteAccount(account.Email); } // Save updated account to database Task.WaitAll(unitOfWork.CompleteAsync()); // Return mapped resource return(Ok(_mapper.Map <UserAccountResource>(account))); }
public async void GetUserMetricsTestAsync() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { IUnitOfWork unitOfWork; TasksController taskController; AccountsController accountController; DateTime startDate; var options = new DbContextOptionsBuilder <SynchroLeanDbContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new SynchroLeanDbContext(options)) { context.Database.EnsureCreated(); } // Create a newUserTask to send to HTTPPost method (without dates) var newUserTask1 = new UserTaskResource { Name = "User metrics", Description = "Modify metrics endpoints to accept DateTimes", Weekdays = 40, CreationDate = DateTime.Now, IsCompleted = false, IsDeleted = false, OwnerEmail = "*****@*****.**" }; // Create a newUserTask to send to HTTPPost method (without dates) var newUserTask2 = new UserTaskResource { Name = "User metrics", Description = "complete this to have 50% completion rate", Weekdays = 0, CreationDate = DateTime.Now, IsCompleted = false, IsDeleted = false, OwnerEmail = "*****@*****.**" }; var account = new UserAccountResource { FirstName = "Cole", LastName = "Phares", Email = "*****@*****.**", IsDeleted = false }; // Run the test against one instance of the context using (var context = new SynchroLeanDbContext(options)) { // Bind the context to the UnitOfWork object unitOfWork = new UnitOfWork(context); // Create an instance of TasksController taskController = new TasksController(unitOfWork, mapper); // Create an instance of AccountsController accountController = new AccountsController(unitOfWork, mapper); var newAccount = new CreateUserAccountResource { Email = account.Email, FirstName = account.FirstName, LastName = account.LastName, Password = "******", IsDeleted = false }; await accountController.AddUserAccountAsync(newAccount); // Add newUserTask to UserTasks table in Db asynchronously await taskController.AddUserTaskAsync(newUserTask1); await taskController.AddUserTaskAsync(newUserTask2); startDate = DateTime.Now; await taskController.EditUserTaskAsync(2, new UserTaskResource { Name = "User metrics", Description = "complete this to have 50% completion rate", Weekdays = 0, IsCompleted = true, IsDeleted = false, } ); } // Use a separate instance of the context to verify correct data was saved to database using (var context = new SynchroLeanDbContext(options)) { // Bind the Db context to the UnitOfWork object unitOfWork = new UnitOfWork(context); // Create new instance of task controller taskController = new TasksController(unitOfWork, mapper); DateTime endDate = DateTime.Now; // Retrieve the task from the Db asynchronously var completionRateResult = taskController.GetUserCompletionRate("*****@*****.**", startDate, endDate); var actionResult = completionRateResult.Result; var okObjectResult = actionResult as OkObjectResult; var completionRate = okObjectResult.Value as double?; Assert.True(completionRate.Equals(.5)); } } finally { connection.Close(); } }