This commit is contained in:
john 2025-05-04 00:57:27 +02:00
parent ab2e20f7e1
commit befaa207d7
23 changed files with 244 additions and 95 deletions

View file

@ -1,5 +1,5 @@
using Femto.Api.Controllers.Authors.Dto;
using Femto.Modules.Blog.Domain.Posts.Commands.GetAuthorPosts;
using Femto.Modules.Blog.Domain.Posts.Commands.GetPosts;
using MediatR;
using Microsoft.AspNetCore.Mvc;
@ -8,21 +8,33 @@ namespace Femto.Api.Controllers.Authors;
[ApiController]
[Route("authors")]
public class AuthorsController(IMediator mediator) : ControllerBase
{
[HttpGet("{authorId}/posts")]
{
[HttpGet("{username}/posts")]
public async Task<ActionResult<GetAuthorPostsResponse>> GetAuthorPosts(
Guid authorId,
string username,
[FromQuery] GetAuthorPostsSearchParams searchParams,
CancellationToken cancellationToken
)
{
var posts = await mediator.Send(
new GetAuthorPostsQuery(authorId, searchParams.Cursor, searchParams.Count),
var res = await mediator.Send(
new GetPostsQuery
{
Username = username,
Amount = searchParams.Amount ?? 20,
From = searchParams.From,
},
cancellationToken
);
return new GetAuthorPostsResponse(
posts.Select(p => new AuthorPostDto(p.PostId, p.Text, p.Media.Select(m => m.Url)))
res.Posts.Select(p => new AuthorPostDto(
p.PostId,
p.Text,
p.Media.Select(m => m.Url),
p.CreatedAt,
new AuthoPostAuthorDto(p.Author.AuthorId, p.Author.Username)
)),
res.Next
);
}
}

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Authors.Dto;
[PublicAPI]
public record AuthoPostAuthorDto(Guid AuthorId, string Username);

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Authors.Dto;
[PublicAPI]
public record AuthorPostDto(Guid PostId, string Content, IEnumerable<Uri> Media, DateTime CreatedAt, AuthoPostAuthorDto Author );

View file

@ -3,7 +3,4 @@ using JetBrains.Annotations;
namespace Femto.Api.Controllers.Authors.Dto;
[PublicAPI]
public record GetAuthorPostsResponse(IEnumerable<AuthorPostDto> Posts);
[PublicAPI]
public record AuthorPostDto(Guid PostId, string Content, IEnumerable<Uri> Media);
public record GetAuthorPostsResponse(IEnumerable<AuthorPostDto> Posts, Guid? Next);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Authors;
public record GetAuthorPostsSearchParams(Guid? From, int? Amount);

View file

@ -1,3 +0,0 @@
namespace Femto.Api.Controllers.Authors;
public record GetAuthorPostsSearchParams(Guid? Cursor, int? Count);

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record GetAllPublicPostsResponse(IEnumerable<PublicPostDto> Posts, Guid? Next);

View file

@ -1,6 +0,0 @@
namespace Femto.Api.Controllers.Posts.Dto;
public record GetPostResponse
{
}

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record GetPublicPostsSearchParams(Guid? From, int? Amount);

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record PublicPostAuthorDto(Guid AuthorId, string Username);

View file

@ -0,0 +1,12 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record PublicPostDto(
PublicPostAuthorDto Author,
Guid PostId,
string Content,
IEnumerable<Uri> Media,
DateTime CreatedAt
);

View file

@ -1,5 +1,6 @@
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;
@ -9,6 +10,33 @@ namespace Femto.Api.Controllers.Posts;
[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,
@ -19,7 +47,7 @@ public class PostsController(IMediator mediator) : ControllerBase
new CreatePostCommand(req.AuthorId, req.Content, req.Media),
cancellationToken
);
return new CreatePostResponse(guid);
}
}