Skip to content
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
7 changes: 4 additions & 3 deletions CommBank-Server/CommBank.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>CommBank_Server</RootNamespace>
Expand All @@ -10,10 +10,11 @@

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="10.0.0" />
<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="3.5.2" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 30 additions & 0 deletions CommBank-Server/Controllers/GoalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,34 @@ public async Task<IActionResult> Delete(string id)

return NoContent();
}

[HttpPost("{id:length(24)}/icon")]
public async Task<IActionResult> UpdateIcon(string id, UpdatedIcon updatedIcon)
{
var goal = await _goalsService.GetAsync(id);

if (goal is null)
{
return NotFound();
}

goal.Icon = updatedIcon.Icon;

await _goalsService.UpdateAsync(id, goal);

return Ok(goal);
}

[HttpGet("{id:length(24)}/icon")]
public async Task<IActionResult> GetIcon(string id)
{
var goal = await _goalsService.GetAsync(id);

if (goal is null)
{
return NotFound();
}

return Ok(new { icon = goal.Icon });
}
}
3 changes: 2 additions & 1 deletion CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Goal

public double Balance { get; set; } = 0.00;

public DateTime Created { get; set; } = DateTime.Now;
public DateTime Created { get; set; } = DateTime.UtcNow;

[BsonRepresentation(BsonType.ObjectId)]
public List<string>? TransactionIds { get; set; }
Expand All @@ -27,4 +27,5 @@ public class Goal

[BsonRepresentation(BsonType.ObjectId)]
public string? UserId { get; set; }
public string? Icon { get; set; }
}
2 changes: 1 addition & 1 deletion CommBank-Server/Models/Transaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Transaction

public double Amount { get; set; } = 0.00;

public DateTime DateTime { get; set; } = DateTime.Now;
public DateTime DateTime { get; set; } = DateTime.UtcNow;

[BsonRepresentation(BsonType.ObjectId)]
public string? GoalId { get; set; }
Expand Down
86 changes: 52 additions & 34 deletions CommBank-Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,71 @@
using CommBank.Models;
using CommBank.Services;
using MongoDB.Driver;
using Newtonsoft.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);
try
{
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Secrets.json");
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("Secrets.json");

var mongoClient = new MongoClient(builder.Configuration.GetConnectionString("CommBank"));
var mongoDatabase = mongoClient.GetDatabase("CommBank");
Console.WriteLine("Attempting to connect to MongoDB...");
var mongoClient = new MongoClient(builder.Configuration.GetConnectionString("CommBank"));
// Use the actual Atlas database name as shown in the cluster
var mongoDatabase = mongoClient.GetDatabase("commbank");
Console.WriteLine("MongoDB connected to database: commbank");

IAccountsService accountsService = new AccountsService(mongoDatabase);
IAuthService authService = new AuthService(mongoDatabase);
IGoalsService goalsService = new GoalsService(mongoDatabase);
ITagsService tagsService = new TagsService(mongoDatabase);
ITransactionsService transactionsService = new TransactionsService(mongoDatabase);
IUsersService usersService = new UsersService(mongoDatabase);
IAccountsService accountsService = new AccountsService(mongoDatabase);
IAuthService authService = new AuthService(mongoDatabase);
IGoalsService goalsService = new GoalsService(mongoDatabase);
ITagsService tagsService = new TagsService(mongoDatabase);
ITransactionsService transactionsService = new TransactionsService(mongoDatabase);
IUsersService usersService = new UsersService(mongoDatabase);

builder.Services.AddSingleton(accountsService);
builder.Services.AddSingleton(authService);
builder.Services.AddSingleton(goalsService);
builder.Services.AddSingleton(tagsService);
builder.Services.AddSingleton(transactionsService);
builder.Services.AddSingleton(usersService);
builder.Services.AddSingleton(accountsService);
builder.Services.AddSingleton(authService);
builder.Services.AddSingleton(goalsService);
builder.Services.AddSingleton(tagsService);
builder.Services.AddSingleton(transactionsService);
builder.Services.AddSingleton(usersService);

builder.Services.AddCors();
builder.Services.AddCors();

var app = builder.Build();
var app = builder.Build();

app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseHttpsRedirection();
}

