Skip to content

Commit

Permalink
Merge pull request #842 from EdiWang/dev/theme-refact
Browse files Browse the repository at this point in the history
Refact System Theme
  • Loading branch information
EdiWang authored Nov 6, 2024
2 parents 07f1f62 + 6616362 commit 7b68930
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class BlogThemeConfiguration : IEntityTypeConfiguration<BlogThemeEntity>
public void Configure(EntityTypeBuilder<BlogThemeEntity> builder)
{
builder.HasKey(e => e.Id);
builder.Property(e => e.Id).ValueGeneratedOnAdd();
builder.Property(e => e.ThemeName).HasMaxLength(32);
}
}
32 changes: 0 additions & 32 deletions src/Moonglade.Data/Seed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ public static async Task SeedAsync(BlogDbContext dbContext, ILogger logger, int

try
{
logger.LogDebug("Adding themes data...");
await dbContext.BlogTheme.AddRangeAsync(GetThemes());

logger.LogDebug("Adding categories data...");
await dbContext.Category.AddRangeAsync(GetCategories());

Expand Down Expand Up @@ -67,35 +64,6 @@ public static async Task SeedAsync(BlogDbContext dbContext, ILogger logger, int
}
}

private static IEnumerable<BlogThemeEntity> GetThemes() =>
new List<BlogThemeEntity>
{
new ()
{
ThemeName = "Word Blue", CssRules = "{\"--accent-color1\": \"#2a579a\",\"--accent-color2\": \"#1a365f\",\"--accent-color3\": \"#3e6db5\"}", ThemeType = 0
},
new ()
{
ThemeName = "Excel Green", CssRules = "{\"--accent-color1\": \"#165331\",\"--accent-color2\": \"#0E351F\",\"--accent-color3\": \"#0E703A\"}", ThemeType = 0
},
new ()
{
ThemeName = "PowerPoint Orange", CssRules = "{\"--accent-color1\": \"#983B22\",\"--accent-color2\": \"#622616\",\"--accent-color3\": \"#C43E1C\"}", ThemeType = 0
},
new ()
{
ThemeName = "OneNote Purple", CssRules = "{\"--accent-color1\": \"#663276\",\"--accent-color2\": \"#52285E\",\"--accent-color3\": \"#7719AA\"}", ThemeType = 0
},
new ()
{
ThemeName = "Outlook Blue", CssRules = "{\"--accent-color1\": \"#035AA6\",\"--accent-color2\": \"#032B51\",\"--accent-color3\": \"#006CBF\"}", ThemeType = 0
},
new ()
{
ThemeName = "Metal Blue", CssRules = "{\"--accent-color1\": \"#4E5967\",\"--accent-color2\": \"#333942\",\"--accent-color3\": \"#6e7c8e\"}", ThemeType = 0
}
};

private static IEnumerable<CategoryEntity> GetCategories() =>
new List<CategoryEntity>
{
Expand Down
8 changes: 8 additions & 0 deletions src/Moonglade.Data/Specifications/BlogThemeForIdNameSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public BlogThemeForIdNameSpec()
Query.Select(p => new(p.Id, p.ThemeName));
Query.AsNoTracking();
}
}

public sealed class ThemeByTypeSpec : Specification<BlogThemeEntity>
{
public ThemeByTypeSpec(ThemeType type)
{
Query.Where(p => p.ThemeType == type);
}
}
18 changes: 18 additions & 0 deletions src/Moonglade.Setup/CleanupLegacySystemThemeCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using MediatR;
using Moonglade.Data;
using Moonglade.Data.Entities;
using Moonglade.Data.Specifications;

namespace Moonglade.Setup;

public class CleanupLegacySystemThemeCommand : IRequest;

public class CleanupLegacySystemThemeCommandHandler(MoongladeRepository<BlogThemeEntity> repo)
: IRequestHandler<CleanupLegacySystemThemeCommand>
{
public async Task Handle(CleanupLegacySystemThemeCommand request, CancellationToken ct)
{
var existingThemes = await repo.ListAsync(new ThemeByTypeSpec(ThemeType.System), ct);
await repo.DeleteRangeAsync(existingThemes, ct);
}
}
15 changes: 15 additions & 0 deletions src/Moonglade.Setup/MigrationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ public async Task TryMigration(BlogDbContext context)
await mediator.Send(new UpdateConfigurationCommand(kvp.Key, kvp.Value));

