hopefully not a horribly foolish refactoring

This commit is contained in:
john 2025-05-11 23:26:09 +02:00
parent 59d660165f
commit 1ecaf64dea
82 changed files with 782 additions and 398 deletions

View file

@ -0,0 +1,27 @@
using Microsoft.AspNetCore.Mvc;
namespace Femto.Api.Controllers.Auth;
[ApiController]
[Route("auth")]
public class AuthController : ControllerBase
{
[HttpPost("login")]
public async Task<ActionResult<LoginResponse>> Login([FromBody] LoginRequest request)
{
return new LoginResponse(Guid.Parse("0196960c-6296-7532-ba66-8fabb38c6ae0"), "johnbotris", "token");
}
[HttpPost("signup")]
public async Task<ActionResult<SignupResponse>> Signup([FromBody] SignupRequest request)
{
throw new NotImplementedException();
}
[HttpPost("delete-session")]
public async Task<ActionResult> DeleteSession([FromBody] DeleteSessionRequest request)
{
// TODO
return Ok(new {});
}
}

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Auth;
public record DeleteSessionRequest(string SessionToken);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Auth;
public record LoginRequest(string Username, string Password);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Auth;
public record LoginResponse(Guid UserId, string Username, string SessionToken);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Auth;
public record SignupRequest(string Username, string Password, string SignupCode, string? Email);

View file

@ -0,0 +1,3 @@
namespace Femto.Api.Controllers.Auth;
public record SignupResponse(Guid UserId, string Username, string SessionToken);

View file

@ -1,40 +0,0 @@
using Femto.Api.Controllers.Authors.Dto;
using Femto.Modules.Blog.Domain.Posts.Commands.GetPosts;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Femto.Api.Controllers.Authors;
[ApiController]
[Route("authors")]
public class AuthorsController(IMediator mediator) : ControllerBase
{
[HttpGet("{username}/posts")]
public async Task<ActionResult<GetAuthorPostsResponse>> GetAuthorPosts(
string username,
[FromQuery] GetAuthorPostsSearchParams searchParams,
CancellationToken cancellationToken
)
{
var res = await mediator.Send(
new GetPostsQuery
{
Username = username,
Amount = searchParams.Amount ?? 20,
From = searchParams.From,
},
cancellationToken
);
return new GetAuthorPostsResponse(
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

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,3 @@
namespace Femto.Api.Controllers.Posts.Dto;
public record CreatePostRequest(Guid AuthorId, string Content, IEnumerable<Uri> Media);
public record CreatePostRequest(Guid AuthorId, string Content, IEnumerable<CreatePostRequestMedia> Media);

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record CreatePostRequestMedia(Guid MediaId, Uri Url, string? Type, int? Width, int? Height);

View file

@ -3,4 +3,4 @@ using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record GetPublicPostsSearchParams(Guid? From, int? Amount);
public record GetPublicPostsSearchParams(Guid? From, int? Amount, Guid? AuthorId, string? Author);

View file

@ -7,6 +7,6 @@ public record PublicPostDto(
PublicPostAuthorDto Author,
Guid PostId,
string Content,
IEnumerable<Uri> Media,
DateTimeOffset CreatedAt
IEnumerable<PublicPostMediaDto> Media,
DateTimeOffset CreatedAt
);

View file

@ -0,0 +1,6 @@
using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record PublicPostMediaDto(Uri Url, int? Width, int? Height);

View file

@ -1,6 +1,8 @@
using Femto.Api.Controllers.Posts.Dto;
using Femto.Modules.Blog.Domain.Posts.Commands.CreatePost;
using Femto.Modules.Blog.Domain.Posts.Commands.GetPosts;
using Femto.Modules.Blog;
using Femto.Modules.Blog.Application;
using Femto.Modules.Blog.Application.Commands.CreatePost;
using Femto.Modules.Blog.Application.Queries.GetPosts;
using MediatR;
using Microsoft.AspNetCore.Mvc;
@ -8,7 +10,7 @@ namespace Femto.Api.Controllers.Posts;
[ApiController]
[Route("posts")]
public class PostsController(IMediator mediator) : ControllerBase
public class PostsController(IBlogModule blogModule) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<GetAllPublicPostsResponse>> GetAllPublicPosts(
@ -16,11 +18,13 @@ public class PostsController(IMediator mediator) : ControllerBase
CancellationToken cancellationToken
)
{
var res = await mediator.Send(
var res = await blogModule.PostQuery(
new GetPostsQuery
{
From = searchParams.From,
Amount = searchParams.Amount ?? 20
Amount = searchParams.Amount ?? 20,
AuthorId = searchParams.AuthorId,
Author = searchParams.Author,
},
cancellationToken
);
@ -30,7 +34,7 @@ public class PostsController(IMediator mediator) : ControllerBase
new PublicPostAuthorDto(p.Author.AuthorId, p.Author.Username),
p.PostId,
p.Text,
p.Media.Select(m => m.Url),
p.Media.Select(m => new PublicPostMediaDto(m.Url, m.Width, m.Height)),
p.CreatedAt
)),
res.Next
@ -43,8 +47,22 @@ public class PostsController(IMediator mediator) : ControllerBase
CancellationToken cancellationToken
)
{
var guid = await mediator.Send(
new CreatePostCommand(req.AuthorId, req.Content, req.Media),
var guid = await blogModule.PostCommand(
new CreatePostCommand(
req.AuthorId,
req.Content,
req.Media.Select(
(media, idx) =>
new CreatePostMedia(
media.MediaId,
media.Url,
media.Type,
idx,
media.Width,
media.Height
)
)
),
cancellationToken
);