feat: GET /api/v1/users/{userRef}/members/{memberRef}

This commit is contained in:
sam 2024-12-25 14:53:36 -05:00
parent 78afb8b9c4
commit 478ba2a406
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
2 changed files with 62 additions and 2 deletions

View file

@ -22,7 +22,7 @@ using Microsoft.EntityFrameworkCore;
namespace Foxnouns.Backend.Controllers.V1;
[Route("/api/v1")]
public class UsersV1Controller(
public class V1ReadController(
UsersV1Service usersV1Service,
MembersV1Service membersV1Service,
DatabaseContext db
@ -85,4 +85,27 @@ public class UsersV1Controller(
return Ok(responses);
}
[HttpGet("users/{userRef}/members/{memberRef}")]
public async Task<IActionResult> GetUserMemberAsync(
string userRef,
string memberRef,
CancellationToken ct = default
)
{
Member member = await membersV1Service.ResolveMemberAsync(
userRef,
memberRef,
CurrentToken,
ct
);
return Ok(
await membersV1Service.RenderMemberAsync(
member,
CurrentToken,
renderFlags: true,
ct: ct
)
);
}
}

View file

@ -21,7 +21,7 @@ using PrideFlag = Foxnouns.Backend.Dto.V1.PrideFlag;
namespace Foxnouns.Backend.Services.V1;
public class MembersV1Service(DatabaseContext db)
public class MembersV1Service(DatabaseContext db, UsersV1Service usersV1Service)
{
public async Task<Member> ResolveMemberAsync(string id, CancellationToken ct = default)
{
@ -44,6 +44,43 @@ public class MembersV1Service(DatabaseContext db)
throw new ApiError.NotFound("No member with that ID found.", ErrorCode.MemberNotFound);
}
public async Task<Member> ResolveMemberAsync(
string userRef,
string memberRef,
Token? token,
CancellationToken ct = default
)
{
User user = await usersV1Service.ResolveUserAsync(userRef, token, ct);
Member? member;
if (Snowflake.TryParse(memberRef, out Snowflake? sf))
{
member = await db
.Members.Include(m => m.User)
.FirstOrDefaultAsync(m => m.Id == sf && m.UserId == user.Id, ct);
if (member != null)
return member;
}
member = await db
.Members.Include(m => m.User)
.FirstOrDefaultAsync(m => m.LegacyId == memberRef && m.UserId == user.Id, ct);
if (member != null)
return member;
member = await db
.Members.Include(m => m.User)
.FirstOrDefaultAsync(m => m.Name == memberRef && m.UserId == user.Id, ct);
if (member != null)
return member;
throw new ApiError.NotFound(
"No member with that ID or name found.",
ErrorCode.MemberNotFound
);
}
public async Task<MemberResponse> RenderMemberAsync(
Member m,
Token? token = default,