Moved features to a specific folder
This commit is contained in:
11
src/Web/Features/Messages/Data/Message.cs
Normal file
11
src/Web/Features/Messages/Data/Message.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace Hutopy.Web.Features.Messages.Data;
|
||||
|
||||
public class Message
|
||||
{
|
||||
public Guid Id { get; init; }
|
||||
public Guid SubjectId { get; init; }
|
||||
public Guid CreatedBy { get; init; }
|
||||
public DateTimeOffset CreatedAt { get; }
|
||||
public Guid? ParentId { get; init; }
|
||||
public string Value { get; init; }
|
||||
}
|
||||
21
src/Web/Features/Messages/Data/MessagingDbContext.cs
Normal file
21
src/Web/Features/Messages/Data/MessagingDbContext.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hutopy.Web.Features.Messages.Data;
|
||||
|
||||
public class MessagingDbContext(
|
||||
DbContextOptions<MessagingDbContext> options)
|
||||
: DbContext(options)
|
||||
{
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.HasDefaultSchema("Messaging");
|
||||
|
||||
modelBuilder
|
||||
.Entity<Message>()
|
||||
.Property(c => c.CreatedAt)
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
}
|
||||
|
||||
public DbSet<Message> Messages { get; set; }
|
||||
}
|
||||
16
src/Web/Features/Messages/DependencyInjection.cs
Normal file
16
src/Web/Features/Messages/DependencyInjection.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hutopy.Web.Features.Messages;
|
||||
|
||||
public static class DependencyInjection
|
||||
{
|
||||
public static IServiceCollection AddMessagingModule(
|
||||
this IServiceCollection services,
|
||||
Action<DbContextOptionsBuilder>? configureAction = null)
|
||||
{
|
||||
services.AddDbContext<MessagingDbContext>(configureAction);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
35
src/Web/Features/Messages/Handlers/GetMessages.cs
Normal file
35
src/Web/Features/Messages/Handlers/GetMessages.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using FastEndpoints;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hutopy.Web.Features.Messages.Handlers;
|
||||
|
||||
public class GetMessagesRequest
|
||||
{
|
||||
public Guid SubjectId { get; set; }
|
||||
}
|
||||
|
||||
public class GetMessages(
|
||||
MessagingDbContext context)
|
||||
: Endpoint<GetMessagesRequest, List<Message>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/messages/{SubjectId:guid}");
|
||||
Options(o => o.WithTags("Messages"));
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(
|
||||
GetMessagesRequest req,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var comments = await context
|
||||
.Messages
|
||||
.Where(c => c.SubjectId == req.SubjectId)
|
||||
.Where(c => c.ParentId == null)
|
||||
.ToListAsync(cancellationToken: ct);
|
||||
|
||||
await SendAsync(comments, cancellation: ct);
|
||||
}
|
||||
}
|
||||
34
src/Web/Features/Messages/Handlers/GetMessagesByParent.cs
Normal file
34
src/Web/Features/Messages/Handlers/GetMessagesByParent.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using FastEndpoints;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hutopy.Web.Features.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);
|
||||
}
|
||||
}
|
||||
35
src/Web/Features/Messages/Handlers/GetMessagesByUser.cs
Normal file
35
src/Web/Features/Messages/Handlers/GetMessagesByUser.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using FastEndpoints;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Hutopy.Web.Features.Messages.Handlers;
|
||||
|
||||
public class GetMessagesByUserRequest
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
}
|
||||
|
||||
public class GetMessagesByUser(
|
||||
MessagingDbContext context)
|
||||
: Endpoint<GetMessagesByUserRequest, List<Message>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/messages/user/{UserId:guid}");
|
||||
Options(o => o.WithTags("Messages"));
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(
|
||||
GetMessagesByUserRequest req,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var posts = await context
|
||||
.Messages
|
||||
.Where(c => c.CreatedBy == req.UserId)
|
||||
.Where(c => c.ParentId == null)
|
||||
.ToListAsync(cancellationToken: ct);
|
||||
|
||||
await SendAsync(posts, cancellation: ct);
|
||||
}
|
||||
}
|
||||
39
src/Web/Features/Messages/Handlers/PostMessage.cs
Normal file
39
src/Web/Features/Messages/Handlers/PostMessage.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using FastEndpoints;
|
||||
using Hutopy.Web.Common;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
|
||||
namespace Hutopy.Web.Features.Messages.Handlers;
|
||||
|
||||
public class PostMessageRequest
|
||||
{
|
||||
public Guid SubjectId { get; set; }
|
||||
public string Message { get; set; }
|
||||
}
|
||||
|
||||
public class PostMessage(
|
||||
MessagingDbContext context)
|
||||
: Endpoint<PostMessageRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/messages");
|
||||
Options(o => o.WithTags("Messages"));
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(
|
||||
PostMessageRequest req,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var message = new Message
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
SubjectId = req.SubjectId,
|
||||
CreatedBy = User.GetUserId(),
|
||||
Value = req.Message
|
||||
};
|
||||
|
||||
await context.Messages.AddAsync(message, ct);
|
||||
|
||||
await context.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
41
src/Web/Features/Messages/Handlers/PostReplyMessage.cs
Normal file
41
src/Web/Features/Messages/Handlers/PostReplyMessage.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using FastEndpoints;
|
||||
using Hutopy.Web.Common;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
|
||||
namespace Hutopy.Web.Features.Messages.Handlers;
|
||||
|
||||
internal sealed class PostReplyMessageRequest
|
||||
{
|
||||
public required Guid SubjectId { get; set; }
|
||||
public required Guid ParentId { get; set; }
|
||||
public required string Message { get; set; }
|
||||
}
|
||||
|
||||
internal sealed class PostReplyMessage(
|
||||
MessagingDbContext context)
|
||||
: Endpoint<PostReplyMessageRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/messages/{ParentId:guid}/replies");
|
||||
Options(o => o.WithTags("Messages"));
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(
|
||||
PostReplyMessageRequest req,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var message = new Message
|
||||
{
|
||||
Id = GuidHelper.GenerateUuidV7(),
|
||||
SubjectId = req.SubjectId,
|
||||
ParentId = req.ParentId,
|
||||
CreatedBy = User.GetUserId(),
|
||||
Value = req.Message
|
||||
};
|
||||
|
||||
await context.Messages.AddAsync(message, ct);
|
||||
|
||||
await context.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
60
src/Web/Features/Messages/Migrations/20240718173016_Initial.Designer.cs
generated
Normal file
60
src/Web/Features/Messages/Migrations/20240718173016_Initial.Designer.cs
generated
Normal file
@@ -0,0 +1,60 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Hutopy.Web.Messages.Migrations
|
||||
{
|
||||
[DbContext(typeof(MessagingDbContext))]
|
||||
[Migration("20240718173016_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasDefaultSchema("Messaging")
|
||||
.HasAnnotation("ProductVersion", "8.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Hutopy.Web.Messages.Data.Message", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
|
||||
b.Property<Guid>("CreatedBy")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid?>("ParentId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid>("SubjectId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Messages", "Messaging");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Hutopy.Web.Messages.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Initial : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.EnsureSchema(
|
||||
name: "Messaging");
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Messages",
|
||||
schema: "Messaging",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
SubjectId = 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"),
|
||||
ParentId = table.Column<Guid>(type: "uuid", nullable: true),
|
||||
Value = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Messages", x => x.Id);
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Messages",
|
||||
schema: "Messaging");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Hutopy.Web.Features.Messages.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Hutopy.Web.Messages.Migrations
|
||||
{
|
||||
[DbContext(typeof(MessagingDbContext))]
|
||||
partial class MessagingDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasDefaultSchema("Messaging")
|
||||
.HasAnnotation("ProductVersion", "8.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Hutopy.Web.Messages.Data.Message", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTimeOffset>("CreatedAt")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("timestamp with time zone")
|
||||
.HasDefaultValueSql("CURRENT_TIMESTAMP");
|
||||
|
||||
b.Property<Guid>("CreatedBy")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid?>("ParentId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<Guid>("SubjectId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Value")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Messages", "Messaging");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user