From 468d029ed5322fe938bb8338c0ab2928db8aed2d Mon Sep 17 00:00:00 2001 From: masiton Date: Sat, 5 Jul 2025 10:22:45 +0200 Subject: [PATCH] Fixed endpoints for /users and /messages. --- ...vironmentPersistenceConfigurationParser.cs | 4 +- .../DesignTimeDbContextFactory.cs | 12 +++ .../MessengerBroker.Db.Sql.Migrator.csproj | 25 ++++++ .../Program.cs | 11 +++ .../20250705074809_Initial.Designer.cs | 83 +++++++++++++++++++ .../Migrations/20250705074809_Initial.cs | 68 +++++++++++++++ .../BrokerSqlDbContextModelSnapshot.cs | 80 ++++++++++++++++++ code/MessengerBroker.Db/BrokerDbContext.cs | 6 +- code/MessengerBroker.sln | 7 ++ .../Handlers/Endpoint/UsersEndpointHandler.cs | 6 +- code/MessengerBroker/Program.cs | 9 +- 11 files changed, 303 insertions(+), 8 deletions(-) create mode 100644 code/MessengerBroker.Db.Sql.Migrator/DesignTimeDbContextFactory.cs create mode 100644 code/MessengerBroker.Db.Sql.Migrator/MessengerBroker.Db.Sql.Migrator.csproj create mode 100644 code/MessengerBroker.Db.Sql.Migrator/Program.cs create mode 100644 code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.Designer.cs create mode 100644 code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.cs create mode 100644 code/MessengerBroker.Db.Sql/Migrations/BrokerSqlDbContextModelSnapshot.cs diff --git a/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs b/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs index 95dcff6..ca68d15 100644 --- a/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs +++ b/code/MessengerBroker.Configuration/Parsers/ApiEnvironmentPersistenceConfigurationParser.cs @@ -14,11 +14,11 @@ namespace MessengerBroker.Configuration.Parsers if (type == MessengerApi.Configuration.Enums.PersistenceTypes.Sql) { - return new SqlPersistenceConfiguration(config); + return new SqlPersistenceConfiguration(config.GetValue(Constants.EnvironmentVariables.API_SQL_CONNECTIONSTRING)); } else if (type == MessengerApi.Configuration.Enums.PersistenceTypes.PostgreSql) { - return new NpgPersistenceConfiguration(config); + return new NpgPersistenceConfiguration(config.GetValue(Constants.EnvironmentVariables.API_NPG_CONNECTIONSTRING)); } throw new InvalidOperationException("Unrecognized persistence type."); diff --git a/code/MessengerBroker.Db.Sql.Migrator/DesignTimeDbContextFactory.cs b/code/MessengerBroker.Db.Sql.Migrator/DesignTimeDbContextFactory.cs new file mode 100644 index 0000000..78280db --- /dev/null +++ b/code/MessengerBroker.Db.Sql.Migrator/DesignTimeDbContextFactory.cs @@ -0,0 +1,12 @@ +using Microsoft.EntityFrameworkCore.Design; + +namespace MessengerBroker.Db.Sql.Migrator +{ + public partial class DesignTimeDbContextFactory : IDesignTimeDbContextFactory + { + public BrokerSqlDbContext CreateDbContext(string[] args) + { + return new BrokerSqlDbContext(CONNECTION_STRING); + } + } +} diff --git a/code/MessengerBroker.Db.Sql.Migrator/MessengerBroker.Db.Sql.Migrator.csproj b/code/MessengerBroker.Db.Sql.Migrator/MessengerBroker.Db.Sql.Migrator.csproj new file mode 100644 index 0000000..daed342 --- /dev/null +++ b/code/MessengerBroker.Db.Sql.Migrator/MessengerBroker.Db.Sql.Migrator.csproj @@ -0,0 +1,25 @@ + + + + Exe + net9.0 + enable + enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/code/MessengerBroker.Db.Sql.Migrator/Program.cs b/code/MessengerBroker.Db.Sql.Migrator/Program.cs new file mode 100644 index 0000000..bdb617d --- /dev/null +++ b/code/MessengerBroker.Db.Sql.Migrator/Program.cs @@ -0,0 +1,11 @@ +namespace MessengerBroker.Db.Sql.Migrator +{ + internal class Program + { + static void Main(string[] args) + { + // Add-Migration Initial -Project MessengerBroker.Db.Sql -StartupProject MessengerBroker.Db.Sql.Migrator -Verbose -Context BrokerSqlDbContext + // Update-Database -Project MessengerBroker.Db.Sql -StartupProject MessengerBroker.Db.Sql.Migrator -Verbose -Context BrokerSqlDbContext + } + } +} diff --git a/code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.Designer.cs b/code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.Designer.cs new file mode 100644 index 0000000..dabd57e --- /dev/null +++ b/code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.Designer.cs @@ -0,0 +1,83 @@ +// +using System; +using MessengerBroker.Db.Sql; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MessengerBroker.Db.Sql.Migrations +{ + [DbContext(typeof(BrokerSqlDbContext))] + [Migration("20250705074809_Initial")] + partial class Initial + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("MessengerBroker.Db.Model.Message", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("BrokerId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("MessengerBroker.Db.Model.Sync", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BrokerId") + .HasColumnType("uniqueidentifier"); + + b.Property("Changes") + .HasColumnType("bigint"); + + b.Property("FinishedUtc") + .HasColumnType("datetime2"); + + b.Property("StartedUtc") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Syncs"); + }); + + modelBuilder.Entity("MessengerBroker.Db.Model.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("BrokerId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.cs b/code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.cs new file mode 100644 index 0000000..2c6d684 --- /dev/null +++ b/code/MessengerBroker.Db.Sql/Migrations/20250705074809_Initial.cs @@ -0,0 +1,68 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace MessengerBroker.Db.Sql.Migrations +{ + /// + public partial class Initial : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Messages", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + BrokerId = table.Column(type: "uniqueidentifier", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Messages", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Syncs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + BrokerId = table.Column(type: "uniqueidentifier", nullable: false), + StartedUtc = table.Column(type: "datetime2", nullable: false), + FinishedUtc = table.Column(type: "datetime2", nullable: true), + Changes = table.Column(type: "bigint", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Syncs", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "uniqueidentifier", nullable: false), + BrokerId = table.Column(type: "uniqueidentifier", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Messages"); + + migrationBuilder.DropTable( + name: "Syncs"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/code/MessengerBroker.Db.Sql/Migrations/BrokerSqlDbContextModelSnapshot.cs b/code/MessengerBroker.Db.Sql/Migrations/BrokerSqlDbContextModelSnapshot.cs new file mode 100644 index 0000000..c5a517f --- /dev/null +++ b/code/MessengerBroker.Db.Sql/Migrations/BrokerSqlDbContextModelSnapshot.cs @@ -0,0 +1,80 @@ +// +using System; +using MessengerBroker.Db.Sql; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace MessengerBroker.Db.Sql.Migrations +{ + [DbContext(typeof(BrokerSqlDbContext))] + partial class BrokerSqlDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.6") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("MessengerBroker.Db.Model.Message", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("BrokerId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("Messages"); + }); + + modelBuilder.Entity("MessengerBroker.Db.Model.Sync", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BrokerId") + .HasColumnType("uniqueidentifier"); + + b.Property("Changes") + .HasColumnType("bigint"); + + b.Property("FinishedUtc") + .HasColumnType("datetime2"); + + b.Property("StartedUtc") + .HasColumnType("datetime2"); + + b.HasKey("Id"); + + b.ToTable("Syncs"); + }); + + modelBuilder.Entity("MessengerBroker.Db.Model.User", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uniqueidentifier"); + + b.Property("BrokerId") + .HasColumnType("uniqueidentifier"); + + b.HasKey("Id"); + + b.ToTable("Users"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/code/MessengerBroker.Db/BrokerDbContext.cs b/code/MessengerBroker.Db/BrokerDbContext.cs index 419140c..cbf13c3 100644 --- a/code/MessengerBroker.Db/BrokerDbContext.cs +++ b/code/MessengerBroker.Db/BrokerDbContext.cs @@ -5,10 +5,10 @@ namespace MessengerBroker.Db { public class BrokerDbContext : DbContext { - public DbSet Syncs { get; } + public DbSet Syncs { get; set; } - public DbSet Users { get; } + public DbSet Users { get; set; } - public DbSet Messages { get; } + public DbSet Messages { get; set; } } } diff --git a/code/MessengerBroker.sln b/code/MessengerBroker.sln index 0db3991..d93f90f 100644 --- a/code/MessengerBroker.sln +++ b/code/MessengerBroker.sln @@ -27,6 +27,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessengerApi.Db.Sql", "..\s EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessengerApi.Db.Npg", "..\subm\messengerapi\code\MessengerApi.Db.Npg\MessengerApi.Db.Npg.csproj", "{2A08099B-1A1E-FE40-9220-43F26AF852EC}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessengerBroker.Db.Sql.Migrator", "MessengerBroker.Db.Sql.Migrator\MessengerBroker.Db.Sql.Migrator.csproj", "{B6AC564C-F5D0-4A6E-B1A2-A6942B9B2E5C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -73,6 +75,10 @@ Global {2A08099B-1A1E-FE40-9220-43F26AF852EC}.Debug|Any CPU.Build.0 = Debug|Any CPU {2A08099B-1A1E-FE40-9220-43F26AF852EC}.Release|Any CPU.ActiveCfg = Release|Any CPU {2A08099B-1A1E-FE40-9220-43F26AF852EC}.Release|Any CPU.Build.0 = Release|Any CPU + {B6AC564C-F5D0-4A6E-B1A2-A6942B9B2E5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B6AC564C-F5D0-4A6E-B1A2-A6942B9B2E5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B6AC564C-F5D0-4A6E-B1A2-A6942B9B2E5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B6AC564C-F5D0-4A6E-B1A2-A6942B9B2E5C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -86,6 +92,7 @@ Global {FCE9214C-CDCB-4D79-B7AB-2BCBAD41AC35} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {5F0E1A67-056B-E6B9-1940-5F31587C05CE} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} {2A08099B-1A1E-FE40-9220-43F26AF852EC} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} + {B6AC564C-F5D0-4A6E-B1A2-A6942B9B2E5C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F0F93DDE-2CDC-4A7D-9D70-A7A12B3AF9CE} diff --git a/code/MessengerBroker/Handlers/Endpoint/UsersEndpointHandler.cs b/code/MessengerBroker/Handlers/Endpoint/UsersEndpointHandler.cs index 93fede5..f623aab 100644 --- a/code/MessengerBroker/Handlers/Endpoint/UsersEndpointHandler.cs +++ b/code/MessengerBroker/Handlers/Endpoint/UsersEndpointHandler.cs @@ -37,10 +37,14 @@ namespace MessengerBroker.Handlers.Endpoint .Where(x => !foreignUserIds.Any(f => f == x.Id)) .ToArray(); + var localUserIds = localUsers + .Select(x => x.Id) + .ToArray(); + var localRoutes = apiCtx.UserRoutes .Include(x => x.From) .Include(x => x.To) - .Where(x => localUsers.Any(l => l.Id == x.From.Id) && localUsers.Any(l => l.Id == x.To.Id)) + .Where(x => localUserIds.Contains(x.From.Id) || localUserIds.Contains(x.To.Id)) .ToArray(); return Task.FromResult(new Users.UsersResponse diff --git a/code/MessengerBroker/Program.cs b/code/MessengerBroker/Program.cs index 4aeb56d..0f54a30 100644 --- a/code/MessengerBroker/Program.cs +++ b/code/MessengerBroker/Program.cs @@ -7,6 +7,7 @@ using MessengerBroker.Model.Http; using MessengerBroker.Models.Scoped; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Net; @@ -123,9 +124,13 @@ namespace MessengerBroker app.MapGet("/messages", async ( MessagesEndpointHandler handler, - [AsParameters] Messages.MessagesRequest request) => + [FromQuery] Guid ownerBrokerId, DateTime fromUtc) => { - var response = await handler.GetMessages(request); + var response = await handler.GetMessages(new Messages.MessagesRequest + { + OwnerBrokerId = ownerBrokerId, + SinceUtc = DateTime.MinValue + }); return Results.Json(response); });