format, add more query extensions
This commit is contained in:
parent
f674d059fd
commit
852036a6f7
7 changed files with 52 additions and 8 deletions
|
@ -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));
|
||||
|
|
|
@ -131,7 +131,7 @@ namespace Foxnouns.Backend.Database.Migrations
|
|||
name: "ix_auth_methods_user_id",
|
||||
table: "auth_methods",
|
||||
column: "user_id");
|
||||
|
||||
|
||||
// EF Core doesn't support creating indexes on arbitrary expressions, so we have to create it manually.
|
||||
// Due to historical reasons (I made a mistake while writing the initial migration for the Go version)
|
||||
// only members have case-insensitive names.
|
||||
|
|
|
@ -8,7 +8,7 @@ public class AuthMethod : BaseModel
|
|||
|
||||
public Snowflake UserId { get; init; }
|
||||
public User User { get; init; } = null!;
|
||||
|
||||
|
||||
public Snowflake? FediverseApplicationId { get; init; }
|
||||
public FediverseApplication? FediverseApplication { get; init; }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue