Files
jobtrackingapp/JobTrackerApi/Models/CoverLetterDocumentSettings.cs
T

61 lines
3.0 KiB
C#

using System.Text.Json;
namespace JobTrackerApi.Models;
// A reviewed snapshot, never a live link to profile or CV settings.
public sealed class CoverLetterDocumentSettings
{
public int SchemaVersion { get; set; } = 1;
public string SenderName { get; set; } = "";
public string SenderTitle { get; set; } = "";
public string SenderEmail { get; set; } = "";
public string SenderPhone { get; set; } = "";
public string SenderLocation { get; set; } = "";
public string Recipient { get; set; } = "";
public string Subject { get; set; } = "";
public string Date { get; set; } = "";
public string ThemeId { get; set; } = "modern";
public string? AccentColor { get; set; }
public string? BodyFont { get; set; }
public double? BaseFontSizePt { get; set; }
public double? LineHeight { get; set; }
public double? PageMarginMm { get; set; }
public string PageSize { get; set; } = "a4";
public int? SourceCvId { get; set; }
}
public static class CoverLetterDocumentSettingsJson
{
private static readonly JsonSerializerOptions Options = new(JsonSerializerDefaults.Web);
public static string Serialize(CoverLetterDocumentSettings? settings) => JsonSerializer.Serialize(Normalize(settings), Options);
public static CoverLetterDocumentSettings Deserialize(string? json)
{
try { return Normalize(string.IsNullOrWhiteSpace(json) ? null : JsonSerializer.Deserialize<CoverLetterDocumentSettings>(json, Options)); }
catch (JsonException) { return Normalize(null); }
}
public static CoverLetterDocumentSettings Normalize(CoverLetterDocumentSettings? value)
{
value ??= new();
var theme = CvThemeCatalog.Resolve(value.ThemeId);
var style = CvVariantSettingsJson.Normalize(new()
{
AccentColor = value.AccentColor, BodyFont = value.BodyFont,
BaseFontSizePt = value.BaseFontSizePt, LineHeight = value.LineHeight, PageMarginMm = value.PageMarginMm,
});
static string Text(string? text, int limit) => (text ?? "")[..Math.Min(text?.Length ?? 0, limit)];
return new()
{
SchemaVersion = 1, ThemeId = theme.Id,
SenderName = Text(value.SenderName, 300), SenderTitle = Text(value.SenderTitle, 300),
SenderEmail = Text(value.SenderEmail, 300), SenderPhone = Text(value.SenderPhone, 100), SenderLocation = Text(value.SenderLocation, 300),
Recipient = Text(value.Recipient, 2000), Subject = Text(value.Subject, 500), Date = Text(value.Date, 100),
AccentColor = style.AccentColor ?? theme.Accent, BodyFont = style.BodyFont ?? theme.BodyFont,
BaseFontSizePt = style.BaseFontSizePt ?? theme.BodySizePt, LineHeight = style.LineHeight ?? theme.LineHeight,
PageMarginMm = style.PageMarginMm ?? theme.PageMarginMm,
PageSize = string.Equals(value.PageSize, "letter", StringComparison.OrdinalIgnoreCase) ? "letter" : "a4",
SourceCvId = value.SourceCvId is > 0 ? value.SourceCvId : null,
};
}
}