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..fc7f669 100644 --- a/CommBank.Tests/Fake/FakeGoalsService.cs +++ b/CommBank.Tests/Fake/FakeGoalsService.cs @@ -1,35 +1,72 @@ 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 async Task> GetAsync() => - await Task.FromResult(_goals); + public FakeGoalsService(List goals, Goal goal) + { + _goals = goals; + _goal = goal; + } - public async Task?> GetForUserAsync(string id) => - await Task.FromResult(_goals); + public async Task> GetAsync() => + await Task.FromResult(_goals); - public async Task GetAsync(string id) => - await Task.FromResult(_goal); + public async Task?> GetForUserAsync(string id) => + await Task.FromResult(_goals); - public async Task CreateAsync(Goal newGoal) => - await Task.FromResult(true); + public async Task GetAsync(string id) => + await Task.FromResult(_goal); - public async Task UpdateAsync(string id, Goal updatedGoal) => - await Task.FromResult(true); + public async Task CreateAsync(Goal newGoal) + { + _goals.Add(newGoal); + await Task.CompletedTask; + } - public async Task RemoveAsync(string id) => - await Task.FromResult(true); -} \ No newline at end of file + // 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 + }; + + 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..73bcf1b 100644 --- a/CommBank.Tests/GoalControllerTests.cs +++ b/CommBank.Tests/GoalControllerTests.cs @@ -16,7 +16,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 +42,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 +63,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 +}