using BlogPlatform.DTO.Comment.Response;
using BlogPlatform.DTO.Post.Response;
using FastEndpoints;
using Microsoft.EntityFrameworkCore;

namespace BlogPlatform.Endpoints.Post;

public class GetAllPostEndpoint(BlogPlatformDbContext db) : EndpointWithoutRequest<List<GetPostDto>>
{
    public override void Configure()
    {
        Get("/api/posts");
    }

    public override async Task HandleAsync(CancellationToken ct)
    {
       
        List<GetPostDto> Response = await db.Posts
            .Include(p => p.User )
            .Select(post => new GetPostDto()
            {
                Id = post.Id,
                Content = post.Content,
                Likes = post.Likes,
                UserId = post.User.Id,
                UserUsername = post.User.Username,
                UserEmail = post.User.Email,
                
            } )
            .ToListAsync(ct);
        
        await Send.OkAsync(Response, ct);
      
    }
}