From 1e4fd1374e6afe2420aa8e7414a8a46fbe533497 Mon Sep 17 00:00:00 2001 From: Rongguang Wang Date: Fri, 12 Jul 2024 04:10:08 +0930 Subject: [PATCH 1/2] Added backend support for icons --- CommBank-Server/CommBank.csproj | 2 +- CommBank-Server/Controllers/GoalController.cs | 16 ++- CommBank-Server/Models/Goal.cs | 2 + CommBank-Server/Secrets.json | 4 +- CommBank-Server/Services/GoalService.cs | 7 +- CommBank-Server/Services/IGoalsService.cs | 7 +- CommBank-Server/appsettings.json | 1 - CommBank.Tests/CommBank.Tests.csproj | 7 +- CommBank.Tests/Fake/FakeGoalsService.cs | 121 ++++++++++++++---- CommBank.Tests/GoalControllerTests.cs | 104 ++++++++++++++- 10 files changed, 223 insertions(+), 48 deletions(-) diff --git a/CommBank-Server/CommBank.csproj b/CommBank-Server/CommBank.csproj index 983cc88..3b22cf1 100644 --- a/CommBank-Server/CommBank.csproj +++ b/CommBank-Server/CommBank.csproj @@ -13,7 +13,7 @@ - + diff --git a/CommBank-Server/Controllers/GoalController.cs b/CommBank-Server/Controllers/GoalController.cs index 98271a5..f17cd63 100644 --- a/CommBank-Server/Controllers/GoalController.cs +++ b/CommBank-Server/Controllers/GoalController.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using CommBank.Services; using CommBank.Models; +using MongoDB.Driver; namespace CommBank.Controllers; @@ -69,7 +70,7 @@ public async Task Post(Goal newGoal) } [HttpPut("{id:length(24)}")] - public async Task Update(string id, Goal updatedGoal) + public async Task UpdateIcon(string id, [FromBody] UpdateIconRequest request) { var goal = await _goalsService.GetAsync(id); @@ -78,9 +79,9 @@ public async Task Update(string id, Goal updatedGoal) return NotFound(); } - updatedGoal.Id = goal.Id; - - await _goalsService.UpdateAsync(id, updatedGoal); + // Build the update definition to only update the icon field + var updateDefinition = Builders.Update.Set(g => g.Icon, request.Icon); + await _goalsService.UpdateAsync(id, updateDefinition); return NoContent(); } @@ -99,4 +100,9 @@ public async Task Delete(string id) return NoContent(); } -} \ No newline at end of file +} + +public class UpdateIconRequest +{ + public string? Icon { get; set; } +} diff --git a/CommBank-Server/Models/Goal.cs b/CommBank-Server/Models/Goal.cs index 77ff1ad..20b9b5d 100644 --- a/CommBank-Server/Models/Goal.cs +++ b/CommBank-Server/Models/Goal.cs @@ -27,4 +27,6 @@ public class Goal [BsonRepresentation(BsonType.ObjectId)] public string? UserId { get; set; } + + public string? Icon { get; set; } } \ No newline at end of file diff --git a/CommBank-Server/Secrets.json b/CommBank-Server/Secrets.json index 0e5bf94..111b1fe 100644 --- a/CommBank-Server/Secrets.json +++ b/CommBank-Server/Secrets.json @@ -1,5 +1,5 @@ { "ConnectionStrings": { - "CommBank": "{CONNECTION_STRING}" + "CommBank": "mongodb+srv://cbuser:LKygPmL6h7HJms5F@cluster0.ipqxbpk.mongodb.net/CommBank?retryWrites=true&w=majority&appName=Cluster0" } -} \ No newline at end of file +} diff --git a/CommBank-Server/Services/GoalService.cs b/CommBank-Server/Services/GoalService.cs index b0c600a..a70c402 100644 --- a/CommBank-Server/Services/GoalService.cs +++ b/CommBank-Server/Services/GoalService.cs @@ -24,9 +24,10 @@ public async Task> GetAsync() => public async Task CreateAsync(Goal newGoal) => await _goalsCollection.InsertOneAsync(newGoal); - public async Task UpdateAsync(string id, Goal updatedGoal) => - await _goalsCollection.ReplaceOneAsync(x => x.Id == id, updatedGoal); + // Updated method to support partial updates + public async Task UpdateAsync(string id, UpdateDefinition updateDefinition) => + await _goalsCollection.UpdateOneAsync(x => x.Id == id, updateDefinition); public async Task RemoveAsync(string id) => await _goalsCollection.DeleteOneAsync(x => x.Id == id); -} \ No newline at end of file +} diff --git a/CommBank-Server/Services/IGoalsService.cs b/CommBank-Server/Services/IGoalsService.cs index 50ee8e0..ec3c23a 100644 --- a/CommBank-Server/Services/IGoalsService.cs +++ b/CommBank-Server/Services/IGoalsService.cs @@ -1,4 +1,5 @@ using CommBank.Models; +using MongoDB.Driver; namespace CommBank.Services { @@ -9,6 +10,8 @@ public interface IGoalsService Task?> GetForUserAsync(string id); Task GetAsync(string id); Task RemoveAsync(string id); - Task UpdateAsync(string id, Goal updatedGoal); + + // Updated method signature to support partial updates + Task UpdateAsync(string id, UpdateDefinition updateDefinition); } -} \ No newline at end of file +} diff --git a/CommBank-Server/appsettings.json b/CommBank-Server/appsettings.json index af0538f..2230277 100644 --- a/CommBank-Server/appsettings.json +++ b/CommBank-Server/appsettings.json @@ -7,4 +7,3 @@ }, "AllowedHosts": "*" } - diff --git a/CommBank.Tests/CommBank.Tests.csproj b/CommBank.Tests/CommBank.Tests.csproj index 4d9413f..a4162a1 100644 --- a/CommBank.Tests/CommBank.Tests.csproj +++ b/CommBank.Tests/CommBank.Tests.csproj @@ -9,9 +9,10 @@ - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/CommBank.Tests/Fake/FakeGoalsService.cs b/CommBank.Tests/Fake/FakeGoalsService.cs index 643a27e..cb0df65 100644 --- a/CommBank.Tests/Fake/FakeGoalsService.cs +++ b/CommBank.Tests/Fake/FakeGoalsService.cs @@ -1,35 +1,108 @@ -using Microsoft.Extensions.Options; +// using Microsoft.Extensions.Options; +// using CommBank.Models; +// using CommBank.Services; + +// namespace CommBank.Tests.Fake; + +// public class FakeGoalsService : IGoalsService +// { +// List _goals; +// Goal _goal; + +// public FakeGoalsService(List goals, Goal goal) +// { +// _goals = goals; +// _goal = goal; +// } + +// public async Task> GetAsync() => +// await Task.FromResult(_goals); + +// public async Task?> GetForUserAsync(string id) => +// await Task.FromResult(_goals); + +// public async Task GetAsync(string id) => +// await Task.FromResult(_goal); + +// public async Task CreateAsync(Goal newGoal) => +// await Task.FromResult(true); + +// public async Task UpdateAsync(string id, Goal updatedGoal) => +// await Task.FromResult(true); + +// public async Task RemoveAsync(string id) => +// await Task.FromResult(true); +// } + +using Microsoft.Extensions.Options; using CommBank.Models; using CommBank.Services; +using MongoDB.Driver; +using System.Collections.Generic; +using System.Threading.Tasks; -namespace CommBank.Tests.Fake; - -public class FakeGoalsService : IGoalsService +namespace CommBank.Tests.Fake { - List _goals; - Goal _goal; - - public FakeGoalsService(List goals, Goal goal) + public class FakeGoalsService : IGoalsService { - _goals = goals; - _goal = goal; - } + private List _goals; + private Goal _goal; + + public FakeGoalsService(List goals, Goal goal) + { + _goals = goals; + _goal = goal; + } - public async Task> GetAsync() => - await Task.FromResult(_goals); + public async Task> GetAsync() => + await Task.FromResult(_goals); - public async Task?> GetForUserAsync(string id) => - await Task.FromResult(_goals); + public async Task?> GetForUserAsync(string id) => + await Task.FromResult(_goals); - public async Task GetAsync(string id) => - await Task.FromResult(_goal); + public async Task GetAsync(string id) => + await Task.FromResult(_goal); - public async Task CreateAsync(Goal newGoal) => - await Task.FromResult(true); + public async Task CreateAsync(Goal newGoal) + { + _goals.Add(newGoal); + await Task.CompletedTask; + } - public async Task UpdateAsync(string id, Goal updatedGoal) => - await Task.FromResult(true); + // Update a goal with the given id using the provided update definition + public async Task UpdateAsync(string id, UpdateDefinition updateDefinition) + { + var goal = _goals.Find(g => g.Id == id); + if (goal != null && updateDefinition != null) + { + // Simulate the update operation + // Assume updateDefinition is a Dictionary for simplicity + var updates = new Dictionary + { + { "Name", "Updated Goal Name" }, + // Add other fields as needed + }; - public async Task RemoveAsync(string id) => - await Task.FromResult(true); -} \ No newline at end of file + foreach (var update in updates) + { + if (update.Key == "Name" && update.Value is string newName) + { + goal.Name = newName; + } + // Handle other fields similarly + } + } + await Task.CompletedTask; + } + + public async Task RemoveAsync(string id) + { + var goal = _goals.Find(g => g.Id == id); + if (goal != null) + { + _goals.Remove(goal); + } + await Task.CompletedTask; + } + } +} diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index 8380181..b9daa0e 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -1,4 +1,79 @@ -using CommBank.Controllers; +// using CommBank.Controllers; +// using CommBank.Services; +// using CommBank.Models; +// using CommBank.Tests.Fake; +// using Microsoft.AspNetCore.Mvc; + +// namespace CommBank.Tests; + +// public class GoalControllerTests +// { +// private readonly FakeCollections collections; + +// public GoalControllerTests() +// { +// collections = new(); +// } + +// [Fact] +// public async void GetAll() +// { +// // Arrange +// var goals = collections.GetGoals(); +// var users = collections.GetUsers(); +// IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); +// IUsersService usersService = new FakeUsersService(users, users[0]); +// GoalController controller = new(goalsService, usersService); + +// // Act +// var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); +// controller.ControllerContext.HttpContext = httpContext; +// var result = await controller.Get(); + +// // Assert +// var index = 0; +// foreach (Goal goal in result) +// { +// Assert.IsAssignableFrom(goal); +// Assert.Equal(goals[index].Id, goal.Id); +// Assert.Equal(goals[index].Name, goal.Name); +// index++; +// } +// } + +// [Fact] +// public async void Get() +// { +// // Arrange +// var goals = collections.GetGoals(); +// var users = collections.GetUsers(); +// IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); +// IUsersService usersService = new FakeUsersService(users, users[0]); +// GoalController controller = new(goalsService, usersService); + +// // Act +// var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); +// controller.ControllerContext.HttpContext = httpContext; +// var result = await controller.Get(goals[0].Id!); + +// // Assert +// Assert.IsAssignableFrom(result.Value); +// Assert.Equal(goals[0], result.Value); +// Assert.NotEqual(goals[1], result.Value); +// } + +// [Fact] +// public async void GetForUser() +// { +// // Arrange + +// // Act + +// // Assert +// } +// } + +using CommBank.Controllers; using CommBank.Services; using CommBank.Models; using CommBank.Tests.Fake; @@ -16,7 +91,7 @@ public GoalControllerTests() } [Fact] - public async void GetAll() + public async Task GetAll() // Changed async void to async Task { // Arrange var goals = collections.GetGoals(); @@ -42,7 +117,7 @@ public async void GetAll() } [Fact] - public async void Get() + public async Task Get() // Changed async void to async Task { // Arrange var goals = collections.GetGoals(); @@ -63,12 +138,27 @@ public async void Get() } [Fact] - public async void GetForUser() + public async Task GetForUser() // Changed async void to async Task { // Arrange - + var goals = collections.GetGoals(); + var users = collections.GetUsers(); + IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); + IUsersService usersService = new FakeUsersService(users, users[0]); + GoalController controller = new(goalsService, usersService); + // Act - + var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); + controller.ControllerContext.HttpContext = httpContext; + var result = await controller.GetForUser(goals[0].UserId!); + // Assert + Assert.NotNull(result); // Ensure the result is not null + + foreach (Goal goal in result!) + { + Assert.IsAssignableFrom(goal); // Ensure each item is of type Goal + Assert.Equal(goals[0].UserId, goal.UserId); // Ensure each goal has the expected UserId + } } -} \ No newline at end of file +} From ebe5ad2918d59f3310b241f7f1a7dd4a5bf3f63f Mon Sep 17 00:00:00 2001 From: Rongguang Wang Date: Fri, 12 Jul 2024 04:28:36 +0930 Subject: [PATCH 2/2] Added backend support for icons --- CommBank.Tests/Fake/FakeGoalsService.cs | 38 +----------- CommBank.Tests/GoalControllerTests.cs | 77 +------------------------ 2 files changed, 2 insertions(+), 113 deletions(-) diff --git a/CommBank.Tests/Fake/FakeGoalsService.cs b/CommBank.Tests/Fake/FakeGoalsService.cs index cb0df65..fc7f669 100644 --- a/CommBank.Tests/Fake/FakeGoalsService.cs +++ b/CommBank.Tests/Fake/FakeGoalsService.cs @@ -1,40 +1,4 @@ -// using Microsoft.Extensions.Options; -// using CommBank.Models; -// using CommBank.Services; - -// namespace CommBank.Tests.Fake; - -// public class FakeGoalsService : IGoalsService -// { -// List _goals; -// Goal _goal; - -// public FakeGoalsService(List goals, Goal goal) -// { -// _goals = goals; -// _goal = goal; -// } - -// public async Task> GetAsync() => -// await Task.FromResult(_goals); - -// public async Task?> GetForUserAsync(string id) => -// await Task.FromResult(_goals); - -// public async Task GetAsync(string id) => -// await Task.FromResult(_goal); - -// public async Task CreateAsync(Goal newGoal) => -// await Task.FromResult(true); - -// public async Task UpdateAsync(string id, Goal updatedGoal) => -// await Task.FromResult(true); - -// public async Task RemoveAsync(string id) => -// await Task.FromResult(true); -// } - -using Microsoft.Extensions.Options; +using Microsoft.Extensions.Options; using CommBank.Models; using CommBank.Services; using MongoDB.Driver; diff --git a/CommBank.Tests/GoalControllerTests.cs b/CommBank.Tests/GoalControllerTests.cs index b9daa0e..73bcf1b 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -1,79 +1,4 @@ -// using CommBank.Controllers; -// using CommBank.Services; -// using CommBank.Models; -// using CommBank.Tests.Fake; -// using Microsoft.AspNetCore.Mvc; - -// namespace CommBank.Tests; - -// public class GoalControllerTests -// { -// private readonly FakeCollections collections; - -// public GoalControllerTests() -// { -// collections = new(); -// } - -// [Fact] -// public async void GetAll() -// { -// // Arrange -// var goals = collections.GetGoals(); -// var users = collections.GetUsers(); -// IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); -// IUsersService usersService = new FakeUsersService(users, users[0]); -// GoalController controller = new(goalsService, usersService); - -// // Act -// var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); -// controller.ControllerContext.HttpContext = httpContext; -// var result = await controller.Get(); - -// // Assert -// var index = 0; -// foreach (Goal goal in result) -// { -// Assert.IsAssignableFrom(goal); -// Assert.Equal(goals[index].Id, goal.Id); -// Assert.Equal(goals[index].Name, goal.Name); -// index++; -// } -// } - -// [Fact] -// public async void Get() -// { -// // Arrange -// var goals = collections.GetGoals(); -// var users = collections.GetUsers(); -// IGoalsService goalsService = new FakeGoalsService(goals, goals[0]); -// IUsersService usersService = new FakeUsersService(users, users[0]); -// GoalController controller = new(goalsService, usersService); - -// // Act -// var httpContext = new Microsoft.AspNetCore.Http.DefaultHttpContext(); -// controller.ControllerContext.HttpContext = httpContext; -// var result = await controller.Get(goals[0].Id!); - -// // Assert -// Assert.IsAssignableFrom(result.Value); -// Assert.Equal(goals[0], result.Value); -// Assert.NotEqual(goals[1], result.Value); -// } - -// [Fact] -// public async void GetForUser() -// { -// // Arrange - -// // Act - -// // Assert -// } -// } - -using CommBank.Controllers; +using CommBank.Controllers; using CommBank.Services; using CommBank.Models; using CommBank.Tests.Fake;