fix injectionses

This commit is contained in:
john 2025-05-21 00:19:49 +02:00
parent b93115d787
commit cd078ca643
11 changed files with 119 additions and 55 deletions

View file

@ -2,6 +2,7 @@ using Femto.Api.Controllers.Posts.Dto;
using Femto.Common;
using Femto.Modules.Blog.Application;
using Femto.Modules.Blog.Application.Commands.CreatePost;
using Femto.Modules.Blog.Application.Commands.DeletePost;
using Femto.Modules.Blog.Application.Queries.GetPosts;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@ -18,7 +19,7 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
CancellationToken cancellationToken
)
{
var res = await blogModule.PostQuery(
var res = await blogModule.Query(
new GetPostsQuery(currentUserContext.CurrentUser?.Id)
{
From = searchParams.From,
@ -48,7 +49,7 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
CancellationToken cancellationToken
)
{
var guid = await blogModule.PostCommand(
var guid = await blogModule.Command(
new CreatePostCommand(
req.AuthorId,
req.Content,
@ -70,4 +71,11 @@ public class PostsController(IBlogModule blogModule, ICurrentUserContext current
return new CreatePostResponse(guid);
}
[HttpDelete("{postId}")]
[Authorize]
public async Task DeletePost(Guid postId, CancellationToken cancellationToken)
{
await blogModule.Command(new DeletePostCommand(postId, currentUserContext.CurrentUser.Id), cancellationToken);
}
}

View file

@ -4,5 +4,23 @@ namespace Femto.Api;
internal class CurrentUserContext : ICurrentUserContext
{
public CurrentUser? CurrentUser { get; set; }
}
private CurrentUser? _currentUser;
public CurrentUser? tryGetUserCurrentUser() => this._currentUser;
public bool HasUser => this._currentUser is not null;
public CurrentUser CurrentUser
{
get
{
if (_currentUser is null)
throw new InvalidOperationException(
"don't access current user if not authenticated"
);
return _currentUser;
}
set => _currentUser = value;
}
}

View file

@ -21,11 +21,16 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddDebug();
builder.Logging.SetMinimumLevel(LogLevel.Information);
var loggerFactory = LoggerFactory.Create(b =>
{
b.SetMinimumLevel(LogLevel.Information)
.AddConfiguration(builder.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug();
});
builder.Services.AddSingleton(loggerFactory);
builder.Services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
var connectionString = builder.Configuration.GetConnectionString("Database");
if (connectionString is null)
@ -39,9 +44,9 @@ if (blobStorageRoot is null)
var eventBus = new EventBus(Channel.CreateUnbounded<IEvent>());
builder.Services.AddHostedService(_ => eventBus);
builder.Services.InitializeBlogModule(connectionString, eventBus);
builder.Services.InitializeBlogModule(connectionString, eventBus, loggerFactory);
builder.Services.InitializeMediaModule(connectionString, blobStorageRoot);
builder.Services.InitializeAuthenticationModule(connectionString, eventBus);
builder.Services.InitializeAuthenticationModule(connectionString, eventBus, loggerFactory);
builder.Services.AddScoped<CurrentUserContext, CurrentUserContext>();
builder.Services.AddScoped<ICurrentUserContext>(s => s.GetRequiredService<CurrentUserContext>());

View file

@ -3,7 +3,9 @@
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
"Femto": "Debug",
"Microsoft.AspNetCore": "Warning",
"Microsoft.EntityFrameworkCore": "Warning"
}
},
"AllowedHosts": "*"