hopefully not a horribly foolish refactoring
This commit is contained in:
parent
59d660165f
commit
1ecaf64dea
82 changed files with 782 additions and 398 deletions
11
Femto.Modules.Blog/Application/BlogApplication.cs
Normal file
11
Femto.Modules.Blog/Application/BlogApplication.cs
Normal file
|
@ -0,0 +1,11 @@
|
|||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Femto.Modules.Blog.Application;
|
||||
|
||||
public class BlogApplication(IHost host) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await host.RunAsync(stoppingToken);
|
||||
}
|
||||
}
|
18
Femto.Modules.Blog/Application/BlogContext.cs
Normal file
18
Femto.Modules.Blog/Application/BlogContext.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using Femto.Common.Infrastructure.Outbox;
|
||||
using Femto.Modules.Blog.Domain.Posts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Femto.Modules.Blog.Application;
|
||||
|
||||
internal class BlogContext(DbContextOptions<BlogContext> options) : DbContext(options), IOutboxContext
|
||||
{
|
||||
public virtual DbSet<Post> Posts { get; set; }
|
||||
public virtual DbSet<OutboxEntry> Outbox { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
base.OnModelCreating(builder);
|
||||
builder.HasDefaultSchema("blog");
|
||||
builder.ApplyConfigurationsFromAssembly(typeof(BlogContext).Assembly);
|
||||
}
|
||||
}
|
38
Femto.Modules.Blog/Application/BlogModule.cs
Normal file
38
Femto.Modules.Blog/Application/BlogModule.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using Femto.Common.Domain;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
namespace Femto.Modules.Blog.Application;
|
||||
|
||||
internal class BlogModule(IHost host) : IBlogModule
|
||||
{
|
||||
public async Task PostCommand(ICommand command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
using var scope = host.Services.CreateScope();
|
||||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
|
||||
await mediator.Send(command, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<TResponse> PostCommand<TResponse>(
|
||||
ICommand<TResponse> command,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
using var scope = host.Services.CreateScope();
|
||||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
|
||||
var response = await mediator.Send(command, cancellationToken);
|
||||
return response;
|
||||
}
|
||||
|
||||
public async Task<TResponse> PostQuery<TResponse>(
|
||||
IQuery<TResponse> query,
|
||||
CancellationToken cancellationToken = default
|
||||
)
|
||||
{
|
||||
using var scope = host.Services.CreateScope();
|
||||
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
|
||||
var response = await mediator.Send(query, cancellationToken);
|
||||
return response;
|
||||
}
|
||||
}
|
62
Femto.Modules.Blog/Application/BlogStartup.cs
Normal file
62
Femto.Modules.Blog/Application/BlogStartup.cs
Normal file
|
@ -0,0 +1,62 @@
|
|||
using Femto.Common.Infrastructure;
|
||||
using Femto.Common.Infrastructure.DbConnection;
|
||||
using Femto.Common.Infrastructure.Outbox;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Femto.Modules.Blog.Application;
|
||||
|
||||
public static class BlogStartup
|
||||
{
|
||||
public static void InitializeBlogModule(
|
||||
this IServiceCollection rootContainer,
|
||||
string connectionString
|
||||
)
|
||||
{
|
||||
var hostBuilder = Host.CreateDefaultBuilder();
|
||||
|
||||
hostBuilder.ConfigureServices(services => ConfigureServices(services, connectionString));
|
||||
|
||||
var host = hostBuilder.Build();
|
||||
|
||||
rootContainer.AddHostedService(services => new BlogApplication(host));
|
||||
|
||||
rootContainer.AddScoped<IBlogModule>(_ => new BlogModule(host));
|
||||
}
|
||||
|
||||
private static void ConfigureServices(this IServiceCollection services, string connectionString)
|
||||
{
|
||||
services.AddTransient<IDbConnectionFactory>(_ => new DbConnectionFactory(connectionString));
|
||||
|
||||
services.AddDbContext<BlogContext>(builder =>
|
||||
{
|
||||
builder.UseNpgsql(
|
||||
connectionString,
|
||||
o =>
|
||||
{
|
||||
o.MapEnum<OutboxEntryStatus>("outbox_status");
|
||||
}
|
||||
);
|
||||
builder.UseSnakeCaseNamingConvention();
|
||||
var loggerFactory = LoggerFactory.Create(b => { });
|
||||
builder.UseLoggerFactory(loggerFactory);
|
||||
builder.EnableSensitiveDataLogging();
|
||||
});
|
||||
|
||||
services.AddMediatR(c =>
|
||||
{
|
||||
c.RegisterServicesFromAssembly(typeof(BlogStartup).Assembly);
|
||||
});
|
||||
|
||||
services.AddScoped<DbContext>(s => s.GetRequiredService<BlogContext>());
|
||||
|
||||
services.AddTransient(
|
||||
typeof(IPipelineBehavior<,>),
|
||||
typeof(SaveChangesPipelineBehaviour<,>)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
using Femto.Common.Domain;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Commands.CreatePost;
|
||||
|
||||
public record CreatePostCommand(Guid AuthorId, string Content, IEnumerable<CreatePostMedia> Media)
|
||||
: ICommand<Guid>;
|
||||
|
||||
public record CreatePostMedia(Guid MediaId, Uri Url, string? Type, int Order, int? Width, int? Height);
|
|
@ -0,0 +1,30 @@
|
|||
using Femto.Modules.Blog.Domain.Posts;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.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(media => new PostMedia(
|
||||
media.MediaId,
|
||||
media.Url,
|
||||
media.Type,
|
||||
media.Order,
|
||||
media.Width,
|
||||
media.Height
|
||||
))
|
||||
.ToList()
|
||||
);
|
||||
|
||||
await context.AddAsync(post, cancellationToken);
|
||||
|
||||
return post.Id;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using Femto.Common.Infrastructure.Outbox;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Configurations;
|
||||
|
||||
internal class OutboxEntryConfiguration : IEntityTypeConfiguration<OutboxEntry>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<OutboxEntry> builder)
|
||||
{
|
||||
builder.ToTable("outbox");
|
||||
|
||||
builder.Property(x => x.Payload).HasColumnType("jsonb");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
using Femto.Modules.Blog.Domain.Posts;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Configurations;
|
||||
|
||||
internal class PostConfiguration : IEntityTypeConfiguration<Post>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Post> table)
|
||||
{
|
||||
table.ToTable("post");
|
||||
table.OwnsMany(post => post.Media).WithOwner();
|
||||
}
|
||||
}
|
18
Femto.Modules.Blog/Application/IBlogModule.cs
Normal file
18
Femto.Modules.Blog/Application/IBlogModule.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using Femto.Common.Domain;
|
||||
|
||||
namespace Femto.Modules.Blog.Application;
|
||||
|
||||
public interface IBlogModule
|
||||
{
|
||||
Task PostCommand(ICommand command, CancellationToken cancellationToken = default);
|
||||
|
||||
Task<TResponse> PostCommand<TResponse>(
|
||||
ICommand<TResponse> command,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
|
||||
Task<TResponse> PostQuery<TResponse>(
|
||||
IQuery<TResponse> query,
|
||||
CancellationToken cancellationToken = default
|
||||
);
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
||||
public record GetPostsQueryResult(IList<PostDto> Posts, Guid? Next);
|
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
||||
public record PostAuthorDto(Guid AuthorId, string Username);
|
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
||||
public record PostDto(Guid PostId, string Text, IList<PostMediaDto> Media, DateTimeOffset CreatedAt, PostAuthorDto Author);
|
|
@ -0,0 +1,3 @@
|
|||
namespace Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
||||
public record PostMediaDto(Uri Url, int? Width, int? Height);
|
|
@ -0,0 +1,25 @@
|
|||
using Femto.Common.Domain;
|
||||
using Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Queries.GetPosts;
|
||||
|
||||
public class GetPostsQuery : IQuery<GetPostsQueryResult>
|
||||
{
|
||||
public Guid? From { get; init; }
|
||||
public int Amount { get; init; } = 20;
|
||||
public Guid? AuthorId { get; init; }
|
||||
public string? Author { get; set; }
|
||||
|
||||
/// <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
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
using Dapper;
|
||||
using Femto.Common.Infrastructure.DbConnection;
|
||||
using Femto.Modules.Blog.Application.Queries.GetPosts.Dto;
|
||||
using MediatR;
|
||||
|
||||
namespace Femto.Modules.Blog.Application.Queries.GetPosts;
|
||||
|
||||
public class GetPostsQueryHandler(IDbConnectionFactory connectionFactory)
|
||||
: IRequestHandler<GetPostsQuery, GetPostsQueryResult>
|
||||
{
|
||||
public async Task<GetPostsQueryResult> Handle(
|
||||
GetPostsQuery query,
|
||||
CancellationToken cancellationToken
|
||||
)
|
||||
{
|
||||
using var conn = connectionFactory.GetConnection();
|
||||
|
||||
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,
|
||||
blog.post_media.width as MediaWidth,
|
||||
blog.post_media.height as MediaHeight,
|
||||
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.Author,
|
||||
authorGuid = query.AuthorId,
|
||||
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 =>
|
||||
{
|
||||
if (row.MediaUrl is not null)
|
||||
{
|
||||
return new PostMediaDto(
|
||||
new Uri(row.MediaUrl),
|
||||
row.MediaHeight,
|
||||
row.MediaHeight
|
||||
);
|
||||
}
|
||||
else
|
||||
return null;
|
||||
})
|
||||
.OfType<PostMediaDto>()
|
||||
.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 string? MediaType { get; set; }
|
||||
public int? MediaWidth { get; set; }
|
||||
public int? MediaHeight { get; set; }
|
||||
public DateTimeOffset PostedOn { get; set; }
|
||||
public Guid AuthorId { get; set; }
|
||||
public string Username { get; set; }
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue