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
8 changes: 7 additions & 1 deletion CommBank-Server/CommBank.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<Folder Include="Services\" />
<Folder Include="Models\" />
<Folder Include="Properties\" />
<Folder Include="Data\" />
</ItemGroup>

<ItemGroup>
<None Update="Data\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
53 changes: 53 additions & 0 deletions CommBank-Server/Data/Accounts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[{
"_id": {
"$oid": "62a3e6aad25715026d1a2938"
},
"Number": 123456789,
"Name": "Tag's Goal Saver",
"Balance": 6483.81,
"AccountType": "GoalSaver",
"TransactionIds": [
{
"$oid": "62a3a284d07648900df72860"
},
{
"$oid": "62a3a2ded07648900df72861"
},
{
"$oid": "62a3a2ded07648900df72862"
},
{
"$oid": "62a3a2ded07648900df72863"
},
{
"$oid": "62a3a2ded07648900df72864"
},
{
"$oid": "62a3a2ded07648900df72865"
},
{
"$oid": "62a3a2ded07648900df72866"
},
{
"$oid": "62a3a2ded07648900df72867"
},
{
"$oid": "62a3a2ded07648900df72868"
},
{
"$oid": "62a3a2ded07648900df72869"
},
{
"$oid": "62a3a344d07648900df7286a"
},
{
"$oid": "62a3a344d07648900df7286b"
},
{
"$oid": "62a3a344d07648900df7286c"
},
{
"$oid": "62a3a344d07648900df7286d"
}
]
}]
7 changes: 7 additions & 0 deletions CommBank-Server/Data/CollectionNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CommBank.Data;

public class CollectionNameAttribute : Attribute
{
public string Name { get; set; }
public CollectionNameAttribute(string name) => Name = name;
}
94 changes: 94 additions & 0 deletions CommBank-Server/Data/Goals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[
{
"_id": {
"$oid": "62a3f587102e921da1253d32"
},
"Name": "House Down Payment",
"TargetAmount": 100000,
"TargetDate": {
"$date": {
"$numberLong": "1736312400000"
}
},
"Balance": 73501.82,
"Created": {
"$date": {
"$numberLong": "1654912390857"
}
},
"TransactionIds": null,
"TagIds": null,
"UserId": {
"$oid": "62a29c15f4605c4c9fa7f306"
}
},
{
"_id": {
"$oid": "62a3f5e0102e921da1253d33"
},
"Name": "Tesla Model Y",
"TargetAmount": 60000,
"TargetDate": {
"$date": {
"$numberLong": "1662004800000"
}
},
"Balance": 43840.02,
"Created": {
"$date": {
"$numberLong": "1654912480950"
}
},
"TransactionIds": null,
"TagIds": null,
"UserId": {
"$oid": "62a29c15f4605c4c9fa7f306"
}
},
{
"_id": {
"$oid": "62a3f62e102e921da1253d34"
},
"Name": "Trip to London",
"TargetAmount": 3500,
"TargetDate": {
"$date": {
"$numberLong": "1659412800000"
}
},
"Created": {
"$date": {
"$numberLong": "1654912558236"
}
},
"TransactionIds": null,
"TagIds": null,
"Balance": 753.89,
"UserId": {
"$oid": "62a29c15f4605c4c9fa7f306"
}
},
{
"_id": {
"$oid": "62a61945fa15f1cd18516a5f"
},
"Name": "Trip to NYC",
"TargetAmount": 800,
"TargetDate": {
"$date": {
"$numberLong": "1702184400000"
}
},
"Balance": 0,
"Created": {
"$date": {
"$numberLong": "1655053065668"
}
},
"TransactionIds": null,
"TagIds": null,
"UserId": {
"$oid": "62a29c15f4605c4c9fa7f306"
}
}
]
15 changes: 15 additions & 0 deletions CommBank-Server/Data/MongoDatabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using CommBank.Data;
using MongoDB.Driver;

namespace CommBank.Data;

