repo(card): SetProtected with zero-count-row preservation

Implements ICardInventoryRepository.SetProtected — loads only the
owned-cards collection (no decks/currency), guards against the EF
owned-nav Card.Id==0 default-init quirk, and accepts Count=0 rows
(destruct→re-protect round-trip). Covered by 4 new NUnit tests
(flip, unset, zero-count-row, unknown-card error). Full suite: 533/533.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
gamer147
2026-05-28 01:48:29 -04:00
parent b64123a9aa
commit ecf819ca61
2 changed files with 92 additions and 0 deletions

View File

@@ -154,4 +154,21 @@ public class CardInventoryRepository : ICardInventoryRepository
return CreateOutcome.Ok(new CreateResult(viewer.Currency.RedEther, allGrants));
}
public async Task<ProtectOutcome> SetProtected(long viewerId, long cardId, bool isProtected)
{
// Lighter load than create/destruct: only need viewer's owned-cards collection. No decks,
// no currency, no CollectionInfo.
var viewer = await _db.Viewers
.Include(v => v.Cards).ThenInclude(c => c.Card)
.FirstAsync(v => v.Id == viewerId);
var owned = viewer.Cards.FirstOrDefault(c => c.Card.Id == cardId);
if (owned is null || owned.Card.Id == 0)
return ProtectOutcome.Fail(ProtectError.UnknownCard);
owned.IsProtected = isProtected;
await _db.SaveChangesAsync();
return ProtectOutcome.Ok();
}
}