logger.LogInformation("Database migration completed.");

await MigrateTheme(mfv);
}
}

private async Task MigrateTheme(Version mfv)
{
// Migrate theme
if (mfv <= Version.Parse("14.12.0"))
{
logger.LogInformation("Migrating theme...");

await mediator.Send(new CleanupLegacySystemThemeCommand());

logger.LogInformation("Theme migration completed.");
}
}

Expand Down
14 changes: 12 additions & 2 deletions src/Moonglade.Theme/GetAllThemeSegmentQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ public record GetAllThemeSegmentQuery : IRequest<List<ThemeSegment>>;

public class GetAllThemeSegmentQueryHandler(MoongladeRepository<BlogThemeEntity> repo) : IRequestHandler<GetAllThemeSegmentQuery, List<ThemeSegment>>
{
public Task<List<ThemeSegment>> Handle(GetAllThemeSegmentQuery request, CancellationToken ct) =>
repo.ListAsync(new BlogThemeForIdNameSpec(), ct);
public async Task<List<ThemeSegment>> Handle(GetAllThemeSegmentQuery request, CancellationToken ct)
{
var systemThemes = ThemeFactory.GetSystemThemes();
var customThemes = await repo.ListAsync(new BlogThemeForIdNameSpec(), ct);

var result = new List<ThemeSegment>();

result.AddRange(systemThemes.Select(t => new ThemeSegment(t.Id, t.ThemeName)));
result.AddRange(customThemes);

return result;
}
}
15 changes: 14 additions & 1 deletion src/Moonglade.Theme/GetSiteThemeStyleSheetQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ public class GetStyleSheetQueryHandler(MoongladeRepository<BlogThemeEntity> repo
{
public async Task<string> Handle(GetSiteThemeStyleSheetQuery request, CancellationToken ct)
{
var theme = await repo.GetByIdAsync(request.Id, ct);
BlogThemeEntity theme;

if (request.Id is < 100 or > 110)
{
// Custom theme
// if not found, fall back to system theme Id 100
theme = await repo.GetByIdAsync(request.Id, ct) ?? ThemeFactory.GetSystemThemes().FirstOrDefault(t => t.Id == 100);
}
else
{
// System theme
theme = ThemeFactory.GetSystemThemes().FirstOrDefault(t => t.Id == request.Id);
}

if (null == theme) return null;

if (string.IsNullOrWhiteSpace(theme.CssRules))
Expand Down
30 changes: 30 additions & 0 deletions src/Moonglade.Theme/ThemeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Moonglade.Data.Entities;

namespace Moonglade.Theme;

public static class ThemeFactory
{
public static IEnumerable<BlogThemeEntity> GetSystemThemes()
{
return new List<BlogThemeEntity>
{
CreateTheme(100, "Word Blue (Default)", "#2a579a", "#1a365f", "#3e6db5"),
CreateTheme(101, "Excel Green", "#165331", "#0E351F", "#0E703A"),
CreateTheme(102, "PowerPoint Orange", "#983B22", "#622616", "#C43E1C"),
CreateTheme(103, "OneNote Purple", "#663276", "#52285E", "#7719AA"),
CreateTheme(104, "Outlook Blue", "#035AA6", "#032B51", "#006CBF"),
CreateTheme(105, "Metal Blue", "#4E5967", "#333942", "#6e7c8e")
};
}

private static BlogThemeEntity CreateTheme(int id, string themeName, string color1, string color2, string color3)
{
return new()
{
Id = id,
ThemeName = themeName,
CssRules = $"{{\"--accent-color1\": \"{color1}\",\"--accent-color2\": \"{color2}\",\"--accent-color3\": \"{color3}\"}}",
ThemeType = 0
};
}
}

0 comments on commit 7b68930

Please sign in to comment.