Adding replies to the Messaging system
This commit is contained in:
@@ -6,6 +6,6 @@ public class Message
|
|||||||
public Guid SubjectId { get; init; }
|
public Guid SubjectId { get; init; }
|
||||||
public Guid CreatedBy { get; init; }
|
public Guid CreatedBy { get; init; }
|
||||||
public DateTimeOffset CreatedAt { get; }
|
public DateTimeOffset CreatedAt { get; }
|
||||||
public Guid ParentId { get; init; }
|
public Guid? ParentId { get; init; }
|
||||||
public string Value { get; init; }
|
public string Value { get; init; }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace Hutopy.Web.Messages.Handlers;
|
namespace Hutopy.Web.Messages.Handlers;
|
||||||
|
|
||||||
public sealed class GetMessagesRequest
|
public class GetMessagesRequest
|
||||||
{
|
{
|
||||||
public Guid SubjectId { get; set; }
|
public Guid SubjectId { get; set; }
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ public class GetMessages(
|
|||||||
var comments = await context
|
var comments = await context
|
||||||
.Messages
|
.Messages
|
||||||
.Where(c => c.SubjectId == req.SubjectId)
|
.Where(c => c.SubjectId == req.SubjectId)
|
||||||
|
.Where(c => c.ParentId == null)
|
||||||
.ToListAsync(cancellationToken: ct);
|
.ToListAsync(cancellationToken: ct);
|
||||||
|
|
||||||
await SendAsync(comments, cancellation: ct);
|
await SendAsync(comments, cancellation: ct);
|
||||||
|
|||||||
34
src/Web/Messages/Handlers/GetMessagesByParent.cs
Normal file
34
src/Web/Messages/Handlers/GetMessagesByParent.cs
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
using FastEndpoints;
|
||||||
|
using Hutopy.Web.Messages.Data;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Hutopy.Web.Messages.Handlers;
|
||||||
|
|
||||||
|
public class GetMessagesRepliesRequest
|
||||||
|
{
|
||||||
|
public Guid ParentId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GetMessagesByParent(
|
||||||
|
MessagingDbContext context)
|
||||||
|
: Endpoint<GetMessagesRepliesRequest, List<Message>>
|
||||||
|
{
|
||||||
|
public override void Configure()
|
||||||
|
{
|
||||||
|
Get("/api/messages/{ParentId:guid}/replies");
|
||||||
|
Options(o => o.WithTags("Messages"));
|
||||||
|
AllowAnonymous();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override async Task HandleAsync(
|
||||||
|
GetMessagesRepliesRequest req,
|
||||||
|
CancellationToken ct)
|
||||||
|
{
|
||||||
|
var posts = await context
|
||||||
|
.Messages
|
||||||
|
.Where(c => c.ParentId == req.ParentId)
|
||||||
|
.ToListAsync(cancellationToken: ct);
|
||||||
|
|
||||||
|
await SendAsync(posts, cancellation: ct);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
|
|
||||||
namespace Hutopy.Web.Messages.Handlers;
|
namespace Hutopy.Web.Messages.Handlers;
|
||||||
|
|
||||||
public sealed class GetMessagesByUserRequest
|
public class GetMessagesByUserRequest
|
||||||
{
|
{
|
||||||
public Guid UserId { get; set; }
|
public Guid UserId { get; set; }
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ public class GetMessagesByUser(
|
|||||||
{
|
{
|
||||||
Get("/api/messages/user/{UserId:guid}");
|
Get("/api/messages/user/{UserId:guid}");
|
||||||
Options(o => o.WithTags("Messages"));
|
Options(o => o.WithTags("Messages"));
|
||||||
|
AllowAnonymous();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task HandleAsync(
|
public override async Task HandleAsync(
|
||||||
@@ -26,6 +27,7 @@ public class GetMessagesByUser(
|
|||||||
var posts = await context
|
var posts = await context
|
||||||
.Messages
|
.Messages
|
||||||
.Where(c => c.CreatedBy == req.UserId)
|
.Where(c => c.CreatedBy == req.UserId)
|
||||||
|
.Where(c => c.ParentId == null)
|
||||||
.ToListAsync(cancellationToken: ct);
|
.ToListAsync(cancellationToken: ct);
|
||||||
|
|
||||||
await SendAsync(posts, cancellation: ct);
|
await SendAsync(posts, cancellation: ct);
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ using Hutopy.Web.Messages.Data;
|
|||||||
|
|
||||||
namespace Hutopy.Web.Messages.Handlers;
|
namespace Hutopy.Web.Messages.Handlers;
|
||||||
|
|
||||||
public record PostMessageRequest(
|
public class PostMessageRequest
|
||||||
Guid SubjectId,
|
{
|
||||||
string Message);
|
public Guid SubjectId { get; set; }
|
||||||
|
public string Message { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public class PostMessage(
|
public class PostMessage(
|
||||||
MessagingDbContext context)
|
MessagingDbContext context)
|
||||||
@@ -22,15 +24,15 @@ public class PostMessage(
|
|||||||
PostMessageRequest req,
|
PostMessageRequest req,
|
||||||
CancellationToken ct)
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
await context.Messages.AddAsync(
|
var message = new Message
|
||||||
new Message
|
{
|
||||||
{
|
Id = GuidHelper.GenerateUuidV7(),
|
||||||
Id = GuidHelper.GenerateUuidV7(),
|
SubjectId = req.SubjectId,
|
||||||
SubjectId = req.SubjectId,
|
CreatedBy = User.GetUserId(),
|
||||||
CreatedBy = User.GetUserId(),
|
Value = req.Message
|
||||||
Value = req.Message
|
};
|
||||||
},
|
|
||||||
ct);
|
await context.Messages.AddAsync(message, ct);
|
||||||
|
|
||||||
await context.SaveChangesAsync(ct);
|
await context.SaveChangesAsync(ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,18 +4,20 @@ using Hutopy.Web.Messages.Data;
|
|||||||
|
|
||||||
namespace Hutopy.Web.Messages.Handlers;
|
namespace Hutopy.Web.Messages.Handlers;
|
||||||
|
|
||||||
public record PostReplyMessageRequest(
|
internal sealed class PostReplyMessageRequest
|
||||||
Guid SubjectId,
|
{
|
||||||
Guid ParentId,
|
public required Guid SubjectId { get; set; }
|
||||||
string Message);
|
public required Guid ParentId { get; set; }
|
||||||
|
public required string Message { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
public sealed class PostReplyMessage(
|
internal sealed class PostReplyMessage(
|
||||||
MessagingDbContext context)
|
MessagingDbContext context)
|
||||||
: Endpoint<PostReplyMessageRequest>
|
: Endpoint<PostReplyMessageRequest>
|
||||||
{
|
{
|
||||||
public override void Configure()
|
public override void Configure()
|
||||||
{
|
{
|
||||||
Post("/api/messages/reply");
|
Post("/api/messages/{ParentId:guid}/replies");
|
||||||
Options(o => o.WithTags("Messages"));
|
Options(o => o.WithTags("Messages"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -23,16 +25,16 @@ public sealed class PostReplyMessage(
|
|||||||
PostReplyMessageRequest req,
|
PostReplyMessageRequest req,
|
||||||
CancellationToken ct)
|
CancellationToken ct)
|
||||||
{
|
{
|
||||||
await context.Messages.AddAsync(
|
var message = new Message
|
||||||
new Message
|
{
|
||||||
{
|
Id = GuidHelper.GenerateUuidV7(),
|
||||||
Id = GuidHelper.GenerateUuidV7(),
|
SubjectId = req.SubjectId,
|
||||||
SubjectId = req.SubjectId,
|
ParentId = req.ParentId,
|
||||||
ParentId = req.ParentId,
|
CreatedBy = User.GetUserId(),
|
||||||
CreatedBy = User.GetUserId(),
|
Value = req.Message
|
||||||
Value = req.Message
|
};
|
||||||
},
|
|
||||||
ct);
|
await context.Messages.AddAsync(message, ct);
|
||||||
|
|
||||||
await context.SaveChangesAsync(ct);
|
await context.SaveChangesAsync(ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
|||||||
namespace Hutopy.Web.Messages.Migrations
|
namespace Hutopy.Web.Messages.Migrations
|
||||||
{
|
{
|
||||||
[DbContext(typeof(MessagingDbContext))]
|
[DbContext(typeof(MessagingDbContext))]
|
||||||
[Migration("20240718041130_Initial")]
|
[Migration("20240718173016_Initial")]
|
||||||
partial class Initial
|
partial class Initial
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -40,7 +40,7 @@ namespace Hutopy.Web.Messages.Migrations
|
|||||||
b.Property<Guid>("CreatedBy")
|
b.Property<Guid>("CreatedBy")
|
||||||
.HasColumnType("uuid");
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
b.Property<Guid>("ParentId")
|
b.Property<Guid?>("ParentId")
|
||||||
.HasColumnType("uuid");
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
b.Property<Guid>("SubjectId")
|
b.Property<Guid>("SubjectId")
|
||||||
@@ -23,7 +23,7 @@ namespace Hutopy.Web.Messages.Migrations
|
|||||||
SubjectId = table.Column<Guid>(type: "uuid", nullable: false),
|
SubjectId = table.Column<Guid>(type: "uuid", nullable: false),
|
||||||
CreatedBy = table.Column<Guid>(type: "uuid", nullable: false),
|
CreatedBy = table.Column<Guid>(type: "uuid", nullable: false),
|
||||||
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
|
CreatedAt = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "CURRENT_TIMESTAMP"),
|
||||||
ParentId = table.Column<Guid>(type: "uuid", nullable: false),
|
ParentId = table.Column<Guid>(type: "uuid", nullable: true),
|
||||||
Value = table.Column<string>(type: "text", nullable: false)
|
Value = table.Column<string>(type: "text", nullable: false)
|
||||||
},
|
},
|
||||||
constraints: table =>
|
constraints: table =>
|
||||||
@@ -37,7 +37,7 @@ namespace Hutopy.Web.Messages.Migrations
|
|||||||
b.Property<Guid>("CreatedBy")
|
b.Property<Guid>("CreatedBy")
|
||||||
.HasColumnType("uuid");
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
b.Property<Guid>("ParentId")
|
b.Property<Guid?>("ParentId")
|
||||||
.HasColumnType("uuid");
|
.HasColumnType("uuid");
|
||||||
|
|
||||||
b.Property<Guid>("SubjectId")
|
b.Property<Guid>("SubjectId")
|
||||||
|
|||||||
@@ -7,3 +7,11 @@ docker run \
|
|||||||
-p 1433:1433 \
|
-p 1433:1433 \
|
||||||
--name azuresqledge \
|
--name azuresqledge \
|
||||||
-d mcr.microsoft.com/azure-sql-edge
|
-d mcr.microsoft.com/azure-sql-edge
|
||||||
|
|
||||||
|
docker run \
|
||||||
|
--cap-add SYS_PTRACE \
|
||||||
|
-e 'POSTGRES_USER=sa' \
|
||||||
|
-e 'POSTGRES_PASSWORD=P@ssword123!' \
|
||||||
|
-p 5432:5432 \
|
||||||
|
--name postgres \
|
||||||
|
-d postgres
|
||||||
Reference in New Issue
Block a user