refactor post reactions
This commit is contained in:
parent
2519fc77d2
commit
5379d29c5f
13 changed files with 129 additions and 97 deletions
|
@ -25,7 +25,7 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
|
|||
var res = await blogModule.Query(
|
||||
new GetPostsQuery(currentUserContext.CurrentUser?.Id)
|
||||
{
|
||||
From = searchParams.From,
|
||||
After = searchParams.From,
|
||||
Amount = searchParams.Amount ?? 20,
|
||||
AuthorId = searchParams.AuthorId,
|
||||
Author = searchParams.Author,
|
||||
|
@ -33,18 +33,7 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
|
|||
cancellationToken
|
||||
);
|
||||
|
||||
return new LoadPostsResponse(
|
||||
res.Posts.Select(p => new PostDto(
|
||||
new PostAuthorDto(p.Author.AuthorId, p.Author.Username),
|
||||
p.PostId,
|
||||
p.Text,
|
||||
p.Media.Select(m => new PostMediaDto(m.Url, m.Width, m.Height)),
|
||||
p.Reactions.Select(r => new PostReactionDto(r.Emoji, r.Count, r.DidReact)),
|
||||
p.CreatedAt,
|
||||
p.PossibleReactions
|
||||
)),
|
||||
res.Next
|
||||
);
|
||||
return new LoadPostsResponse(res.Posts.Select(PostDto.FromModel));
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
|
@ -75,17 +64,26 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
|
|||
cancellationToken
|
||||
);
|
||||
|
||||
return new CreatePostResponse(
|
||||
new PostDto(
|
||||
new PostAuthorDto(post.Author.AuthorId, post.Author.Username),
|
||||
post.PostId,
|
||||
post.Text,
|
||||
post.Media.Select(m => new PostMediaDto(m.Url, m.Width, m.Height)),
|
||||
post.Reactions.Select(r => new PostReactionDto(r.Emoji, r.Count, r.DidReact)).ToList(),
|
||||
post.CreatedAt,
|
||||
post.PossibleReactions
|
||||
)
|
||||
return new CreatePostResponse(PostDto.FromModel(post));
|
||||
}
|
||||
|
||||
[HttpGet("{postId}")]
|
||||
public async Task<ActionResult<GetPostResponse>> GetPost(
|
||||
Guid postId,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var result = await blogModule.Query(
|
||||
new GetPostsQuery(postId, currentUserContext.CurrentUser?.Id),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
var post = result.Posts.SingleOrDefault();
|
||||
|
||||
if (post is null)
|
||||
return NotFound();
|
||||
|
||||
return new GetPostResponse(PostDto.FromModel(post));
|
||||
}
|
||||
|
||||
[HttpDelete("{postId}")]
|
||||
|
@ -100,24 +98,37 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
|
|||
|
||||
[HttpPost("{postId}/reactions")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> AddPostReaction(Guid postId, [FromBody] AddPostReactionRequest request, CancellationToken cancellationToken)
|
||||
public async Task<ActionResult> AddPostReaction(
|
||||
Guid postId,
|
||||
[FromBody] AddPostReactionRequest request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var currentUser = currentUserContext.CurrentUser!;
|
||||
|
||||
await blogModule.Command(new AddPostReactionCommand(postId, request.Emoji, currentUser.Id), cancellationToken);
|
||||
await blogModule.Command(
|
||||
new AddPostReactionCommand(postId, request.Emoji, currentUser.Id),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
|
||||
|
||||
[HttpDelete("{postId}/reactions")]
|
||||
[Authorize]
|
||||
public async Task<ActionResult> DeletePostReaction(Guid postId, [FromBody] DeletePostReactionRequest request, CancellationToken cancellationToken)
|
||||
public async Task<ActionResult> DeletePostReaction(
|
||||
Guid postId,
|
||||
[FromBody] DeletePostReactionRequest request,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
var currentUser = currentUserContext.CurrentUser!;
|
||||
|
||||
await blogModule.Command(new ClearPostReactionCommand(postId, request.Emoji, currentUser.Id), cancellationToken);
|
||||
await blogModule.Command(
|
||||
new ClearPostReactionCommand(postId, request.Emoji, currentUser.Id),
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
return this.Ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue