refactor(bootstrap): migrate payment items to seed file
Lifts ImportPaymentItems out of GlobalsImporter into a dedicated PaymentItemImporter driven by Data/seeds/payment-items.json. Wired into Program.cs and SVSimTestFactory.SeedGlobalsAsync after PracticeOpponentImporter. Drops the prod-capture file in favor of the extractor pipeline. Canonical 4-test suite (basic, idempotent, leave-untouched, skip-zero) keeps the dict-in-sync upsert pattern Task 2 established. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
69
SVSim.Bootstrap/Importers/PaymentItemImporter.cs
Normal file
69
SVSim.Bootstrap/Importers/PaymentItemImporter.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using System.Globalization;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using SVSim.Bootstrap.Models.Seed;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Models;
|
||||
|
||||
namespace SVSim.Bootstrap.Importers;
|
||||
|
||||
/// <summary>
|
||||
/// Idempotent upsert of Steam payment items from <c>seeds/payment-items.json</c>.
|
||||
/// Rows missing from the seed are LEFT INTACT.
|
||||
/// </summary>
|
||||
public class PaymentItemImporter
|
||||
{
|
||||
public async Task<int> ImportAsync(SVSimDbContext context, string seedDir)
|
||||
{
|
||||
string path = Path.Combine(seedDir, "payment-items.json");
|
||||
var seed = SeedLoader.LoadList<PaymentItemSeed>(path);
|
||||
if (seed.Count == 0)
|
||||
{
|
||||
Console.WriteLine("[PaymentItemImporter] No seed rows; skipping.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
var existing = await context.PaymentItems.ToDictionaryAsync(e => e.Id);
|
||||
int created = 0, updated = 0;
|
||||
|
||||
foreach (var s in seed)
|
||||
{
|
||||
if (s.RecordId == 0) continue;
|
||||
|
||||
var entry = existing.TryGetValue(s.RecordId, out var ex)
|
||||
? ex : new PaymentItemEntry { Id = s.RecordId };
|
||||
|
||||
entry.ProductId = s.ProductId;
|
||||
entry.StoreProductId = s.StoreProductId;
|
||||
entry.Name = s.Name;
|
||||
entry.Text = s.Text;
|
||||
entry.Price = decimal.TryParse(s.Price, NumberStyles.Number, CultureInfo.InvariantCulture, out var d) ? d : 0m;
|
||||
entry.ChargeCrystalNum = s.ChargeCrystalNum;
|
||||
entry.FreeCrystalNum = s.FreeCrystalNum;
|
||||
entry.PurchaseLimit = s.PurchaseLimit;
|
||||
entry.SpecialShopFlag = s.SpecialShopFlag;
|
||||
entry.ImageName = s.ImageName;
|
||||
entry.StartTime = ParseDate(s.StartTime);
|
||||
entry.EndTime = ParseDate(s.EndTime);
|
||||
entry.RemainingTime = s.RemainingTime;
|
||||
entry.IsResaleProduct = s.IsResaleProduct;
|
||||
entry.ResaleStartDate = string.IsNullOrWhiteSpace(s.ResaleStartDate) ? null : ParseDate(s.ResaleStartDate);
|
||||
|
||||
if (ex is null)
|
||||
{
|
||||
context.PaymentItems.Add(entry);
|
||||
existing[s.RecordId] = entry;
|
||||
created++;
|
||||
}
|
||||
else updated++;
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
Console.WriteLine($"[PaymentItemImporter] +{created}/~{updated}");
|
||||
return created + updated;
|
||||
}
|
||||
|
||||
private static DateTime ParseDate(string s) =>
|
||||
DateTime.TryParse(s, CultureInfo.InvariantCulture,
|
||||
DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal,
|
||||
out var dt) ? dt : DateTime.MinValue;
|
||||
}
|
||||
Reference in New Issue
Block a user