Add collector fields, including market value
Rating, notes, condition, region, purchase price and date, plus a market
value carrying the timestamp and source that make it interpretable.
Condition is load-bearing rather than cosmetic: price feeds quote per
condition, so it selects which quoted price applies to a copy. Market value
records when it was captured and where it came from — a collection total is
only as good as its staleness — and an edit to an unrelated field leaves
that timestamp alone, so a stale price cannot start looking freshly checked.
Two storage decisions worth naming:
* Money is stored as integer minor units. SQLite has no decimal type and
EF Core maps decimal to TEXT, which compares lexically: "9.00" sorts
above "10.00" and SUM is unavailable. A value converter keeps decimals
in C# while ordering and totalling work. A test pins the ordering.
* Enums serialise as names. The default is ordinals, which meant the API
rejected the browser's {"condition":"Cib"} with a 400 while the C# tests
passed, because they round-tripped ints and never spoke the client's
dialect. The tests now share the API's serializer options.
Also fixes a data-loss bug in the Python tools. Both built their PUT body
from a hardcoded list of field names, so any column added to the model was
omitted and therefore nulled. Adding collector fields meant the next art or
enrichment run would have erased every rating, note, condition, price and
valuation in the library. Payloads are now built by excluding the handful of
server-owned fields, so new columns carry through by default.
The migration was rehearsed against a copy of the live database before being
applied: 105 rows, descriptions and developers intact.
67 backend tests, 8 frontend.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -187,6 +187,33 @@ three entries. `Merge` adds and updates but never deletes; `Replace` wipes the
|
|||||||
library first and is confirmed twice in the UI. `dryRun` reports exactly what
|
library first and is confirmed twice in the UI. `dryRun` reports exactly what
|
||||||
would happen and writes nothing.
|
would happen and writes nothing.
|
||||||
|
|
||||||
|
### Collector fields
|
||||||
|
|
||||||
|
Beyond the four original flags, each game carries rating (1-10), notes,
|
||||||
|
condition, region, what you paid and when, and a current market value.
|
||||||
|
|
||||||
|
`condition` is not cosmetic: price feeds quote per condition, and the gap
|
||||||
|
between loose and sealed is routinely a multiple, so it selects which quoted
|
||||||
|
price applies to a copy.
|
||||||
|
|
||||||
|
Market value is stored with **when it was captured** and **where it came from**.
|
||||||
|
A figure with neither is not something you can reason about, and a collection
|
||||||
|
total is only as good as its staleness. Editing an unrelated field leaves the
|
||||||
|
timestamp alone; changing the figure moves it. A future price feed writes the
|
||||||
|
same three columns.
|
||||||
|
|
||||||
|
**Money is stored as integer minor units.** SQLite has no decimal type, and EF
|
||||||
|
Core's default maps `decimal` to TEXT, which compares lexically — `"9.00"` sorts
|
||||||
|
above `"10.00"`, and SUM does not work at all. A value converter keeps the C#
|
||||||
|
side as `decimal` while ordering and totalling behave.
|
||||||
|
|
||||||
|
**Enums travel as names, not ordinals.** `"Cib"` is self-describing in a payload,
|
||||||
|
an export and a log line; `2` is not, and renumbering the enum would silently
|
||||||
|
reinterpret every stored export.
|
||||||
|
|
||||||
|
New query parameters: `condition`, `region`, `minRating`, `hasValue`. New sort
|
||||||
|
keys: `rating`, `value`, `price`, `purchased`.
|
||||||
|
|
||||||
### Database changes
|
### Database changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System.ComponentModel.DataAnnotations;
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
using LudosData.Api.Domain;
|
||||||
|
|
||||||
namespace LudosData.Api.Contracts;
|
namespace LudosData.Api.Contracts;
|
||||||
|
|
||||||
/// <summary>A page of results plus the totals the paginator needs.</summary>
|
/// <summary>A page of results plus the totals the paginator needs.</summary>
|
||||||
@@ -28,6 +30,15 @@ public record GameResponse(
|
|||||||
bool Dumped,
|
bool Dumped,
|
||||||
bool Played,
|
bool Played,
|
||||||
bool Finished,
|
bool Finished,
|
||||||
|
int? Rating,
|
||||||
|
string? Notes,
|
||||||
|
GameCondition Condition,
|
||||||
|
GameRegion Region,
|
||||||
|
decimal? PurchasePrice,
|
||||||
|
DateOnly? PurchaseDate,
|
||||||
|
decimal? MarketValue,
|
||||||
|
DateTimeOffset? MarketValueUpdatedAt,
|
||||||
|
string? MarketValueSource,
|
||||||
DateTimeOffset CreatedAt,
|
DateTimeOffset CreatedAt,
|
||||||
DateTimeOffset UpdatedAt);
|
DateTimeOffset UpdatedAt);
|
||||||
|
|
||||||
@@ -52,6 +63,24 @@ public record GameRequest
|
|||||||
public bool Dumped { get; init; }
|
public bool Dumped { get; init; }
|
||||||
public bool Played { get; init; }
|
public bool Played { get; init; }
|
||||||
public bool Finished { get; init; }
|
public bool Finished { get; init; }
|
||||||
|
|
||||||
|
[Range(1, 10)] public int? Rating { get; init; }
|
||||||
|
[MaxLength(10_000)] public string? Notes { get; init; }
|
||||||
|
|
||||||
|
public GameCondition Condition { get; init; } = GameCondition.Unspecified;
|
||||||
|
public GameRegion Region { get; init; } = GameRegion.Unspecified;
|
||||||
|
|
||||||
|
[Range(0, 1_000_000)] public decimal? PurchasePrice { get; init; }
|
||||||
|
public DateOnly? PurchaseDate { get; init; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Current estimated resale value. Accepted here so a figure can be entered
|
||||||
|
/// by hand; a price feed will later write the same field, stamping
|
||||||
|
/// MarketValueUpdatedAt and MarketValueSource as it goes.
|
||||||
|
/// </summary>
|
||||||
|
[Range(0, 1_000_000)] public decimal? MarketValue { get; init; }
|
||||||
|
|
||||||
|
[MaxLength(100)] public string? MarketValueSource { get; init; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Query string for the library list, bound from [FromQuery].</summary>
|
/// <summary>Query string for the library list, bound from [FromQuery].</summary>
|
||||||
@@ -68,12 +97,24 @@ public record GameQuery
|
|||||||
public bool? Played { get; init; }
|
public bool? Played { get; init; }
|
||||||
public bool? Finished { get; init; }
|
public bool? Finished { get; init; }
|
||||||
|
|
||||||
|
public GameCondition? Condition { get; init; }
|
||||||
|
public GameRegion? Region { get; init; }
|
||||||
|
|
||||||
|
/// <summary>Lowest personal score to include. Unrated games are excluded when set.</summary>
|
||||||
|
[Range(1, 10)] public int? MinRating { get; init; }
|
||||||
|
|
||||||
|
/// <summary>Restrict to games that do, or do not, have a market value recorded.</summary>
|
||||||
|
public bool? HasValue { get; init; }
|
||||||
|
|
||||||
[Range(1, int.MaxValue)] public int Page { get; init; } = 1;
|
[Range(1, int.MaxValue)] public int Page { get; init; } = 1;
|
||||||
|
|
||||||
/// <summary>Capped at 100 to keep a hostile or buggy client from asking for everything.</summary>
|
/// <summary>Capped at 100 to keep a hostile or buggy client from asking for everything.</summary>
|
||||||
[Range(1, 100)] public int PageSize { get; init; } = 20;
|
[Range(1, 100)] public int PageSize { get; init; } = 20;
|
||||||
|
|
||||||
/// <summary>One of: title, system, genre, year, developer, publisher, created, updated.</summary>
|
/// <summary>
|
||||||
|
/// One of: title, system, genre, year, developer, publisher, rating,
|
||||||
|
/// value, price, purchased, created, updated.
|
||||||
|
/// </summary>
|
||||||
public string Sort { get; init; } = "title";
|
public string Sort { get; init; } = "title";
|
||||||
|
|
||||||
/// <summary>"asc" or "desc".</summary>
|
/// <summary>"asc" or "desc".</summary>
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
using LudosData.Api.Domain;
|
||||||
|
|
||||||
namespace LudosData.Api.Contracts;
|
namespace LudosData.Api.Contracts;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -28,6 +30,18 @@ public record ExportGame
|
|||||||
public bool Dumped { get; init; }
|
public bool Dumped { get; init; }
|
||||||
public bool Played { get; init; }
|
public bool Played { get; init; }
|
||||||
public bool Finished { get; init; }
|
public bool Finished { get; init; }
|
||||||
|
|
||||||
|
// Collector fields travel with the export; a backup that quietly dropped
|
||||||
|
// ratings, notes and valuations would not be a backup.
|
||||||
|
public int? Rating { get; init; }
|
||||||
|
public string? Notes { get; init; }
|
||||||
|
public GameCondition Condition { get; init; }
|
||||||
|
public GameRegion Region { get; init; }
|
||||||
|
public decimal? PurchasePrice { get; init; }
|
||||||
|
public DateOnly? PurchaseDate { get; init; }
|
||||||
|
public decimal? MarketValue { get; init; }
|
||||||
|
public DateTimeOffset? MarketValueUpdatedAt { get; init; }
|
||||||
|
public string? MarketValueSource { get; init; }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Envelope written by the JSON exporter.</summary>
|
/// <summary>Envelope written by the JSON exporter.</summary>
|
||||||
|
|||||||
@@ -48,6 +48,18 @@ public class GamesController(
|
|||||||
if (query.Played is { } played) q = q.Where(g => g.Played == played);
|
if (query.Played is { } played) q = q.Where(g => g.Played == played);
|
||||||
if (query.Finished is { } finished) q = q.Where(g => g.Finished == finished);
|
if (query.Finished is { } finished) q = q.Where(g => g.Finished == finished);
|
||||||
|
|
||||||
|
if (query.Condition is { } condition) q = q.Where(g => g.Condition == condition);
|
||||||
|
if (query.Region is { } region) q = q.Where(g => g.Region == region);
|
||||||
|
|
||||||
|
// An unrated game is not a zero-rated one, so it drops out of a
|
||||||
|
// minimum-rating filter rather than sorting to the bottom.
|
||||||
|
if (query.MinRating is { } minRating) q = q.Where(g => g.Rating >= minRating);
|
||||||
|
|
||||||
|
if (query.HasValue is { } hasValue)
|
||||||
|
{
|
||||||
|
q = hasValue ? q.Where(g => g.MarketValue != null) : q.Where(g => g.MarketValue == null);
|
||||||
|
}
|
||||||
|
|
||||||
var total = await q.CountAsync(ct);
|
var total = await q.CountAsync(ct);
|
||||||
|
|
||||||
q = ApplySort(q, query.Sort, query.Dir);
|
q = ApplySort(q, query.Sort, query.Dir);
|
||||||
@@ -150,6 +162,10 @@ public class GamesController(
|
|||||||
"year" => descending ? q.OrderByDescending(g => g.Year) : q.OrderBy(g => g.Year),
|
"year" => descending ? q.OrderByDescending(g => g.Year) : q.OrderBy(g => g.Year),
|
||||||
"developer" => descending ? q.OrderByDescending(g => g.Developer) : q.OrderBy(g => g.Developer),
|
"developer" => descending ? q.OrderByDescending(g => g.Developer) : q.OrderBy(g => g.Developer),
|
||||||
"publisher" => descending ? q.OrderByDescending(g => g.Publisher) : q.OrderBy(g => g.Publisher),
|
"publisher" => descending ? q.OrderByDescending(g => g.Publisher) : q.OrderBy(g => g.Publisher),
|
||||||
|
"rating" => descending ? q.OrderByDescending(g => g.Rating) : q.OrderBy(g => g.Rating),
|
||||||
|
"value" => descending ? q.OrderByDescending(g => g.MarketValue) : q.OrderBy(g => g.MarketValue),
|
||||||
|
"price" => descending ? q.OrderByDescending(g => g.PurchasePrice) : q.OrderBy(g => g.PurchasePrice),
|
||||||
|
"purchased" => descending ? q.OrderByDescending(g => g.PurchaseDate) : q.OrderBy(g => g.PurchaseDate),
|
||||||
"created" => descending ? q.OrderByDescending(g => g.CreatedAt) : q.OrderBy(g => g.CreatedAt),
|
"created" => descending ? q.OrderByDescending(g => g.CreatedAt) : q.OrderBy(g => g.CreatedAt),
|
||||||
"updated" => descending ? q.OrderByDescending(g => g.UpdatedAt) : q.OrderBy(g => g.UpdatedAt),
|
"updated" => descending ? q.OrderByDescending(g => g.UpdatedAt) : q.OrderBy(g => g.UpdatedAt),
|
||||||
_ => descending ? q.OrderByDescending(g => g.Title) : q.OrderBy(g => g.Title),
|
_ => descending ? q.OrderByDescending(g => g.Title) : q.OrderBy(g => g.Title),
|
||||||
@@ -170,10 +186,36 @@ public class GamesController(
|
|||||||
game.Dumped = request.Dumped;
|
game.Dumped = request.Dumped;
|
||||||
game.Played = request.Played;
|
game.Played = request.Played;
|
||||||
game.Finished = request.Finished;
|
game.Finished = request.Finished;
|
||||||
|
|
||||||
|
game.Rating = request.Rating;
|
||||||
|
game.Notes = request.Notes;
|
||||||
|
game.Condition = request.Condition;
|
||||||
|
game.Region = request.Region;
|
||||||
|
game.PurchasePrice = request.PurchasePrice;
|
||||||
|
game.PurchaseDate = request.PurchaseDate;
|
||||||
|
|
||||||
|
// Only stamp the valuation when the figure actually changes, so an
|
||||||
|
// unrelated edit does not make a stale price look freshly checked.
|
||||||
|
if (request.MarketValue != game.MarketValue)
|
||||||
|
{
|
||||||
|
game.MarketValue = request.MarketValue;
|
||||||
|
game.MarketValueUpdatedAt = request.MarketValue is null ? null : DateTimeOffset.UtcNow;
|
||||||
|
game.MarketValueSource = request.MarketValue is null
|
||||||
|
? null
|
||||||
|
: request.MarketValueSource?.Trim() ?? "manual";
|
||||||
|
}
|
||||||
|
else if (request.MarketValueSource is { } source && game.MarketValue is not null)
|
||||||
|
{
|
||||||
|
game.MarketValueSource = source.Trim();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private GameResponse ToResponse(Game g, string ownerId) => new(
|
private GameResponse ToResponse(Game g, string ownerId) => new(
|
||||||
g.Id, g.Title, g.System, g.Genre, g.Year, g.Developer, g.Publisher,
|
g.Id, g.Title, g.System, g.Genre, g.Year, g.Developer, g.Publisher,
|
||||||
g.Art, images.BuildUrl(ownerId, g.Art), g.Description,
|
g.Art, images.BuildUrl(ownerId, g.Art), g.Description,
|
||||||
g.Own, g.Dumped, g.Played, g.Finished, g.CreatedAt, g.UpdatedAt);
|
g.Own, g.Dumped, g.Played, g.Finished,
|
||||||
|
g.Rating, g.Notes, g.Condition, g.Region,
|
||||||
|
g.PurchasePrice, g.PurchaseDate,
|
||||||
|
g.MarketValue, g.MarketValueUpdatedAt, g.MarketValueSource,
|
||||||
|
g.CreatedAt, g.UpdatedAt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Globalization;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using LudosData.Api.Auth;
|
using LudosData.Api.Auth;
|
||||||
@@ -30,10 +31,18 @@ public class LibraryController(
|
|||||||
[
|
[
|
||||||
"title", "system", "genre", "year", "developer", "publisher",
|
"title", "system", "genre", "year", "developer", "publisher",
|
||||||
"description", "art", "own", "dumped", "played", "finished",
|
"description", "art", "own", "dumped", "played", "finished",
|
||||||
|
"rating", "notes", "condition", "region",
|
||||||
|
"purchasePrice", "purchaseDate", "marketValue", "marketValueUpdatedAt",
|
||||||
|
"marketValueSource",
|
||||||
];
|
];
|
||||||
|
|
||||||
private static readonly JsonSerializerOptions JsonOptions =
|
// Must match the converter registered on the controllers, so an export
|
||||||
new(JsonSerializerDefaults.Web) { WriteIndented = true };
|
// written with enum names is readable by the importer.
|
||||||
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
Converters = { new System.Text.Json.Serialization.JsonStringEnumConverter() },
|
||||||
|
};
|
||||||
|
|
||||||
// ---- export ----------------------------------------------------------
|
// ---- export ----------------------------------------------------------
|
||||||
|
|
||||||
@@ -56,6 +65,16 @@ public class LibraryController(
|
|||||||
g.Title, g.System, g.Genre, g.Year, g.Developer, g.Publisher,
|
g.Title, g.System, g.Genre, g.Year, g.Developer, g.Publisher,
|
||||||
g.Description, g.Art,
|
g.Description, g.Art,
|
||||||
g.Own.ToString(), g.Dumped.ToString(), g.Played.ToString(), g.Finished.ToString(),
|
g.Own.ToString(), g.Dumped.ToString(), g.Played.ToString(), g.Finished.ToString(),
|
||||||
|
g.Rating?.ToString(), g.Notes,
|
||||||
|
g.Condition == GameCondition.Unspecified ? null : g.Condition.ToString(),
|
||||||
|
g.Region == GameRegion.Unspecified ? null : g.Region.ToString(),
|
||||||
|
// Invariant culture throughout: a comma decimal separator would
|
||||||
|
// collide with the delimiter, and dates must not depend on locale.
|
||||||
|
g.PurchasePrice?.ToString(CultureInfo.InvariantCulture),
|
||||||
|
g.PurchaseDate?.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
|
||||||
|
g.MarketValue?.ToString(CultureInfo.InvariantCulture),
|
||||||
|
g.MarketValueUpdatedAt?.ToString("O", CultureInfo.InvariantCulture),
|
||||||
|
g.MarketValueSource,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// A BOM keeps Excel from mangling non-ASCII titles such as Pokémon.
|
// A BOM keeps Excel from mangling non-ASCII titles such as Pokémon.
|
||||||
@@ -265,12 +284,48 @@ public class LibraryController(
|
|||||||
Dumped = Flag(row, "dumped"),
|
Dumped = Flag(row, "dumped"),
|
||||||
Played = Flag(row, "played"),
|
Played = Flag(row, "played"),
|
||||||
Finished = Flag(row, "finished"),
|
Finished = Flag(row, "finished"),
|
||||||
|
|
||||||
|
Rating = ParseInt(Field(row, "rating")),
|
||||||
|
Notes = Field(row, "notes"),
|
||||||
|
Condition = ParseEnum<GameCondition>(Field(row, "condition")),
|
||||||
|
Region = ParseEnum<GameRegion>(Field(row, "region")),
|
||||||
|
PurchasePrice = ParseMoney(Field(row, "purchaseprice")),
|
||||||
|
PurchaseDate = ParseDate(Field(row, "purchasedate")),
|
||||||
|
MarketValue = ParseMoney(Field(row, "marketvalue")),
|
||||||
|
MarketValueUpdatedAt = ParseTimestamp(Field(row, "marketvalueupdatedat")),
|
||||||
|
MarketValueSource = Field(row, "marketvaluesource"),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return games;
|
return games;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parsers are forgiving: a spreadsheet round-trip is a normal way for these
|
||||||
|
// files to arrive, and one unreadable cell should not cost the whole row.
|
||||||
|
private static int? ParseInt(string? value) =>
|
||||||
|
int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed)
|
||||||
|
? parsed : null;
|
||||||
|
|
||||||
|
private static decimal? ParseMoney(string? value)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(value)) return null;
|
||||||
|
// Tolerate a currency symbol and thousands separators from a spreadsheet.
|
||||||
|
var cleaned = value.Trim().TrimStart('$', '£', '€').Replace(",", string.Empty);
|
||||||
|
return decimal.TryParse(cleaned, NumberStyles.Number, CultureInfo.InvariantCulture, out var parsed)
|
||||||
|
? parsed : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DateOnly? ParseDate(string? value) =>
|
||||||
|
DateOnly.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out var parsed)
|
||||||
|
? parsed : null;
|
||||||
|
|
||||||
|
private static DateTimeOffset? ParseTimestamp(string? value) =>
|
||||||
|
DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out var parsed)
|
||||||
|
? parsed : null;
|
||||||
|
|
||||||
|
private static T ParseEnum<T>(string? value) where T : struct, Enum =>
|
||||||
|
Enum.TryParse<T>(value, ignoreCase: true, out var parsed) ? parsed : default;
|
||||||
|
|
||||||
private static string Key(string title, string? system) =>
|
private static string Key(string title, string? system) =>
|
||||||
$"{title.Trim().ToLowerInvariant()} | |||||||