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
3 changes: 3 additions & 0 deletions CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class Goal
[BsonRepresentation(BsonType.ObjectId)]
public List<string>? TagIds { get; set; }

[BsonElement("icon")]
public string? Icon { get; set; }

[BsonRepresentation(BsonType.ObjectId)]
public string? UserId { get; set; }
}
2 changes: 1 addition & 1 deletion 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://dhanushraju24_db_user:Dhanu2012@m0.utwvbze.mongodb.net/?appName=M0"
}
}
2 changes: 1 addition & 1 deletion CommBank-Server/Services/GoalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class GoalsService : IGoalsService

public GoalsService(IMongoDatabase mongoDatabase)
{
_goalsCollection = mongoDatabase.GetCollection<Goal>("Goals");
_goalsCollection = mongoDatabase.GetCollection<Goal>("goals");
}

public async Task<List<Goal>> GetAsync() =>
Expand Down
6 changes: 6 additions & 0 deletions CommBank-Server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 26 additions & 7 deletions CommBank.Tests/GoalControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,32 @@ public async void Get()
Assert.NotEqual(goals[1], result.Value);
}

[Fact]
public async void GetForUser()


[Fact]
public async void GetForUser()
{
// 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(users[0].Id!);

// Assert
Assert.NotNull(result);

foreach (Goal goal in result!)
{
// Arrange

// Act

// Assert
Assert.IsAssignableFrom<Goal>(goal);
}
}
}
259 changes: 259 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
# Commonwealth Bank Forage – Task 1

## Objective

The task was to extend the Goal model by adding a new property called **Icon** and ensure that the API returns this field when goal data is requested.

---

# Step 1: Understand the Existing Architecture

The project follows a typical ASP.NET Core + MongoDB architecture:

MongoDB Database
Services Layer
Controllers
API Endpoints
Postman / Swagger

When a request is sent to:

GET /api/Goal

the flow is:

GoalController
GoalsService
MongoDB Collection (Goals)
Return JSON Response

---

# Step 2: Setup MongoDB Atlas

### Database

CommBank

### Collections

- accounts
- goals
- tags
- transactions
- users

Imported:

- Accounts.json
- Goals.json
- Tags.json
- Transactions.json
- Users.json

---

# Step 3: Connect Backend to MongoDB

In `Program.cs`:

```csharp
var mongoClient =
new MongoClient(
builder.Configuration.GetConnectionString("CommBank"));

var mongoDatabase =
mongoClient.GetDatabase("CommBank");
```
This creates a connection to MongoDB Atlas and accesses the CommBank database.

---

# Step 4: Verify Existing API

Using Swagger and Postman:

```http
GET /api/Goal
```

Initially, goal objects did not contain an `icon` field.

---

# Step 5: Extend the Goal Model

File:

`Models/Goal.cs`

Added:

```csharp
[BsonElement("icon")]
public string? Icon { get; set; }
```

---

# Why This Was Needed

MongoDB documents contained an `icon` field, but the Goal model did not.

Without the property:

```csharp
public string? Icon { get; set; }
```

MongoDB could not map the field and threw:

```text
Element 'icon' does not match any field or property of class CommBank.Models.Goal
```

Adding the property fixed the issue.

---

# Step 6: Add Icon Values to MongoDB

Examples:

```json
{
"Name": "House Down Payment",
"icon": "home"
}
```

```json
{
"Name": "Tesla Model Y",
"icon": "car"
}
```

```json
{
"Name": "Trip to London",
"icon": "flight"
}
```

```json
{
"Name": "Trip to NYC",
"icon": "location"
}
```

---

# Step 7: Build and Run the Application

```bash
dotnet build
dotnet run
```

The backend started successfully.

---

# Step 8: Test the API Again

Request:

```http
GET /api/Goal
```

Response now includes:

```json
{
"name": "House Down Payment",
"balance": 73501.82,
"icon": "home"
}
```

This confirms the implementation works correctly.

---

# Step 9: Commit and Push to GitHub

```bash
git add .
git commit -m "Completed CommBank Forage project"
git push origin main
```

Changes were successfully pushed to GitHub.

---

# Final Result

### Before

```json
{
"name": "House Down Payment",
"balance": 73501.82
}
```

### After

```json
{
"name": "House Down Payment",
"balance": 73501.82,
"icon": "home"
}
```

---

# Skills Demonstrated

### MongoDB
- Atlas setup
- Database creation
- Collection creation
- JSON import
- Document updates

### ASP.NET Core
- Models
- Services
- Controllers
- Dependency Injection
- REST APIs

### API Testing
- Swagger
- Postman
- GET requests
- JSON responses

### Git & GitHub
- Commits
- Push operations
- Repository management

---

# Summary

Successfully extended the Goal model by adding an optional Icon property, updated MongoDB documents with icon values, and verified through Swagger and Postman that the API correctly returns the new icon field in goal responses.
3 changes: 1 addition & 2 deletions Server.sln
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 25.0.1700.1
MinimumVisualStudioVersion = 10.0.40219.1
Expand Down
20 changes: 20 additions & 0 deletions pull-request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Task 5 - Create Pull Request

## Pull Request Information

Title: Complete Task 5 - Create Pull Request

Pull Request Number: #263

Branch: task-5-pull-request

## Changes Made

- Created a new branch named task-5-pull-request.
- Added and committed project changes.
- Pushed the branch to GitHub.
- Created Pull Request #263.

## Purpose

This Pull Request demonstrates the use of Git branching, committing, pushing changes, and creating Pull Requests.