Skip to content

Commit

Permalink
Database Seeding
Browse files Browse the repository at this point in the history
  • Loading branch information
DejanMilicic committed Feb 5, 2021
1 parent de6646c commit 98b63e9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Digitalis/Infrastructure/Services/DbSeeding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Linq;
using Digitalis.Models;
using Raven.Client.Documents;

namespace Digitalis.Infrastructure.Services
{
public class DbSeeding
{
public void Setup(IDocumentStore store, string superAdminEmail)
{
using var session = store.OpenSession();

User superAdmin = session.Query<User>().SingleOrDefault(x => x.Email == superAdminEmail);
if (superAdmin == null)
{
superAdmin = new User();
superAdmin.Claims.Add((AppClaims.CreateNewEntry, ""));

session.Store(superAdmin);
session.SaveChanges();
}
}
}
}
2 changes: 2 additions & 0 deletions src/Digitalis/Infrastructure/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public class DatabaseSettings

public string CertPass { get; set; }
}

public string SuperAdmin { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/Digitalis/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Digitalis.Features;
using Digitalis.Infrastructure.Mediatr;
using Digitalis.Infrastructure.OpenApi;
using Digitalis.Infrastructure.Services;
using Digitalis.Services;
using FluentValidation;
using Microsoft.AspNetCore.Authentication.JwtBearer;
Expand Down Expand Up @@ -68,6 +69,9 @@ public void ConfigureServices(IServiceCollection services)

IndexCreation.CreateIndexes(typeof(Startup).Assembly, store);

DbSeeding dbs = new DbSeeding();
dbs.Setup(store, Configuration.GetSection("SuperAdmin").Get<string>());

return store;
});

Expand Down
1 change: 1 addition & 0 deletions src/Digitalis/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"CertPath": null,
"CertPass": null
},
"SuperAdmin": "[email protected]",
"AllowedHosts": "*"
}

0 comments on commit 98b63e9

Please sign in to comment.