femto-backend/Femto.Api/Controllers/Posts/PostsController.cs
2025-05-04 00:57:27 +02:00

53 lines
1.5 KiB
C#

using Femto.Api.Controllers.Posts.Dto;
using Femto.Modules.Blog.Domain.Posts.Commands.CreatePost;
using Femto.Modules.Blog.Domain.Posts.Commands.GetPosts;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Femto.Api.Controllers.Posts;
[ApiController]
[Route("posts")]
public class PostsController(IMediator mediator) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<GetAllPublicPostsResponse>> GetAllPublicPosts(
[FromQuery] GetPublicPostsSearchParams searchParams,
CancellationToken cancellationToken
)
{
var res = await mediator.Send(
new GetPostsQuery
{
From = searchParams.From,
Amount = searchParams.Amount ?? 20
},
cancellationToken
);
return new GetAllPublicPostsResponse(
res.Posts.Select(p => new PublicPostDto(
new PublicPostAuthorDto(p.Author.AuthorId, p.Author.Username),
p.PostId,
p.Text,
p.Media.Select(m => m.Url),
p.CreatedAt
)),
res.Next
);
}
[HttpPost]
public async Task<ActionResult<CreatePostResponse>> Post(
[FromBody] CreatePostRequest req,
CancellationToken cancellationToken
)
{
var guid = await mediator.Send(
new CreatePostCommand(req.AuthorId, req.Content, req.Media),
cancellationToken
);
return new CreatePostResponse(guid);
}
}