Files
BeReadyBackend/BeReadyBackend/Endpoints/Posts/GetAllPostsEndpoint.cs
T
2026-05-14 11:48:20 +01:00

41 lines
1.4 KiB
C#

using BeReadyBackend.DTO.Posts;
using BeReadyBackend.Models;
using BeReadyBackend.Repositories;
using BeReadyBackend.Services;
using BeReadyBackend.Specifications.Posts;
using FastEndpoints;
namespace BeReadyBackend.Endpoints.Posts;
public class GetAllPostsEndpoint(PostsRepository postsRepository, UserService userService) : EndpointWithoutRequest<List<GetPostDto>>
{
public override void Configure()
{
Get("/Posts/");
}
public override async Task HandleAsync(CancellationToken ct)
{
int userId = userService.GetUserIdFromToken();
List<Post> posts = await postsRepository.ListAsync(new GetPostNotMeSpec(userId), ct);
List<GetPostDto> result = posts.Select(x => new GetPostDto
{
Id = x.Id,
Libelle = x.Libelle,
CreationDate = x.CreationDate,
Likes = x.Likes,
Proof = x.User?.UserRandomChallenges?
.SingleOrDefault(u =>
u.RandomChallenge?.GeneratedAt is not null
&& DateOnly.FromDateTime(u.RandomChallenge.GeneratedAt.Value) == DateOnly.FromDateTime(x.CreationDate))
?.Proof,
UserId = x.UserId,
Username = x.User?.Username,
IsLiked = x.UserPosts?.Count(y => y.UserId == userId) > 0
}).ToList();
await Send.OkAsync(result, ct);
}
}