Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add backend support for icons #52

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CommBank-Server/CommBank.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.1" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="MongoDB.Driver" Version="2.27.0" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 11 additions & 5 deletions CommBank-Server/Controllers/GoalController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using CommBank.Services;
using CommBank.Models;
using MongoDB.Driver;

namespace CommBank.Controllers;

Expand Down Expand Up @@ -69,7 +70,7 @@ public async Task<IActionResult> Post(Goal newGoal)
}

[HttpPut("{id:length(24)}")]
public async Task<IActionResult> Update(string id, Goal updatedGoal)
public async Task<IActionResult> UpdateIcon(string id, [FromBody] UpdateIconRequest request)
{
var goal = await _goalsService.GetAsync(id);

Expand All @@ -78,9 +79,9 @@ public async Task<IActionResult> 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<Goal>.Update.Set(g => g.Icon, request.Icon);
await _goalsService.UpdateAsync(id, updateDefinition);

return NoContent();
}
Expand All @@ -99,4 +100,9 @@ public async Task<IActionResult> Delete(string id)

return NoContent();
}
}
}

public class UpdateIconRequest
{
public string? Icon { get; set; }
}
2 changes: 2 additions & 0 deletions CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class Goal

[BsonRepresentation(BsonType.ObjectId)]
public string? UserId { get; set; }

public string? Icon { get; set; }
}
4 changes: 2 additions & 2 deletions CommBank-Server/Secrets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"ConnectionStrings": {
"CommBank": "{CONNECTION_STRING}"
"CommBank": "mongodb+srv://cbuser:[email protected]/CommBank?retryWrites=true&w=majority&appName=Cluster0"
}
}
}
7 changes: 4 additions & 3 deletions CommBank-Server/Services/GoalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public async Task<List<Goal>> 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<Goal> updateDefinition) =>
await _goalsCollection.UpdateOneAsync(x => x.Id == id, updateDefinition);

public async Task RemoveAsync(string id) =>
await _goalsCollection.DeleteOneAsync(x => x.Id == id);
}
}
7 changes: 5 additions & 2 deletions CommBank-Server/Services/IGoalsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CommBank.Models;
using MongoDB.Driver;

namespace CommBank.Services
{
Expand All @@ -9,6 +10,8 @@ public interface IGoalsService
Task<List<Goal>?> GetForUserAsync(string id);
Task<Goal?> 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<Goal> updateDefinition);
}
}
}
1 change: 0 additions & 1 deletion CommBank-Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
},
"AllowedHosts": "*"
}

7 changes: 4 additions & 3 deletions CommBank.Tests/CommBank.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MongoDB.Driver" Version="2.27.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
83 changes: 60 additions & 23 deletions CommBank.Tests/Fake/FakeGoalsService.cs
Original file line number Diff line number Diff line change
@@ -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<Goal> _goals;
Goal _goal;

public FakeGoalsService(List<Goal> goals, Goal goal)
public class FakeGoalsService : IGoalsService
{
_goals = goals;
_goal = goal;
}
private List<Goal> _goals;
private Goal _goal;

public async Task<List<Goal>> GetAsync() =>
await Task.FromResult(_goals);
public FakeGoalsService(List<Goal> goals, Goal goal)
{
_goals = goals;
_goal = goal;
}

public async Task<List<Goal>?> GetForUserAsync(string id) =>
await Task.FromResult(_goals);
public async Task<List<Goal>> GetAsync() =>
await Task.FromResult(_goals);

public async Task<Goal?> GetAsync(string id) =>
await Task.FromResult(_goal);
public async Task<List<Goal>?> GetForUserAsync(string id) =>
await Task.FromResult(_goals);

public async Task CreateAsync(Goal newGoal) =>
await Task.FromResult(true);
public async Task<Goal?> 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);
}
// Update a goal with the given id using the provided update definition
public async Task UpdateAsync(string id, UpdateDefinition<Goal> 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<string, object>
{
{ "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;
}
}
}
27 changes: 21 additions & 6 deletions CommBank.Tests/GoalControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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>(goal); // Ensure each item is of type Goal
Assert.Equal(goals[0].UserId, goal.UserId); // Ensure each goal has the expected UserId
}
}
}
}