hopefully not a horribly foolish refactoring
This commit is contained in:
parent
59d660165f
commit
1ecaf64dea
82 changed files with 782 additions and 398 deletions
|
@ -1,7 +0,0 @@
|
|||
namespace Femto.Modules.Blog.Domain.Authors;
|
||||
|
||||
internal class Author
|
||||
{
|
||||
public string Id { get; private set; } = null!;
|
||||
public string Name { get; private set; } = null!;
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Domain.Posts.Commands.CreatePost;
|
||||
|
||||
public record CreatePostCommand(Guid AuthorId, string Content, IEnumerable<Uri> Media)
|
||||
: IRequest<Guid>;
|
|
@ -1,21 +0,0 @@
|
|||
using Femto.Modules.Blog.Data;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Domain.Posts.Commands.CreatePost;
|
||||
|
||||
internal class CreatePostCommandHandler(BlogContext context)
|
||||
: IRequestHandler<CreatePostCommand, Guid>
|
||||
{
|
||||
public async Task<Guid> Handle(CreatePostCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var post = new Post(
|
||||
request.AuthorId,
|
||||
request.Content,
|
||||
request.Media.Select((url, idx) => new PostMedia(Guid.CreateVersion7(), url, idx)).ToList()
|
||||
);
|
||||
|
||||
await context.AddAsync(post, cancellationToken);
|
||||
|
||||
return post.Id;
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
namespace Femto.Modules.Blog.Domain.Posts.Commands.GetPosts.Dto;
|
||||
|
||||
public record GetPostsQueryResult(IList<PostDto> Posts, Guid? Next);
|
|
@ -1,3 +0,0 @@
|
|||
namespace Femto.Modules.Blog.Domain.Posts.Commands.GetPosts.Dto;
|
||||
|
||||
public record PostAuthorDto(Guid AuthorId, string Username);
|
|
@ -1,3 +0,0 @@
|
|||
namespace Femto.Modules.Blog.Domain.Posts.Commands.GetPosts.Dto;
|
||||
|
||||
public record PostDto(Guid PostId, string Text, IList<PostMediaDto> Media, DateTimeOffset CreatedAt, PostAuthorDto Author);
|
|
@ -1,3 +0,0 @@
|
|||
namespace Femto.Modules.Blog.Domain.Posts.Commands.GetPosts.Dto;
|
||||
|
||||
public record PostMediaDto(Uri Url);
|
|
@ -1,25 +0,0 @@
|
|||
using Femto.Modules.Blog.Domain.Posts.Commands.GetPosts.Dto;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Domain.Posts.Commands.GetPosts;
|
||||
|
||||
public class GetPostsQuery : IRequest<GetPostsQueryResult>
|
||||
{
|
||||
|
||||
public string? Username { get; init; }
|
||||
public Guid? From { get; init; }
|
||||
public int Amount { get; init; } = 20;
|
||||
public Guid? AuthorGuid { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Default is to load in reverse chronological order
|
||||
/// TODO this is not exposed on the client as it probably wouldn't work that well
|
||||
/// </summary>
|
||||
public GetPostsDirection Direction { get; init; } = GetPostsDirection.Backward;
|
||||
}
|
||||
|
||||
public enum GetPostsDirection
|
||||
{
|
||||
Forward,
|
||||
Backward
|
||||
}
|
|
@ -1,99 +0,0 @@
|
|||
using Dapper;
|
||||
using Femto.Modules.Blog.Domain.Posts.Commands.GetPosts.Dto;
|
||||
using Femto.Modules.Blog.Infrastructure.DbConnection;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Domain.Posts.Commands.GetPosts;
|
||||
|
||||
public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
||||
: IRequestHandler<GetPostsQuery, GetPostsQueryResult>
|
||||
{
|
||||
public async Task<GetPostsQueryResult> Handle(
|
||||
GetPostsQuery query,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
using var conn = connectionFactory.GetConnection();
|
||||
|
||||
if (query.Username is not null && query.AuthorGuid is not null)
|
||||
throw new ArgumentException(
|
||||
"Cannot specify both username and authorGuid",
|
||||
nameof(query)
|
||||
);
|
||||
|
||||
var orderBy = query.Direction is GetPostsDirection.Backward ? "desc" : "asc";
|
||||
var pageFilter = query.Direction is GetPostsDirection.Backward ? "<=" : ">=";
|
||||
|
||||
// lang=sql
|
||||
var sql = $$"""
|
||||
with page as (
|
||||
select blog.post.*, blog.author.username as Username, blog.author.id as AuthorId
|
||||
from blog.post
|
||||
inner join blog.author on blog.author.id = blog.post.author_id
|
||||
where (@username is null or blog.author.username = @username)
|
||||
and (@authorGuid is null or blog.author.id = @authorGuid)
|
||||
and (@cursor is null or blog.post.id {{pageFilter}} @cursor)
|
||||
order by blog.post.id {{orderBy}}
|
||||
limit @amount
|
||||
)
|
||||
select
|
||||
page.id as PostId,
|
||||
page.content as Content,
|
||||
blog.post_media.url as MediaUrl,
|
||||
page.posted_on as PostedOn,
|
||||
page.Username,
|
||||
page.AuthorId
|
||||
from page
|
||||
left join blog.post_media on blog.post_media.post_id = page.id
|
||||
order by page.id {{orderBy}}
|
||||
""";
|
||||
|
||||
var result = await conn.QueryAsync<QueryResult>(
|
||||
sql,
|
||||
new
|
||||
{
|
||||
username = query.Username,
|
||||
authorGuid = query.AuthorGuid,
|
||||
cursor = query.From,
|
||||
// load an extra one to take for the curst
|
||||
amount = query.Amount + 1,
|
||||
}
|
||||
);
|
||||
|
||||
var rows = result.ToList();
|
||||
|
||||
var posts = rows.GroupBy(row => row.PostId)
|
||||
.Select(group =>
|
||||
{
|
||||
var postId = group.Key;
|
||||
var post = group.First();
|
||||
var media = group
|
||||
.Select(row => row.MediaUrl)
|
||||
.OfType<string>()
|
||||
.Select(url => new PostMediaDto(new Uri(url)))
|
||||
.ToList();
|
||||
return new PostDto(
|
||||
postId,
|
||||
post.Content,
|
||||
media,
|
||||
post.PostedOn,
|
||||
new PostAuthorDto(post.AuthorId, post.Username)
|
||||
);
|
||||
})
|
||||
.ToList();
|
||||
|
||||
var next = rows.Count >= query.Amount ? rows.LastOrDefault()?.PostId : null;
|
||||
|
||||
return new GetPostsQueryResult(posts, next);
|
||||
}
|
||||
|
||||
internal class QueryResult
|
||||
{
|
||||
public Guid PostId { get; set; }
|
||||
public string Content { get; set; }
|
||||
public string? MediaUrl { get; set; }
|
||||
public DateTimeOffset PostedOn { get; set; }
|
||||
public Guid AuthorId { get; set; }
|
||||
public string Username { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
using Femto.Modules.Blog.Contracts.Events;
|
||||
using Femto.Modules.Blog.Domain.Posts.Events;
|
||||
using Femto.Modules.Blog.Infrastructure.Integration;
|
||||
using Femto.Modules.Blog.Infrastructure.Integration.Outbox;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Domain.Posts.Handlers;
|
||||
|
||||
internal class PostCreatedHandler(Outbox outbox) : INotificationHandler<PostCreated>
|
||||
{
|
||||
public async Task Handle(PostCreated notification, CancellationToken cancellationToken)
|
||||
{
|
||||
var post = notification.Post;
|
||||
await outbox.AddMessage(
|
||||
post.Id,
|
||||
new PostCreatedIntegrationEvent(Guid.CreateVersion7(), post.Id, post.Media.Select(m => m.Id)),
|
||||
cancellationToken
|
||||
);
|
||||
}
|
||||
}
|
|
@ -4,14 +4,20 @@ internal class PostMedia
|
|||
{
|
||||
public Guid Id { get; private set; }
|
||||
public Uri Url { get; private set; }
|
||||
public string? Type { get; private set; }
|
||||
public int Ordering { get; private set; }
|
||||
public int? Width { get; private set; }
|
||||
public int? Height { get; private set; }
|
||||
|
||||
private PostMedia() {}
|
||||
|
||||
public PostMedia(Guid id, Uri url, int ordering)
|
||||
public PostMedia(Guid id, Uri url, string type, int ordering, int? width, int? height)
|
||||
{
|
||||
this.Id = id;
|
||||
this.Url = url;
|
||||
this.Type = type;
|
||||
this.Ordering = ordering;
|
||||
this.Width = width;
|
||||
this.Height = height;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue