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