public static class MongoDatabaseExtensions
{
public static IMongoCollection<T> GetCollection<T>(this IMongoDatabase database)
{
var attribute = typeof(T).GetCustomAttribute<CollectionNameAttribute>();
var collection = database.GetCollection<T>(attribute?.Name ?? typeof(T).Name + "s");
return collection;
}
}
95 changes: 95 additions & 0 deletions CommBank-Server/Data/MongoDbSeeder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Reflection;
using System.Text.Json;
using CommBank.Models;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using Tag = CommBank.Models.Tag;

namespace CommBank.Data;

public class MongoDbSeeder : IDisposable
{

private readonly IMongoDatabase _database;
private readonly IMongoCollection<Account> _accountsCollection;
private readonly IMongoCollection<Goal> _goalsCollection;
private readonly IMongoCollection<Tag> _tagsCollection;
private readonly IMongoCollection<Transaction> _transactionsCollection;
private readonly IMongoCollection<User> _usersCollection;




public MongoDbSeeder(IMongoDatabase mongoDatabase)
{
_database = mongoDatabase;
_accountsCollection = mongoDatabase.GetCollection<Account>();
_goalsCollection = mongoDatabase.GetCollection<Goal>();
_tagsCollection = mongoDatabase.GetCollection<Tag>();
_transactionsCollection = mongoDatabase.GetCollection<Transaction>();
_usersCollection = mongoDatabase.GetCollection<User>();
}

public async Task SeedAsync()
{
await SeedCollectionAsync<Account>("Accounts.json");
await SeedCollectionAsync<Goal>("Goals.json");
await SeedCollectionAsync<Tag>("Tags.json");
await SeedCollectionAsync<Transaction>("Transactions.json");
await SeedCollectionAsync<User>("Users.json");
}

private async Task SeedCollectionAsync<T>(string fileName)
{
var collectionName = typeof(T).GetCustomAttribute<CollectionNameAttribute>()?.Name ?? typeof(T).Name + "s";
var collection = _database.GetCollection<T>(collectionName);

// Check if the collection is already seeded
bool hasData = await collection.Find(_ => true).AnyAsync();

if (!hasData)
{
var filePath = Path.Combine(AppContext.BaseDirectory, "Data", fileName);

if (!File.Exists(filePath))
{
Console.WriteLine($"Seed file not found: {filePath}");
return;
}

// Read and deserialize the JSON file
var jsonString = await File.ReadAllTextAsync(filePath);

var documentArray = BsonSerializer.Deserialize<BsonArray>(jsonString);

var items = documentArray
.Select(doc => BsonSerializer.Deserialize<T>(doc.AsBsonDocument))
.ToList();

// Insert the data if the file wasn't empty
if (items is { Count: > 0 })
{
await collection.InsertManyAsync(items);
Console.WriteLine($"Successfully seeded {items.Count} items into {collectionName}.");
}
}
}

public void Dispose()
{


}
}

public static class MongoDbSeederExtensions
{
public static async Task SeedDataAsync(this IHost host)
{
using var scope = host.Services.CreateScope();
var mongoDatabase = scope.ServiceProvider.GetRequiredService<IMongoDatabase>();
using var seeder = new MongoDbSeeder(mongoDatabase);
await seeder.SeedAsync();
}
}
26 changes: 26 additions & 0 deletions CommBank-Server/Data/Tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[{
"_id": {
"$oid": "62a39d27025ca1ba8f1f1c1e"
},
"Name": "Groceries"
},{
"_id": {
"$oid": "62a39d42025ca1ba8f1f1c1f"
},
"Name": "Restaurant"
},{
"_id": {
"$oid": "62a39d4e025ca1ba8f1f1c20"
},
"Name": "Income"
},{
"_id": {
"$oid": "62a39d5a025ca1ba8f1f1c21"
},
"Name": "Gas"
},{
"_id": {
"$oid": "62a39d63025ca1ba8f1f1c22"
},
"Name": "Investment"
}]
Loading