format, add more query extensions

This commit is contained in:
sam 2024-05-30 16:59:40 +02:00
parent f674d059fd
commit 852036a6f7
Signed by: sam
GPG key ID: B4EF20DDE721CAA1
7 changed files with 52 additions and 8 deletions

View file

@ -13,14 +13,15 @@ public class UsersController(DatabaseContext db, UserRendererService userRendere
public async Task<IActionResult> GetUser(string userRef)
{
var user = await db.ResolveUserAsync(userRef);
return Ok(await userRendererService.RenderUserAsync(user));
return Ok(await userRendererService.RenderUserAsync(user, selfUser: User));
}
[HttpGet("@me")]
[Authorize("identify")]
public Task<IActionResult> GetMe()
public async Task<IActionResult> GetMe()
{
throw new NotImplementedException();
var user = await db.ResolveUserAsync(User!.Id);
return Ok(await userRendererService.RenderUserAsync(user, selfUser: User));
}
[HttpPatch("@me")]

View file

@ -23,6 +23,48 @@ public static class DatabaseQueryExtensions
throw new ApiError.NotFound("No user with that ID or username found.", code: ErrorCode.UserNotFound);
}
public static async Task<User> ResolveUserAsync(this DatabaseContext context, Snowflake id)
{
var user = await context.Users
.FirstOrDefaultAsync(u => u.Id == id);
if (user != null) return user;
throw new ApiError.NotFound("No user with that ID found.", code: ErrorCode.UserNotFound);
}
public static async Task<Member> ResolveMemberAsync(this DatabaseContext context, Snowflake id)
{
var member = await context.Members
.Include(m => m.User)
.FirstOrDefaultAsync(m => m.Id == id);
if (member != null) return member;
throw new ApiError.NotFound("No member with that ID found.", code: ErrorCode.MemberNotFound);
}
public static async Task<Member> ResolveMemberAsync(this DatabaseContext context, string userRef, string memberRef)
{
var user = await context.ResolveUserAsync(userRef);
return await context.ResolveMemberAsync(user.Id, memberRef);
}
public static async Task<Member> ResolveMemberAsync(this DatabaseContext context, Snowflake userId,
string memberRef)
{
Member? member;
if (Snowflake.TryParse(memberRef, out var snowflake))
{
member = await context.Members
.Include(m => m.User)
.FirstOrDefaultAsync(m => m.Id == snowflake && m.UserId == userId);
if (member != null) return member;
}
member = await context.Members
.Include(m => m.User)
.FirstOrDefaultAsync(m => m.Name == memberRef && m.UserId == userId);
if (member != null) return member;
throw new ApiError.NotFound("No member with that ID or name found.", code: ErrorCode.MemberNotFound);
}
public static async Task<Application> GetFrontendApplicationAsync(this DatabaseContext context)
{
var app = await context.Applications.FirstOrDefaultAsync(a => a.Id == new Snowflake(0));

View file

@ -75,4 +75,5 @@ public enum ErrorCode
AuthenticationError,
GenericApiError,
UserNotFound,
MemberNotFound,
}