Skip to content

Commit 0db3020

Browse files
chore : seeder configuration
1 parent 966c978 commit 0db3020

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

Bottle.Api/Configurations/MySqlDbContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.EntityFrameworkCore;
22
using Bottle.Models;
3+
using Bottle.Api.Configurations.Seeders;
34

45
namespace Bottle.Api.Configurations
56
{
@@ -21,5 +22,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2122
.IsUnique();
2223
// user model end
2324
}
25+
public async Task SeedDataAsync()
26+
{
27+
await UserSeeder.SeedUsers(this);
28+
}
2429
}
2530
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Linq;
3+
using System.Security.Cryptography;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.EntityFrameworkCore;
7+
using Bottle.Models;
8+
9+
namespace Bottle.Api.Configurations.Seeders
10+
{
11+
public static class UserSeeder
12+
{
13+
public static async Task SeedUsers(MySqlDbContext context)
14+
{
15+
if (await context.Users.AnyAsync())
16+
{
17+
Console.WriteLine("Users already exist. Skipping seeding.");
18+
return;
19+
}
20+
21+
var users = new[]
22+
{
23+
new User
24+
{
25+
Id = Guid.NewGuid(),
26+
Name = "James Obeng",
27+
Email = "obengkofijames@gmail.com",
28+
PasswordHash = HashPassword("amenamen"),
29+
Api_secret = Guid.NewGuid().ToString("N"),
30+
CreatedAt = DateTime.UtcNow
31+
},
32+
};
33+
34+
await context.Users.AddRangeAsync(users);
35+
await context.SaveChangesAsync();
36+
Console.WriteLine("Users seeded successfully.");
37+
}
38+
39+
private static string HashPassword(string password)
40+
{
41+
using var sha256 = SHA256.Create();
42+
byte[] hashedBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
43+
return Convert.ToBase64String(hashedBytes);
44+
}
45+
}
46+
}

Bottle.Seeder/Bottle.Seeder.csproj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\Bottle.Api\Bottle.Api.csproj" />
5+
</ItemGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
9+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.4">
10+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
11+
<PrivateAssets>all</PrivateAssets>
12+
</PackageReference>
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.4" />
14+
</ItemGroup>
15+
16+
<PropertyGroup>
17+
<OutputType>Exe</OutputType>
18+
<TargetFramework>net9.0</TargetFramework>
19+
<ImplicitUsings>enable</ImplicitUsings>
20+
<Nullable>enable</Nullable>
21+
</PropertyGroup>
22+
23+
</Project>

Bottle.Seeder/Program.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.Hosting;
4+
using DotNetEnv;
5+
using Bottle.Api.Configurations;
6+
using Bottle.Api.Configurations.Seeders;
7+
8+
class Program
9+
{
10+
static async Task Main(string[] args)
11+
{
12+
Console.WriteLine("Starting Seeder...");
13+
14+
string? projectRoot = Directory.GetParent(AppContext.BaseDirectory)?.Parent?.Parent?.Parent?.Parent?.FullName;
15+
if (projectRoot != null)
16+
{
17+
string envPath = Path.Combine(projectRoot, ".env");
18+
Env.Load(envPath);
19+
}
20+
21+
string dbHost = Environment.GetEnvironmentVariable("DB_HOST") ?? "";
22+
string dbPort = Environment.GetEnvironmentVariable("DB_PORT") ?? "";
23+
string dbName = Environment.GetEnvironmentVariable("DB_NAME") ?? "";
24+
string dbUser = Environment.GetEnvironmentVariable("DB_USER") ?? "";
25+
string dbPassword = Environment.GetEnvironmentVariable("DB_PASSWORD") ?? "";
26+
27+
string connectionString = $"server={dbHost};port={dbPort};database={dbName};user={dbUser};password={dbPassword};";
28+
29+
var builder = Host.CreateDefaultBuilder()
30+
.ConfigureServices((context, services) =>
31+
{
32+
services.AddDbContext<MySqlDbContext>(options =>
33+
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString))
34+
);
35+
})
36+
.Build();
37+
38+
using (var scope = builder.Services.CreateScope())
39+
{
40+
var dbContext = scope.ServiceProvider.GetRequiredService<MySqlDbContext>();
41+
42+
Console.WriteLine("Seeding...");
43+
await UserSeeder.SeedUsers(dbContext);
44+
Console.WriteLine("seeding completed.");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)