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

namespace BlogPlatform.Endpoints.Comment;

public class GetAllCommentEndpoint(BlogPlatformDbContext db) : EndpointWithoutRequest<List<GetCommentDto>>
{
    public override void Configure()
    {
        Get("/api/comments");
    }

    public override async Task HandleAsync(CancellationToken ct)
    {
       
        List<GetCommentDto> Response = await db.Comments
            .Include(comment => comment.User)
            .Include(comment => comment.Post)
            .Select(comment => new GetCommentDto()
            {
                Id = comment.Id,
                Content = comment.Content,
                PostId = comment.Post.Id,
                PostTitle = comment.Post.Title,
                UserId = comment.User.Id,
                UserUsername = comment.User.Username
            })
            .ToListAsync(ct);
        
        await Send.OkAsync(Response, ct);
      
    }
}