feat(sleeve): /sleeve/sleeve_list returns owned ids
This commit is contained in:
@@ -10,6 +10,7 @@ using SVSim.EmulatedEntrypoint.Models.Dtos;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Sleeve;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Sleeve;
|
||||
using SVSim.EmulatedEntrypoint.Models.Dtos.Common;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Controllers;
|
||||
|
||||
@@ -152,6 +153,22 @@ public class SleeveController : SVSimController
|
||||
};
|
||||
}
|
||||
|
||||
[HttpPost("sleeve_list")]
|
||||
public async Task<ActionResult<List<SleeveListEntry>>> SleeveList(BaseRequest _, CancellationToken ct)
|
||||
{
|
||||
if (!TryGetViewerId(out long viewerId)) return Unauthorized();
|
||||
|
||||
var viewer = await _db.Viewers
|
||||
.Include(v => v.Sleeves)
|
||||
.FirstOrDefaultAsync(v => v.Id == viewerId, ct);
|
||||
if (viewer is null) return Unauthorized();
|
||||
|
||||
return viewer.Sleeves
|
||||
.Select(s => new SleeveListEntry { SleeveId = s.Id })
|
||||
.OrderBy(e => e.SleeveId)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A product is "purchased" once the viewer owns at least one of its sleeve-typed reward
|
||||
/// grants. Emblem/other grants aren't load-bearing for this check — a viewer who somehow
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
using MessagePack;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Sleeve;
|
||||
|
||||
[MessagePackObject]
|
||||
public class SleeveListEntry
|
||||
{
|
||||
[JsonPropertyName("sleeve_id")]
|
||||
[Key("sleeve_id")]
|
||||
public long SleeveId { get; set; }
|
||||
}
|
||||
54
SVSim.UnitTests/Controllers/SleeveControllerListTests.cs
Normal file
54
SVSim.UnitTests/Controllers/SleeveControllerListTests.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using NUnit.Framework;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using SVSim.Database;
|
||||
using SVSim.Database.Models;
|
||||
using SVSim.UnitTests.Infrastructure;
|
||||
|
||||
namespace SVSim.UnitTests.Controllers;
|
||||
|
||||
public class SleeveControllerListTests
|
||||
{
|
||||
[Test]
|
||||
public async Task SleeveList_returns_owned_ids_wrapped()
|
||||
{
|
||||
using var factory = new SVSimTestFactory();
|
||||
long viewerId = await factory.SeedViewerAsync(tutorialState: 0);
|
||||
|
||||
// Grant two sleeves directly. (Avoid the buy path — we want the test to
|
||||
// only exercise the read.)
|
||||
using (var scope = factory.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<SVSimDbContext>();
|
||||
var viewer = await db.Viewers.Include(v => v.Sleeves).FirstAsync(v => v.Id == viewerId);
|
||||
// SleeveEntry is a globally-shared catalog row; reuse two ids that
|
||||
// can plausibly exist or get created on the fly.
|
||||
var s100 = await db.Set<SleeveEntry>().FirstOrDefaultAsync(s => s.Id == 100)
|
||||
?? db.Set<SleeveEntry>().Add(new SleeveEntry { Id = 100 }).Entity;
|
||||
var s101 = await db.Set<SleeveEntry>().FirstOrDefaultAsync(s => s.Id == 101)
|
||||
?? db.Set<SleeveEntry>().Add(new SleeveEntry { Id = 101 }).Entity;
|
||||
viewer.Sleeves.Add(s100);
|
||||
viewer.Sleeves.Add(s101);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
using var client = factory.CreateAuthenticatedClient(viewerId);
|
||||
var requestJson = """{"viewer_id":"0","steam_id":0,"steam_session_ticket":""}""";
|
||||
var response = await client.PostAsync("/sleeve/sleeve_list",
|
||||
new StringContent(requestJson, Encoding.UTF8, "application/json"));
|
||||
|
||||
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.OK));
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync();
|
||||
using var doc = JsonDocument.Parse(body);
|
||||
var ids = doc.RootElement.EnumerateArray()
|
||||
.Select(e => e.GetProperty("sleeve_id").GetInt64())
|
||||
.ToHashSet();
|
||||
// Viewer has a default sleeve from RegisterViewer; just assert our two grants are present.
|
||||
Assert.That(ids, Does.Contain(100L));
|
||||
Assert.That(ids, Does.Contain(101L));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user