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

This commit is contained in:
sam 2024-12-25 14:33:42 -05:00
parent e908e67ca6
commit 78afb8b9c4
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
2 changed files with 43 additions and 6 deletions

View file

@ -12,15 +12,21 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
using Foxnouns.Backend.Database;
using Foxnouns.Backend.Database.Models; using Foxnouns.Backend.Database.Models;
using Foxnouns.Backend.Dto.V1;
using Foxnouns.Backend.Services.V1; using Foxnouns.Backend.Services.V1;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace Foxnouns.Backend.Controllers.V1; namespace Foxnouns.Backend.Controllers.V1;
[Route("/api/v1")] [Route("/api/v1")]
public class UsersV1Controller(UsersV1Service usersV1Service, MembersV1Service membersV1Service) public class UsersV1Controller(
: ApiControllerBase UsersV1Service usersV1Service,
MembersV1Service membersV1Service,
DatabaseContext db
) : ApiControllerBase
{ {
[HttpGet("users/{userRef}")] [HttpGet("users/{userRef}")]
public async Task<IActionResult> GetUserAsync(string userRef, CancellationToken ct = default) public async Task<IActionResult> GetUserAsync(string userRef, CancellationToken ct = default)
@ -50,4 +56,33 @@ public class UsersV1Controller(UsersV1Service usersV1Service, MembersV1Service m
) )
); );
} }
[HttpGet("users/{userRef}/members")]
public async Task<IActionResult> GetUserMembersAsync(
string userRef,
CancellationToken ct = default
)
{
User user = await usersV1Service.ResolveUserAsync(userRef, CurrentToken, ct);
List<Member> members = await db
.Members.Where(m => m.UserId == user.Id)
.OrderBy(m => m.Name)
.ToListAsync(ct);
List<MemberResponse> responses = [];
foreach (Member member in members)
{
responses.Add(
await membersV1Service.RenderMemberAsync(
member,
CurrentToken,
user,
renderFlags: true,
ct: ct
)
);
}
return Ok(responses);
}
} }

View file

@ -47,10 +47,12 @@ public class MembersV1Service(DatabaseContext db)
public async Task<MemberResponse> RenderMemberAsync( public async Task<MemberResponse> RenderMemberAsync(
Member m, Member m,
Token? token = default, Token? token = default,
User? user = null,
bool renderFlags = true, bool renderFlags = true,
CancellationToken ct = default CancellationToken ct = default
) )
{ {
user ??= m.User;
bool renderUnlisted = m.UserId == token?.UserId; bool renderUnlisted = m.UserId == token?.UserId;
List<MemberFlag> flags = renderFlags List<MemberFlag> flags = renderFlags
@ -66,9 +68,9 @@ public class MembersV1Service(DatabaseContext db)
m.Bio, m.Bio,
m.Avatar, m.Avatar,
m.Links, m.Links,
Names: FieldEntry.FromEntries(m.Names, m.User.CustomPreferences), Names: FieldEntry.FromEntries(m.Names, user.CustomPreferences),
Pronouns: PronounEntry.FromPronouns(m.Pronouns, m.User.CustomPreferences), Pronouns: PronounEntry.FromPronouns(m.Pronouns, user.CustomPreferences),
Fields: ProfileField.FromFields(m.Fields, m.User.CustomPreferences), Fields: ProfileField.FromFields(m.Fields, user.CustomPreferences),
Flags: flags Flags: flags
.Where(f => f.PrideFlag.Hash != null) .Where(f => f.PrideFlag.Hash != null)
.Select(f => new PrideFlag( .Select(f => new PrideFlag(
@ -79,7 +81,7 @@ public class MembersV1Service(DatabaseContext db)
f.PrideFlag.Description f.PrideFlag.Description
)) ))
.ToArray(), .ToArray(),
User: UsersV1Service.RenderPartialUser(m.User), User: UsersV1Service.RenderPartialUser(user),
Unlisted: renderUnlisted ? m.Unlisted : null Unlisted: renderUnlisted ? m.Unlisted : null
); );
} }