Skip to content

Commit

Permalink
Merge pull request #187 from fluentcms/183-seeddata-cleanup
Browse files Browse the repository at this point in the history
#183 default data loader
  • Loading branch information
pournasserian authored Nov 16, 2023
2 parents 85fd9bc + 316319e commit 7fbc893
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 89 deletions.
20 changes: 20 additions & 0 deletions src/FluentCMS.Api/DefaultData/DefaultData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using FluentCMS.Entities;

namespace FluentCMS.Api;

public class DefaultData
{
public required Host Host { get; set; }
public required DefaultUser SuperAdmin { get; set; }
public required DefaultUser Admin { get; set; }
public required Role AdminRole { get; set; }
public required Site Site { get; set; }
public required List<Page> Pages { get; set; }

public class DefaultUser
{
public required string UserName { get; set; }
public required string Email { get; set; }
public required string Password { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

namespace FluentCMS.Api;

public static class SeedData
public static class DefaultDataLoaderExtensions
{
// TODO: in production we should delete the folder after seeding
// TODO: check if at least one user if initialized
// TODO: check this for the file path on deployment
public static void SeedDefaultData(this IServiceProvider provider, string dataFolder)
public static void LoadInitialDataFrom(this IServiceProvider provider, string dataFolder)
{
var scope = provider.CreateScope();

Expand All @@ -23,52 +20,47 @@ public static void SeedDefaultData(this IServiceProvider provider, string dataFo

if (!hostService.IsInitialized().GetAwaiter().GetResult())
{
var superAdmin = LoadData<DefaultUser>($@"{dataFolder}\superadmin.json");
var host = LoadData<Host>($@"{dataFolder}\host.json");
var site = LoadData<Site>($@"{dataFolder}\site.json");
var admin = LoadData<DefaultUser>($@"{dataFolder}\admin.json");
var adminRole = LoadData<Role>($@"{dataFolder}\adminrole.json");
var defaultData = LoadData<DefaultData>($@"{dataFolder}\default.json");

var superUser = new User
{
UserName = superAdmin.UserName,
Email = superAdmin.Email
UserName = defaultData.SuperAdmin.UserName,
Email = defaultData.SuperAdmin.Email
};

var adminUser = new User
{
UserName = admin.UserName,
Email = admin.Email
UserName = defaultData.Admin.UserName,
Email = defaultData.Admin.Email
};

appContext.Current = new CurrentContext
{
User = superUser,
Host = host
Host = defaultData.Host
};

// Super Admin creation
userService.Create(superUser, superAdmin.Password).GetAwaiter().GetResult();
userService.Create(superUser, defaultData.SuperAdmin.Password).GetAwaiter().GetResult();

// Admin Role creation
roleService.Create(adminRole).GetAwaiter().GetResult();
roleService.Create(defaultData.AdminRole).GetAwaiter().GetResult();

// Admin creation
adminUser.RoleIds = [adminRole.Id];
userService.Create(adminUser, admin.Password).GetAwaiter().GetResult();
adminUser.RoleIds = [defaultData.AdminRole.Id];
userService.Create(adminUser, defaultData.Admin.Password).GetAwaiter().GetResult();

// Host creation
hostService.Create(host).GetAwaiter().GetResult();
hostService.Create(defaultData.Host).GetAwaiter().GetResult();

// Site creation
site.AdminRoleIds = [adminRole.Id];
siteService.Create(site).GetAwaiter().GetResult();
defaultData.Site.AdminRoleIds = [defaultData.AdminRole.Id];
siteService.Create(defaultData.Site).GetAwaiter().GetResult();

// Pages creation: adding a few default pages
var pages = LoadData<List<Page>>($@"{dataFolder}\pages.json");
foreach (var page in pages)
foreach (var page in defaultData.Pages)
{
page.SiteId = site.Id;
page.SiteId = defaultData.Site.Id;
pageService.Create(page).GetAwaiter().GetResult();
}
}
Expand All @@ -91,12 +83,4 @@ private static T LoadData<T>(string jsonFile)
throw new Exception($"Unable to load seed data from {jsonFile}");
}
}

private class DefaultUser
{
public required string UserName { get; set; }
public required string Email { get; set; }
public required string Password { get; set; }
}

}
1 change: 0 additions & 1 deletion src/FluentCMS.Entities/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ public class Page : AuditEntity
public required string Path { get; set; }
public IEnumerable<Guid> AdminRoleIds { get; set; } = new List<Guid>();
public IEnumerable<Guid> ViewRoleIds { get; set; } = new List<Guid>();

}
22 changes: 11 additions & 11 deletions src/FluentCMS.Services/RoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public Task<IEnumerable<Role>> GetAll(Guid siteId, CancellationToken cancellatio

public async Task<Role> Create(Role role, CancellationToken cancellationToken)
{
var site = await _siteRepository.GetById(role.SiteId, cancellationToken);
//var site = await _siteRepository.GetById(role.SiteId, cancellationToken);

if (!Current.IsInRole(site?.AdminRoleIds ?? []))
throw new Exception("Only admin can create a role.");
//if (!Current.IsInRole(site?.AdminRoleIds ?? []))
// throw new Exception("Only admin can create a role.");

PrepareForCreate(role);

Expand All @@ -57,13 +57,13 @@ public async Task<Role> Create(Role role, CancellationToken cancellationToken)

public async Task<Role> Update(Role role, CancellationToken cancellationToken)
{
var site = await _siteRepository.GetById(role.SiteId, cancellationToken);
//var site = await _siteRepository.GetById(role.SiteId, cancellationToken);

if (role.SiteId != site.Id)
throw new Exception("Role must be updated for the current site.");
//if (role.SiteId != site.Id)
// throw new Exception("Role must be updated for the current site.");

if (!Current.IsInRole(site.AdminRoleIds))
throw new Exception("Only admin can update a role.");
//if (!Current.IsInRole(site.AdminRoleIds))
// throw new Exception("Only admin can update a role.");

PrepareForUpdate(role);

Expand All @@ -76,10 +76,10 @@ public async Task<Role> Update(Role role, CancellationToken cancellationToken)

public async Task Delete(Role role, CancellationToken cancellationToken = default)
{
var site = await _siteRepository.GetById(role.SiteId, cancellationToken);
//var site = await _siteRepository.GetById(role.SiteId, cancellationToken);

if (!Current.IsInRole(site.AdminRoleIds))
throw new Exception("Only admin can update a role.");
//if (!Current.IsInRole(site.AdminRoleIds))
// throw new Exception("Only admin can update a role.");

var idResult = await RoleManager.DeleteAsync(role);

Expand Down
44 changes: 44 additions & 0 deletions src/FluentCMS.Web.UI/DefaultData/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"Host": {
"SuperUsers": [ "superadmin" ]
},
"SuperAdmin": {
"Username": "superadmin",
"Password": "Passw0rd!",
"Email": "[email protected]"
},
"Admin": {
"Username": "admin",
"Password": "Passw0rd!",
"Email": "[email protected]"
},
"Site": {
"Name": "My First Site",
"Description": "This is my first site",
"Urls": [ "http://www.myfirstsite.com" ]
},
"AdminRole": {
"Name": "Administrators",
"Description": "Administrator roles for the site"
},
"Pages": [
{
"Title": "Home",
"Description": "This is my home page",
"Path": "/",
"Order": 0
},
{
"Title": "About me",
"Description": "This is about me page",
"Path": "/about-me",
"Order": 1
},
{
"Title": "Contact me",
"Description": "This is contact me page",
"Path": "/contact",
"Order": 2
}
]
}
2 changes: 1 addition & 1 deletion src/FluentCMS.Web.UI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

var app = builder.Build();

app.Services.SeedDefaultData(@".\SeedData\");
app.Services.LoadInitialDataFrom(@".\DefaultData\");

if (app.Environment.IsDevelopment())
{
Expand Down
5 changes: 0 additions & 5 deletions src/FluentCMS.Web.UI/SeedData/admin.json

This file was deleted.

4 changes: 0 additions & 4 deletions src/FluentCMS.Web.UI/SeedData/adminrole.json

This file was deleted.

3 changes: 0 additions & 3 deletions src/FluentCMS.Web.UI/SeedData/host.json

This file was deleted.

20 changes: 0 additions & 20 deletions src/FluentCMS.Web.UI/SeedData/pages.json

This file was deleted.

5 changes: 0 additions & 5 deletions src/FluentCMS.Web.UI/SeedData/site.json

This file was deleted.

5 changes: 0 additions & 5 deletions src/FluentCMS.Web.UI/SeedData/superadmin.json

This file was deleted.

0 comments on commit 7fbc893

Please sign in to comment.