app.UseHttpsRedirection();
app.UseAuthorization();

app.UseAuthorization();
app.MapControllers();

app.MapControllers();
// Add a simple health check endpoint
app.MapGet("/health", () => "OK");

app.Run();
app.Run();
}
catch (Exception ex)
{
Console.WriteLine($"Fatal error: {ex}");
throw;
}

2 changes: 1 addition & 1 deletion CommBank-Server/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"profiles": {
"CommBank_Server": {
"commandName": "Project",
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:11366;http://localhost:5203",
"environmentVariables": {
Expand Down
8 changes: 5 additions & 3 deletions CommBank-Server/Secrets.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{

{
"ConnectionStrings": {
"CommBank": "{CONNECTION_STRING}"
"CommBank": "mongodb+sv://username:password@cluster0.jja3hhp.mongodb.net/?appName=Cluster0"
}
}
}

3 changes: 2 additions & 1 deletion CommBank.Tests/CommBank.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MongoDB.Driver" Version="3.5.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
27 changes: 25 additions & 2 deletions CommBank.Tests/GoalControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,32 @@ public async void Get()
public async void GetForUser()
{
// Arrange

var goals = collections.GetGoals();
var users = collections.GetUsers();

// Ensure goals have a UserId to filter/assert against
var expectedUserId = users[0].Id!;
foreach (var g in goals)
{
g.UserId = expectedUserId;
}

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(expectedUserId);

// Assert
Assert.NotNull(result);

foreach (Goal goal in result!)
{
Assert.IsAssignableFrom<Goal>(goal);
Assert.Equal(expectedUserId, goal.UserId);
}
}
}
21 changes: 21 additions & 0 deletions Connection/Connect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

using MongoDB.Driver;
using MongoDB.Bson;

const string connectionUri = "mongodb+srv://username:password@cluster0.jja3hhp.mongodb.net/?appName=Cluster0";

var settings = MongoClientSettings.FromConnectionString(connectionUri);

// Set the ServerApi field of the settings object to set the version of the Stable API on the client
settings.ServerApi = new ServerApi(ServerApiVersion.V1);

// Create a new client and connect to the server
var client = new MongoClient(settings);

// Send a ping to confirm a successful connection
try {
var result = client.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
} catch (Exception ex) {
Console.WriteLine(ex);
}
56 changes: 56 additions & 0 deletions http-test.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
### Health Check
GET http://localhost:11366/health

### Get All Goals (should be empty initially)
GET http://localhost:11366/api/Goal
Accept: application/json

### Get All Accounts
GET http://localhost:11366/api/Account
Accept: application/json

### Get All Users
GET http://localhost:11366/api/User
Accept: application/json

### Get All Transactions
GET http://localhost:11366/api/Transaction
Accept: application/json

### Get All Tags
GET http://localhost:11366/api/Tag
Accept: application/json

### Create a new User
POST http://localhost:11366/api/User
Content-Type: application/json

{
"name": "Test User",
"email": "test@example.com",
"password": "TestPassword123"
}

### Create a new Goal (you'll need a valid userId from the step above)
POST http://localhost:11366/api/Goal
Content-Type: application/json

{
"name": "Save Money",
"targetAmount": 5000,
"currentAmount": 0,
"userId": "696772c9be66d4f22d7e4d77"
}

### adding icon to Goal (you'll need a valid goalId from the step above)
POST http://localhost:11366/api/Goal/696772f9be66d4f22d7e4d78/icon
Content-Type: application/json

{
"icon": "😡"
}

###
### Get All Goals (should be empty initially)
GET http://localhost:11366/api/Goal/696772f9be66d4f22d7e4d78/icon
Accept: application/json
Loading