Skip to content

Commit a8d8afc

Browse files
committed
Refactor to spin off classes
1 parent 7dec430 commit a8d8afc

34 files changed

+5034
-4428
lines changed

.tools/Bible.Alarm.AudioLinksHarvestor/Utility/DbSeeder.cs

Lines changed: 37 additions & 1190 deletions
Large diffs are not rendered by default.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#nullable enable
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Bible.Alarm.Shared.Database;
5+
using Bible.Alarm.Shared.Models.Media;
6+
using Bible.Alarm.Shared.Models.Media.BiblePublications;
7+
using Microsoft.EntityFrameworkCore;
8+
using Serilog;
9+
10+
namespace Bible.Alarm.AudioLinksHarvestor.Utility.Helpers;
11+
12+
/// <summary>
13+
/// Helper class for seeding categories and API URLs.
14+
/// </summary>
15+
internal sealed class CategorySeeder
16+
{
17+
private readonly ILogger logger;
18+
19+
public CategorySeeder(ILogger logger)
20+
{
21+
this.logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
22+
}
23+
24+
public async Task SeedDefaultCategoriesAndApiUrls(MediaDbContext db)
25+
{
26+
// Seed Categories
27+
var categories = new[]
28+
{
29+
new Category { CategoryName = "Bible" },
30+
new Category { CategoryName = "Dramas" },
31+
new Category { CategoryName = "Music" }
32+
};
33+
34+
foreach (var category in categories)
35+
{
36+
var existing = await db.Categories.FirstOrDefaultAsync(c => c.CategoryName == category.CategoryName);
37+
if (existing == null)
38+
{
39+
db.Categories.Add(category);
40+
logger.Information("Seeding category: {CategoryName}", category.CategoryName);
41+
}
42+
}
43+
44+
// Seed ApiUrls
45+
var apiUrls = new[]
46+
{
47+
new BaseUrl
48+
{
49+
Url = "https://b.jw-cdn.org",
50+
PathPrefix = "apis/pub-media/GETPUBMEDIALINKS"
51+
},
52+
new BaseUrl
53+
{
54+
Url = "https://app.jw-cdn.org",
55+
PathPrefix = "apis/pub-media/GETPUBMEDIALINKS"
56+
}
57+
};
58+
59+
foreach (var apiUrl in apiUrls)
60+
{
61+
// Check if this specific URL and PathPrefix combination already exists
62+
var existingApiUrl = await db.BaseUrls.FirstOrDefaultAsync(a =>
63+
a.Url == apiUrl.Url && a.PathPrefix == apiUrl.PathPrefix);
64+
if (existingApiUrl == null)
65+
{
66+
db.BaseUrls.Add(apiUrl);
67+
await db.SaveChangesAsync(); // Save to get the ID
68+
69+
// Seed UrlParam with output=json for this base URL
70+
var urlParam = new UrlParam
71+
{
72+
BaseUrlId = apiUrl.Id,
73+
Key = "output",
74+
Value = "json",
75+
IsQueryParam = true
76+
};
77+
db.UrlParams.Add(urlParam);
78+
79+
logger.Information("Seeding ApiUrl: {BaseUrl} / {PathPrefix} with output=json", apiUrl.Url, apiUrl.PathPrefix);
80+
}
81+
else
82+
{
83+
// Check if output=json param already exists for this base URL
84+
var existingParam = await db.UrlParams.FirstOrDefaultAsync(p =>
85+
p.BaseUrlId == existingApiUrl.Id && p.Key == "output" && p.Value == "json");
86+
if (existingParam == null)
87+
{
88+
var urlParam = new UrlParam
89+
{
90+
BaseUrlId = existingApiUrl.Id,
91+
Key = "output",
92+
Value = "json",
93+
IsQueryParam = true
94+
};
95+
db.UrlParams.Add(urlParam);
96+
logger.Information("Adding output=json param to existing ApiUrl: {BaseUrl} / {PathPrefix}", existingApiUrl.Url, existingApiUrl.PathPrefix);
97+
}
98+
}
99+
}
100+
101+
await db.SaveChangesAsync();
102+
}
103+
}

0 commit comments

Comments
 (0)