From 78afb8b9c463097745d754796e0fbf2e0c5c4fa3 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 25 Dec 2024 14:33:42 -0500 Subject: [PATCH] feat: GET /api/v1/users/{userRef}/members --- .../Controllers/V1/UsersV1Controller.cs | 39 ++++++++++++++++++- .../Services/V1/MembersV1Service.cs | 10 +++-- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Foxnouns.Backend/Controllers/V1/UsersV1Controller.cs b/Foxnouns.Backend/Controllers/V1/UsersV1Controller.cs index 8433689..51e2b17 100644 --- a/Foxnouns.Backend/Controllers/V1/UsersV1Controller.cs +++ b/Foxnouns.Backend/Controllers/V1/UsersV1Controller.cs @@ -12,15 +12,21 @@ // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +using Foxnouns.Backend.Database; using Foxnouns.Backend.Database.Models; +using Foxnouns.Backend.Dto.V1; using Foxnouns.Backend.Services.V1; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; namespace Foxnouns.Backend.Controllers.V1; [Route("/api/v1")] -public class UsersV1Controller(UsersV1Service usersV1Service, MembersV1Service membersV1Service) - : ApiControllerBase +public class UsersV1Controller( + UsersV1Service usersV1Service, + MembersV1Service membersV1Service, + DatabaseContext db +) : ApiControllerBase { [HttpGet("users/{userRef}")] public async Task GetUserAsync(string userRef, CancellationToken ct = default) @@ -50,4 +56,33 @@ public class UsersV1Controller(UsersV1Service usersV1Service, MembersV1Service m ) ); } + + [HttpGet("users/{userRef}/members")] + public async Task GetUserMembersAsync( + string userRef, + CancellationToken ct = default + ) + { + User user = await usersV1Service.ResolveUserAsync(userRef, CurrentToken, ct); + List members = await db + .Members.Where(m => m.UserId == user.Id) + .OrderBy(m => m.Name) + .ToListAsync(ct); + + List responses = []; + foreach (Member member in members) + { + responses.Add( + await membersV1Service.RenderMemberAsync( + member, + CurrentToken, + user, + renderFlags: true, + ct: ct + ) + ); + } + + return Ok(responses); + } } diff --git a/Foxnouns.Backend/Services/V1/MembersV1Service.cs b/Foxnouns.Backend/Services/V1/MembersV1Service.cs index 5033e7f..b11a510 100644 --- a/Foxnouns.Backend/Services/V1/MembersV1Service.cs +++ b/Foxnouns.Backend/Services/V1/MembersV1Service.cs @@ -47,10 +47,12 @@ public class MembersV1Service(DatabaseContext db) public async Task RenderMemberAsync( Member m, Token? token = default, + User? user = null, bool renderFlags = true, CancellationToken ct = default ) { + user ??= m.User; bool renderUnlisted = m.UserId == token?.UserId; List flags = renderFlags @@ -66,9 +68,9 @@ public class MembersV1Service(DatabaseContext db) m.Bio, m.Avatar, m.Links, - Names: FieldEntry.FromEntries(m.Names, m.User.CustomPreferences), - Pronouns: PronounEntry.FromPronouns(m.Pronouns, m.User.CustomPreferences), - Fields: ProfileField.FromFields(m.Fields, m.User.CustomPreferences), + Names: FieldEntry.FromEntries(m.Names, user.CustomPreferences), + Pronouns: PronounEntry.FromPronouns(m.Pronouns, user.CustomPreferences), + Fields: ProfileField.FromFields(m.Fields, user.CustomPreferences), Flags: flags .Where(f => f.PrideFlag.Hash != null) .Select(f => new PrideFlag( @@ -79,7 +81,7 @@ public class MembersV1Service(DatabaseContext db) f.PrideFlag.Description )) .ToArray(), - User: UsersV1Service.RenderPartialUser(m.User), + User: UsersV1Service.RenderPartialUser(user), Unlisted: renderUnlisted ? m.Unlisted : null ); }