Add a Creator property on Content

This commit is contained in:
Jonathan Bourdon
2024-08-03 23:23:37 -04:00
parent 0340904847
commit e560dfccc1
4 changed files with 18 additions and 24 deletions

View File

@@ -1,12 +1,15 @@
namespace Hutopy.Web.Features.Contents.Data; using System.ComponentModel.DataAnnotations;
namespace Hutopy.Web.Features.Contents.Data;
public class Content public class Content
{ {
public Guid Id { get; init; } public Guid Id { get; init; }
public Guid CreatedBy { get; init; } public Guid CreatedBy { get; init; }
public Creator? Creator { get; set; }
public DateTimeOffset CreatedAt { get; init; } public DateTimeOffset CreatedAt { get; init; }
public string Title { get; set; } [MaxLength(128)] public required string Title { get; set; }
public string Description { get; set; } [MaxLength(2048)] public required string Description { get; set; }
public string[]? Urls { get; init; } public string[]? Urls { get; init; }
} }

View File

@@ -21,6 +21,12 @@ public class ContentDbContext(
.Property(c => c.CreatedAt) .Property(c => c.CreatedAt)
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasDefaultValueSql("CURRENT_TIMESTAMP"); .HasDefaultValueSql("CURRENT_TIMESTAMP");
modelBuilder
.Entity<Content>()
.HasOne(c => c.Creator)
.WithMany()
.HasForeignKey(c => c.CreatedBy);
modelBuilder modelBuilder
.Entity<Creator>() .Entity<Creator>()
@@ -41,19 +47,4 @@ public class ContentDbContext(
.OwnsOne<StoredDataUrls>(x => x.StoredDataUrls) .OwnsOne<StoredDataUrls>(x => x.StoredDataUrls)
.ToTable(nameof(StoredDataUrls)); .ToTable(nameof(StoredDataUrls));
} }
public async Task<Creator?> FindByCreatorAliasAsync(
string creatorAlias,
CancellationToken cancellationToken = default)
{
ArgumentException.ThrowIfNullOrEmpty(creatorAlias);
var user = await Creators.SingleOrDefaultAsync(creator =>
EF.Functions.Like(
creatorAlias,
creator.Name),
cancellationToken: cancellationToken);
return user;
}
} }

View File

@@ -26,7 +26,7 @@ public class GetContent(
{ {
var content = await context var content = await context
.Contents .Contents
.FirstOrDefaultAsync( .SingleOrDefaultAsync(
c => c.Id == req.ContentId, c => c.Id == req.ContentId,
cancellationToken: ct); cancellationToken: ct);

View File

@@ -4,26 +4,26 @@ using Microsoft.EntityFrameworkCore;
namespace Hutopy.Web.Features.Contents.Handlers; namespace Hutopy.Web.Features.Contents.Handlers;
public sealed class GetContentsByUserRequest public sealed class GetContentsByCreatorRequest
{ {
public Guid UserId { get; set; } public Guid UserId { get; set; }
[BindFrom("page_size")] public int PageSize { get; set; } = 10; [BindFrom("page_size")] public int PageSize { get; set; } = 10;
[BindFrom("last_id")] public Guid? LastId { get; set; } [BindFrom("last_id")] public Guid? LastId { get; set; }
} }
public class GetContentsByUser( public class GetContentsByCreatorHandler(
ContentDbContext context) ContentDbContext context)
: Endpoint<GetContentsByUserRequest, List<Content>> : Endpoint<GetContentsByCreatorRequest, List<Content>>
{ {
public override void Configure() public override void Configure()
{ {
Get("/api/contents/user/{UserId:guid}"); Get("/api/contents/creator/{UserId:guid}");
Options(o => o.WithTags("Contents")); Options(o => o.WithTags("Contents"));
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync( public override async Task HandleAsync(
GetContentsByUserRequest req, GetContentsByCreatorRequest req,
CancellationToken ct) CancellationToken ct)
{ {
var query = context var query = context