43 lines
1.1 KiB
C#
43 lines
1.1 KiB
C#
using BlogPlatform.DTO.Comment.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BlogPlatform.Endpoints.Comment;
|
|
|
|
public class GetCommentRequest
|
|
{
|
|
public int Id { get; set; }
|
|
}
|
|
|
|
public class GetCommentEndpoint(BlogPlatformDbContext database) : Endpoint<GetCommentRequest, GetCommentDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Get("/api/comments/{@Id}", x => new {x.Id});
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(GetCommentRequest req, CancellationToken ct)
|
|
{
|
|
var comment = await database.Comments.Include(c => c.Post)!
|
|
.Include(p => p.User)
|
|
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
|
|
|
if (comment == null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
GetCommentDto responseDto = new()
|
|
{
|
|
Id = comment.Id,
|
|
Content = comment.Content,
|
|
CreatedAt = comment.CreatedAt,
|
|
UserId = comment.UserId,
|
|
PostId = comment.Post.Id
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct);
|
|
}
|
|
} |