Skip to content

Commit

Permalink
move some common logic into an extension method
Browse files Browse the repository at this point in the history
  • Loading branch information
adelikat committed Nov 18, 2024
1 parent 307eeea commit b4df061
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
11 changes: 11 additions & 0 deletions TASVideos.Data/Entity/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,15 @@ public static IQueryable<User> ThatAreNotBanned(this IQueryable<User> query)
=> query.Where(u => !u.BannedUntil.HasValue || u.BannedUntil < DateTime.UtcNow);

public static bool IsBanned(this User user) => user.BannedUntil.HasValue && user.BannedUntil > DateTime.UtcNow;

public static IQueryable<SubmissionAuthor> ToSubmissionAuthors(this IQueryable<User> query, int submissionId, IList<string> authors)
=> query
.ForUsers(authors)
.Select(u => new SubmissionAuthor
{
SubmissionId = submissionId,
UserId = u.Id,
Author = u,
Ordinal = authors.IndexOf(u.UserName)
});
}
9 changes: 1 addition & 8 deletions TASVideos/Pages/Submissions/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,14 +312,7 @@ public async Task<IActionResult> OnPost()

submission.SubmissionAuthors.Clear();
submission.SubmissionAuthors.AddRange(await db.Users
.ForUsers(Submission.Authors)
.Select(u => new SubmissionAuthor
{
SubmissionId = submission.Id,
UserId = u.Id,
Author = u,
Ordinal = Submission.Authors.IndexOf(u.UserName)
})
.ToSubmissionAuthors(submission.Id, Submission.Authors)
.ToListAsync());

submission.GenerateTitle();
Expand Down
9 changes: 1 addition & 8 deletions TASVideos/Pages/Submissions/Submit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,7 @@ await wikiPages.Add(new WikiCreateRequest
});

db.SubmissionAuthors.AddRange(await db.Users
.ForUsers(Authors)
.Select(u => new SubmissionAuthor
{
SubmissionId = submission.Id,
UserId = u.Id,
Author = u,
Ordinal = Authors.IndexOf(u.UserName)
})
.ToSubmissionAuthors(submission.Id, Authors)
.ToListAsync());

submission.GenerateTitle();
Expand Down

0 comments on commit b4df061

Please sign in to comment.