Skip to content

Commit 55cdae7

Browse files
feat: implement db
1 parent 35ed53d commit 55cdae7

5 files changed

Lines changed: 153 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using CanBeYours.Core.Domain.DemoThings;
2+
using CodeBlock.DevKit.Infrastructure.Database;
3+
using MongoDB.Driver;
4+
5+
namespace CanBeYours.Infrastructure.DbContext;
6+
7+
internal class DemoThingsDbContext : MongoDbContext
8+
{
9+
public DemoThingsDbContext(MongoDbSettings mongoDbSettings)
10+
: base(mongoDbSettings) { }
11+
12+
public IMongoCollection<DemoThing> DemoThings { get; private set; }
13+
14+
protected override void CreateIndexes()
15+
{
16+
DemoThings.Indexes.CreateOne(
17+
new CreateIndexModel<DemoThing>(
18+
Builders<DemoThing>.IndexKeys.Ascending(x => x.Name),
19+
new CreateIndexOptions() { Name = nameof(DemoThing.Name), Unique = false }
20+
)
21+
);
22+
}
23+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CanBeYours.Infrastructure.DbMigrations;
2+
using CodeBlock.DevKit.Infrastructure.Database.Migrations;
3+
using Microsoft.Extensions.DependencyInjection;
4+
5+
namespace CanBeYours.Infrastructure.DbContext;
6+
7+
internal static class DemoThingsDbMigrator
8+
{
9+
public static void MigrateDatabes(this IServiceProvider serviceProvider)
10+
{
11+
using var serviceScope = serviceProvider.CreateScope();
12+
var dbContext = serviceScope.ServiceProvider.GetService<DemoThingsDbContext>();
13+
var dbMigrationRunner = serviceScope.ServiceProvider.GetService<IDbMigrationRunner>();
14+
15+
var migrations = new List<IDbMigration>
16+
{
17+
new RenameSummaryToDescription(dbContext),
18+
// Add more migrations here as needed
19+
};
20+
21+
foreach (var migration in migrations)
22+
{
23+
dbMigrationRunner.ApplyMigration(migration);
24+
}
25+
}
26+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using CanBeYours.Core.Domain.DemoThings;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using MongoDB.Driver;
4+
5+
namespace CanBeYours.Infrastructure.DbContext;
6+
7+
internal static class DemoThingsDbSeeder
8+
{
9+
public static void SeedSampleDemoThings(this IServiceProvider serviceProvider)
10+
{
11+
using var serviceScope = serviceProvider.CreateScope();
12+
var dbContext = serviceScope.ServiceProvider.GetService<DemoThingsDbContext>();
13+
14+
serviceScope.SeedDemoThings(dbContext);
15+
}
16+
17+
private static void SeedDemoThings(this IServiceScope serviceScope, DemoThingsDbContext dbContext)
18+
{
19+
if (dbContext.DemoThings.Find(_ => true).Any())
20+
return;
21+
22+
var demoThingRepository = serviceScope.ServiceProvider.GetRequiredService<IDemoThingRepository>();
23+
24+
var demoThings = new List<DemoThing>
25+
{
26+
DemoThing.Create("Alpha Widget", "A simple starter widget."),
27+
DemoThing.Create("Beta Module", "Second-generation example module."),
28+
DemoThing.Create("Gamma Tool", "Helper tool for demos."),
29+
DemoThing.Create("Delta Feature", "Experimental feature for testing."),
30+
DemoThing.Create("Epsilon Block", "A UI block used in layouts."),
31+
DemoThing.Create("Zeta Component", "Blazor component example."),
32+
DemoThing.Create("Eta Job", "Background job simulator."),
33+
DemoThing.Create("Theta Report", "Analytics report generator."),
34+
DemoThing.Create("Iota API", "Sample REST endpoint scaffold."),
35+
DemoThing.Create("Kappa Function", "Business rule executor."),
36+
};
37+
38+
foreach (var thing in demoThings)
39+
demoThingRepository.Add(thing);
40+
}
41+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) CodeBlock.Dev. All rights reserved.
2+
// For more information visit https://codeblock.dev
3+
4+
using CanBeYours.Core.Domain.DemoThings;
5+
using CanBeYours.Infrastructure.DbContext;
6+
using CodeBlock.DevKit.Infrastructure.Database.Migrations;
7+
using MongoDB.Bson;
8+
using MongoDB.Driver;
9+
10+
namespace CanBeYours.Infrastructure.DbMigrations;
11+
12+
internal class RenameSummaryToDescription : IDbMigration
13+
{
14+
private readonly DemoThingsDbContext _dbContext;
15+
16+
public RenameSummaryToDescription(DemoThingsDbContext dbContext)
17+
{
18+
_dbContext = dbContext;
19+
}
20+
21+
public string UniqueName => "1_Rename_Summary_To_Description";
22+
23+
public string Description => "Renames the 'Summary' property to 'Description' in the DemoThings collection.";
24+
25+
public void Up()
26+
{
27+
var settings = _dbContext.GetCollection(nameof(DemoThingsDbContext.DemoThings));
28+
var filter = Builders<BsonDocument>.Filter.Exists("Summary");
29+
30+
var update = Builders<BsonDocument>.Update.Rename("Summary", nameof(DemoThing.Description));
31+
32+
settings.UpdateMany(filter, update);
33+
}
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) CodeBlock.Dev. All rights reserved.
2+
// For more information visit https://codeblock.dev
3+
4+
using CanBeYours.Core.Domain.DemoThings;
5+
using CodeBlock.DevKit.Infrastructure.Database;
6+
using MongoDB.Driver;
7+
8+
namespace CanBeYours.Infrastructure.Repositories;
9+
10+
internal class DemoThingRepository : MongoDbBaseAggregateRepository<DemoThing>, IDemoThingRepository
11+
{
12+
private readonly IMongoCollection<DemoThing> _settings;
13+
14+
public DemoThingRepository(DemoThingsDbContext dbContext)
15+
: base(dbContext.AppDemoThings)
16+
{
17+
_settings = dbContext.AppDemoThings;
18+
}
19+
20+
public async Task<DemoThing> GetAsync()
21+
{
22+
return await _settings.Find(x => true).FirstOrDefaultAsync();
23+
}
24+
25+
public DemoThing Get()
26+
{
27+
return _settings.Find(x => true).FirstOrDefault();
28+
}
29+
}

0 commit comments

Comments
 (0)