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
173 changes: 173 additions & 0 deletions CommBank-Server/Data/SeedDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using MongoDB.Driver;
using CommBank.Models;
using System;
using System.Threading.Tasks;

namespace CommBank.Data
{
public class SeedDatabase
{
public static async Task SeedData(IMongoDatabase database)
{
Console.WriteLine("Starting database seeding...");

// Get collections
var usersCollection = database.GetCollection<User>("users");
var accountsCollection = database.GetCollection<Account>("accounts");
var goalsCollection = database.GetCollection<Goal>("goals");
var transactionsCollection = database.GetCollection<Transaction>("transactions");
var tagsCollection = database.GetCollection<Tag>("tags");

// Clear existing data
await usersCollection.DeleteManyAsync(FilterDefinition<User>.Empty);
await accountsCollection.DeleteManyAsync(FilterDefinition<Account>.Empty);
await goalsCollection.DeleteManyAsync(FilterDefinition<Goal>.Empty);
await transactionsCollection.DeleteManyAsync(FilterDefinition<Transaction>.Empty);
await tagsCollection.DeleteManyAsync(FilterDefinition<Tag>.Empty);

Console.WriteLine("Cleared existing data");

// Seed users
var users = new[]
{
new User
{
Id = "user123",
Username = "john_doe",
Email = "john@example.com",
Password = "$2a$10$N9qo8kLO2zzq2w3Kv9SjKgqNj", // hashed password
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
},
new User
{
Id = "user456",
Username = "jane_smith",
Email = "jane@example.com",
Password = "$2a$10$N9qo8kLO2zzq2w3Kv9SjKgqNj", // hashed password
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
}
};

await usersCollection.InsertManyAsync(users);
Console.WriteLine($"Seeded {users.Length} users");

// Seed accounts
var accounts = new[]
{
new Account
{
Id = "acc123",
UserId = "user123",
AccountType = "checking",
Balance = 1500.00m,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
},
new Account
{
Id = "acc456",
UserId = "user456",
AccountType = "savings",
Balance = 5000.00m,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
}
};

await accountsCollection.InsertManyAsync(accounts);
Console.WriteLine($"Seeded {accounts.Length} accounts");

// Seed goals
var goals = new[]
{
new Goal
{
Id = "goal123",
UserId = "user123",
Name = "Emergency Fund",
TargetAmount = 10000,
TargetDate = new DateTime(2024, 12, 31),
Balance = 1500.00,
Created = DateTime.UtcNow,
TransactionIds = new List<string>(),
TagIds = new List<string>(),
Icon = "🎯"
},
new Goal
{
Id = "goal456",
UserId = "user456",
Name = "Vacation Fund",
TargetAmount = 3000,
TargetDate = new DateTime(2024, 6, 30),
Balance = 500.00,
Created = DateTime.UtcNow,
TransactionIds = new List<string>(),
TagIds = new List<string>(),
Icon = "✈️"
}
};

await goalsCollection.InsertManyAsync(goals);
Console.WriteLine($"Seeded {goals.Length} goals");

// Seed transactions
var transactions = new[]
{
new Transaction
{
Id = "trans123",
UserId = "user123",
Amount = -200.00m,
TransactionType = "withdrawal",
Description = "ATM withdrawal",
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
},
new Transaction
{
Id = "trans456",
UserId = "user123",
Amount = 500.00m,
TransactionType = "deposit",
Description = "Salary deposit",
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
}
};

await transactionsCollection.InsertManyAsync(transactions);
Console.WriteLine($"Seeded {transactions.Length} transactions");

// Seed tags
var tags = new[]
{
new Tag
{
Id = "tag123",
UserId = "user123",
Name = "Bills",
Color = "#FF6B6B",
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
},
new Tag
{
Id = "tag456",
UserId = "user456",
Name = "Food",
Color = "#4CAF50",
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
}
};

await tagsCollection.InsertManyAsync(tags);
Console.WriteLine($"Seeded {tags.Length} tags");

Console.WriteLine("Database seeding completed successfully!");
}
}
}
121 changes: 121 additions & 0 deletions CommBank-Server/Data/seed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Database seeding script for CommBank
// Run this script to populate the database with initial data

// Sample users
const users = [
{
_id: "user123",
username: "john_doe",
email: "john@example.com",
password: "$2a$10$...", // hashed password
created_at: new Date(),
updated_at: new Date()
},
{
_id: "user456",
username: "jane_smith",
email: "jane@example.com",
password: "$2a$10$...", // hashed password
created_at: new Date(),
updated_at: new Date()
}
];

// Sample accounts
const accounts = [
{
_id: "acc123",
user_id: "user123",
account_type: "checking",
balance: 1500.00,
created_at: new Date(),
updated_at: new Date()
},
{
_id: "acc456",
user_id: "user456",
account_type: "savings",
balance: 5000.00,
created_at: new Date(),
updated_at: new Date()
}
];

// Sample goals
const goals = [
{
_id: "goal123",
user_id: "user123",
account_id: "acc123",
name: "Emergency Fund",
target_amount: 10000.00,
current_amount: 1500.00,
deadline: new Date("2024-12-31"),
icon: "🎯",
created_at: new Date(),
updated_at: new Date()
},
{
_id: "goal456",
user_id: "user456",
account_id: "acc456",
name: "Vacation Fund",
target_amount: 3000.00,
current_amount: 500.00,
deadline: new Date("2024-06-30"),
icon: "✈️",
created_at: new Date(),
updated_at: new Date()
}
];

// Sample transactions
const transactions = [
{
_id: "trans123",
user_id: "user123",
account_id: "acc123",
amount: -200.00,
transaction_type: "withdrawal",
description: "ATM withdrawal",
created_at: new Date(),
updated_at: new Date()
},
{
_id: "trans456",
user_id: "user123",
account_id: "acc123",
amount: 500.00,
transaction_type: "deposit",
description: "Salary deposit",
created_at: new Date(),
updated_at: new Date()
}
];

// Sample tags
const tags = [
{
_id: "tag123",
user_id: "user123",
name: "Bills",
color: "#FF6B6B",
created_at: new Date(),
updated_at: new Date()
},
{
_id: "tag456",
user_id: "user456",
name: "Food",
color: "#4CAF50",
created_at: new Date(),
updated_at: new Date()
}
];

console.log("Database seeding data created successfully!");
console.log("Users:", users.length);
console.log("Accounts:", accounts.length);
console.log("Goals:", goals.length);
console.log("Transactions:", transactions.length);
console.log("Tags:", tags.length);
6 changes: 4 additions & 2 deletions CommBank-Server/Models/Goal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class Goal
public DateTime Created { get; set; } = DateTime.Now;

[BsonRepresentation(BsonType.ObjectId)]
public List<string>? TransactionIds { get; set; }
public List<string>? TagIds { get; set; }

[BsonRepresentation(BsonType.ObjectId)]
public List<string>? TagIds { get; set; }
public List<string>? TransactionIds { get; set; }

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

public string? Icon { get; set; }
}
53 changes: 0 additions & 53 deletions CommBank-Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,53 +0,0 @@
using CommBank.Models;
using CommBank.Services;
using MongoDB.Driver;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

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

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

var mongoClient = new MongoClient(builder.Configuration.GetConnectionString("CommBank"));
var mongoDatabase = mongoClient.GetDatabase("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);

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

var app = builder.Build();

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

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

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

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://username:password@cluster.mongodb.net/"
}
}
Loading