load with public flag

This commit is contained in:
john 2025-05-18 13:40:05 +02:00
parent e3c95eb109
commit 322dd01ee0
14 changed files with 73 additions and 38 deletions

View file

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

View file

@ -3,4 +3,4 @@ using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record GetAllPublicPostsResponse(IEnumerable<PublicPostDto> Posts, Guid? Next);
public record GetAllPublicPostsResponse(IEnumerable<PostDto> Posts, Guid? Next);

View file

@ -3,4 +3,4 @@ using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record PublicPostAuthorDto(Guid AuthorId, string Username);
public record PostAuthorDto(Guid AuthorId, string Username);

View file

@ -3,10 +3,10 @@ using JetBrains.Annotations;
namespace Femto.Api.Controllers.Posts.Dto;
[PublicAPI]
public record PublicPostDto(
PublicPostAuthorDto Author,
public record PostDto(
PostAuthorDto Author,
Guid PostId,
string Content,
IEnumerable<PublicPostMediaDto> Media,
IEnumerable<PostMediaDto> Media,
DateTimeOffset CreatedAt
);

View file

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

View file

@ -1,4 +1,5 @@
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.Queries.GetPosts;
@ -9,17 +10,16 @@ namespace Femto.Api.Controllers.Posts;
[ApiController]
[Route("posts")]
public class PostsController(IBlogModule blogModule) : ControllerBase
public class PostsController(IBlogModule blogModule, ICurrentUserContext currentUserContext) : ControllerBase
{
[HttpGet]
[Authorize]
public async Task<ActionResult<GetAllPublicPostsResponse>> GetAllPublicPosts(
public async Task<ActionResult<GetAllPublicPostsResponse>> LoadPosts(
[FromQuery] GetPublicPostsSearchParams searchParams,
CancellationToken cancellationToken
)
{
var res = await blogModule.PostQuery(
new GetPostsQuery
new GetPostsQuery(currentUserContext.CurrentUser?.Id)
{
From = searchParams.From,
Amount = searchParams.Amount ?? 20,
@ -30,11 +30,11 @@ public class PostsController(IBlogModule blogModule) : ControllerBase
);
return new GetAllPublicPostsResponse(
res.Posts.Select(p => new PublicPostDto(
new PublicPostAuthorDto(p.Author.AuthorId, p.Author.Username),
res.Posts.Select(p => new PostDto(
new PostAuthorDto(p.Author.AuthorId, p.Author.Username),
p.PostId,
p.Text,
p.Media.Select(m => new PublicPostMediaDto(m.Url, m.Width, m.Height)),
p.Media.Select(m => new PostMediaDto(m.Url, m.Width, m.Height)),
p.CreatedAt
)),
res.Next
@ -43,7 +43,7 @@ public class PostsController(IBlogModule blogModule) : ControllerBase
[HttpPost]
[Authorize]
public async Task<ActionResult<CreatePostResponse>> Post(
public async Task<ActionResult<CreatePostResponse>> CreatePost(
[FromBody] CreatePostRequest req,
CancellationToken cancellationToken
)
@ -62,7 +62,8 @@ public class PostsController(IBlogModule blogModule) : ControllerBase
media.Width,
media.Height
)
)
),
req.IsPublic
),
cancellationToken
);

View file

@ -5,7 +5,11 @@ namespace Femto.Api.Sessions;
internal static class HttpContextSessionExtensions
{
public static void SetSession(this HttpContext httpContext, Session session, CookieSettings cookieSettings)
public static void SetSession(
this HttpContext httpContext,
Session session,
CookieSettings cookieSettings
)
{
httpContext.Response.Cookies.Append(
"session",
@ -14,9 +18,20 @@ internal static class HttpContextSessionExtensions
{
HttpOnly = true,
Secure = cookieSettings.Secure,
SameSite = cookieSettings.SameSite? SameSiteMode.Strict : SameSiteMode.None,
SameSite = cookieSettings.SameSite ? SameSiteMode.Strict : SameSiteMode.Unspecified,
Expires = session.Expires,
}
);
httpContext.Response.Cookies.Append(
"hasSession",
"true",
new CookieOptions
{
Secure = cookieSettings.Secure,
SameSite = cookieSettings.SameSite ? SameSiteMode.Strict : SameSiteMode.Unspecified,
Expires = session.Expires,
}
);
}
}
}