/
GetNewestPostsEndpoint.cs
36 lines (27 loc) · 1.11 KB
/
GetNewestPostsEndpoint.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
namespace FE.RssFeedAggregator.Features;
public class GetNewestPostsEndpoint : Endpoint<GetNewestPostsRequest, GetNewestPostsResponse>
{
private readonly AppDbContext context;
public GetNewestPostsEndpoint(AppDbContext context)
{
this.context = context;
}
public override void Configure()
{
Post("posts/newest");
}
public override async Task<GetNewestPostsResponse> ExecuteAsync(GetNewestPostsRequest req, CancellationToken ct)
{
var currentUser = User.ToTokenUser();
var posts = await context.Posts
.Where(x => x.Feed.AppUsers.Any(u => u.Id == currentUser.Id))
.OrderByDescending(x => x.PublishedAt)
.Take(req.Limit ?? 200)
.Select(x => new PostDto(x.Title, x.Description, x.PublishedAt, x.Url, x.Feed.Name))
.ToListAsync();
return new GetNewestPostsResponse(posts);
}
}
public record GetNewestPostsRequest(int? Limit);
public record GetNewestPostsResponse(List<PostDto> Posts);
public record PostDto(string Title, string Description, DateTime PublishedAt, string Url, string FeedName);