diff --git a/AsbCloudApp/Data/NotificationDto.cs b/AsbCloudApp/Data/NotificationDto.cs index 29e50b41..3ade9b04 100644 --- a/AsbCloudApp/Data/NotificationDto.cs +++ b/AsbCloudApp/Data/NotificationDto.cs @@ -1,9 +1,10 @@ using System; +using System.Text.Json.Serialization; namespace AsbCloudApp.Data; /// -/// DTO уведомлений +/// DTO уведомления /// public class NotificationDto : IId { @@ -17,11 +18,6 @@ public class NotificationDto : IId /// public int IdUser { get; set; } - /// - /// Id способа отправки уведомления - /// - public int IdNotificationTransport { get; set; } - /// /// Id категории уведомления /// @@ -33,9 +29,9 @@ public class NotificationDto : IId public string Title { get; set; } = null!; /// - /// Текст уведомления + /// Сообщение уведомления /// - public string Subject { get; set; } = null!; + public string Message { get; set; } = null!; /// /// Время жизни уведомления @@ -45,20 +41,52 @@ public class NotificationDto : IId /// /// Дата отправки уведомления /// - public DateTime? SentDateAtUtc { get; set; } + public DateTime? SentDate { get; set; } + + /// + /// Состояния уведомления + /// + public NotificationState NotificationState { get; set; } + + /// + /// Способ доставки уведомления + /// + public NotificationTransport NotificationTransport { get; set; } - /// - /// Прочитано ли уведомление - /// - public bool? IsRead { get; set; } - - /// - /// DTO способа доставки уведомления - /// - public NotificationTransportDto NotificationTransport { get; set; } = null!; - /// /// DTO категории уведомления /// public NotificationCategoryDto NotificationCategory { get; set; } = null!; +} + +/// +/// Состояние уведомления +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum NotificationState +{ + /// + /// Зарегистрировано + /// + Registered = 1, + /// + /// Отправлено + /// + Sent = 2, + /// + /// Прочитано + /// + Read = 3, +} + +/// +/// Способ отправки уведомления +/// +[JsonConverter(typeof(JsonStringEnumConverter))] +public enum NotificationTransport +{ + /// + /// SignalR + /// + SignalR = 1 } \ No newline at end of file diff --git a/AsbCloudApp/Repositories/INotificationRepository.cs b/AsbCloudApp/Repositories/INotificationRepository.cs index 1a04f26a..13ccf31c 100644 --- a/AsbCloudApp/Repositories/INotificationRepository.cs +++ b/AsbCloudApp/Repositories/INotificationRepository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; +using AsbCloudApp.Requests; using AsbCloudApp.Services; namespace AsbCloudApp.Repositories; @@ -12,28 +13,25 @@ namespace AsbCloudApp.Repositories; public interface INotificationRepository : ICrudRepository { /// - /// Метод для получения не отправленных уведомлений + /// Получение не отправленных уведомлений /// /// - /// + /// /// /// Task> GetUnsentNotificationsAsync(int idUser, - int idNotificationTransport, + NotificationTransport notificationTransport, CancellationToken cancellationToken); + /// - /// Метод получения уведомлений по параметрам + /// Получение уведомлений по параметрам /// - /// - /// /// - /// + /// /// /// - Task> GetNotificationsAsync(int? skip, - int? take, - int idUser, - int idNotificationTransport, + Task> GetNotificationsAsync(int idUser, + NotificationRequest request, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/AsbCloudApp/Requests/NotificationRequest.cs b/AsbCloudApp/Requests/NotificationRequest.cs new file mode 100644 index 00000000..929bf952 --- /dev/null +++ b/AsbCloudApp/Requests/NotificationRequest.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; +using AsbCloudApp.Data; + +namespace AsbCloudApp.Requests; + +/// +/// Параметры запроса для получения уведомлений +/// +public class NotificationRequest : RequestBase +{ + /// + /// Способ отправки уведомления + /// + [Required] + public NotificationTransport NotificationTransport { get; set; } +} \ No newline at end of file diff --git a/AsbCloudDb/Migrations/20230710113646_Add_Notifications.cs b/AsbCloudDb/Migrations/20230710113646_Add_Notifications.cs deleted file mode 100644 index a5765bc7..00000000 --- a/AsbCloudDb/Migrations/20230710113646_Add_Notifications.cs +++ /dev/null @@ -1,176 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; -using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; - -#nullable disable - -namespace AsbCloudDb.Migrations -{ - public partial class Add_Notifications : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.AlterColumn( - name: "id_category", - table: "t_help_page", - type: "integer", - nullable: false, - comment: "Id категории файла", - oldClrType: typeof(int), - oldType: "integer", - oldComment: "id категории файла"); - - migrationBuilder.CreateTable( - name: "t_notification_category", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - name = table.Column(type: "text", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_t_notification_category", x => x.id); - }, - comment: "Категории уведомлений"); - - migrationBuilder.CreateTable( - name: "t_notification_transport", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - name = table.Column(type: "text", nullable: false, comment: "Название способа доставки уведомлений") - }, - constraints: table => - { - table.PrimaryKey("PK_t_notification_transport", x => x.id); - }, - comment: "Способ доставки уведомлений"); - - migrationBuilder.CreateTable( - name: "t_notification", - columns: table => new - { - id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), - id_user = table.Column(type: "integer", nullable: false, comment: "Id получателя"), - id_notification_transport = table.Column(type: "integer", nullable: false, comment: "Id способа доставки уведомления"), - id_notification_category = table.Column(type: "integer", nullable: false, comment: "Id категории уведомления"), - title = table.Column(type: "text", nullable: false, comment: "Заголовок уведомления"), - subject = table.Column(type: "text", nullable: false, comment: "Текст уведомления"), - time_to_life = table.Column(type: "interval", nullable: false, comment: "Время жизни уведомления"), - sent_date_at_utc = table.Column(type: "timestamp with time zone", nullable: true, comment: "Дата отправки уведомления"), - is_read = table.Column(type: "boolean", nullable: true, comment: "Прочитано ли уведомление") - }, - constraints: table => - { - table.PrimaryKey("PK_t_notification", x => x.id); - table.ForeignKey( - name: "FK_t_notification_t_notification_category_id_notification_cate~", - column: x => x.id_notification_category, - principalTable: "t_notification_category", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_t_notification_t_notification_transport_id_notification_tra~", - column: x => x.id_notification_transport, - principalTable: "t_notification_transport", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - table.ForeignKey( - name: "FK_t_notification_t_user_id_user", - column: x => x.id_user, - principalTable: "t_user", - principalColumn: "id", - onDelete: ReferentialAction.Cascade); - }, - comment: "Уведомлений"); - - migrationBuilder.InsertData( - table: "t_notification_category", - columns: new[] { "id", "name" }, - values: new object[] { 1, "Системные уведомления" }); - - migrationBuilder.InsertData( - table: "t_notification_transport", - columns: new[] { "id", "name" }, - values: new object[] { 1, "SignalR" }); - - migrationBuilder.InsertData( - table: "t_permission", - columns: new[] { "id", "description", "name" }, - values: new object[,] - { - { 519, "Разрешение просматривать список контактов", "WellContact.get" }, - { 520, "Разрешение редактировать список контактов", "WellContact.edit" } - }); - - migrationBuilder.InsertData( - table: "t_relation_user_role_permission", - columns: new[] { "id_permission", "id_user_role" }, - values: new object[,] - { - { 519, 1 }, - { 520, 1 } - }); - - migrationBuilder.CreateIndex( - name: "IX_t_notification_id_notification_category", - table: "t_notification", - column: "id_notification_category"); - - migrationBuilder.CreateIndex( - name: "IX_t_notification_id_notification_transport", - table: "t_notification", - column: "id_notification_transport"); - - migrationBuilder.CreateIndex( - name: "IX_t_notification_id_user", - table: "t_notification", - column: "id_user"); - } - - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "t_notification"); - - migrationBuilder.DropTable( - name: "t_notification_category"); - - migrationBuilder.DropTable( - name: "t_notification_transport"); - - migrationBuilder.DeleteData( - table: "t_relation_user_role_permission", - keyColumns: new[] { "id_permission", "id_user_role" }, - keyValues: new object[] { 519, 1 }); - - migrationBuilder.DeleteData( - table: "t_relation_user_role_permission", - keyColumns: new[] { "id_permission", "id_user_role" }, - keyValues: new object[] { 520, 1 }); - - migrationBuilder.DeleteData( - table: "t_permission", - keyColumn: "id", - keyValue: 519); - - migrationBuilder.DeleteData( - table: "t_permission", - keyColumn: "id", - keyValue: 520); - - migrationBuilder.AlterColumn( - name: "id_category", - table: "t_help_page", - type: "integer", - nullable: false, - comment: "id категории файла", - oldClrType: typeof(int), - oldType: "integer", - oldComment: "Id категории файла"); - } - } -} diff --git a/AsbCloudDb/Migrations/20230710113646_Add_Notifications.Designer.cs b/AsbCloudDb/Migrations/20230711130624_Add_Notification.Designer.cs similarity index 99% rename from AsbCloudDb/Migrations/20230710113646_Add_Notifications.Designer.cs rename to AsbCloudDb/Migrations/20230711130624_Add_Notification.Designer.cs index 5946f800..15714251 100644 --- a/AsbCloudDb/Migrations/20230710113646_Add_Notifications.Designer.cs +++ b/AsbCloudDb/Migrations/20230711130624_Add_Notification.Designer.cs @@ -13,8 +13,8 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace AsbCloudDb.Migrations { [DbContext(typeof(AsbCloudDbContext))] - [Migration("20230710113646_Add_Notifications")] - partial class Add_Notifications + [Migration("20230711130624_Add_Notification")] + partial class Add_Notification { protected override void BuildTargetModel(ModelBuilder modelBuilder) { @@ -991,7 +991,7 @@ namespace AsbCloudDb.Migrations b.Property("IdCategory") .HasColumnType("integer") .HasColumnName("id_category") - .HasComment("Id категории файла"); + .HasComment("id категории файла"); b.Property("Name") .IsRequired() @@ -1168,31 +1168,33 @@ namespace AsbCloudDb.Migrations .HasColumnName("id_notification_category") .HasComment("Id категории уведомления"); - b.Property("IdNotificationTransport") - .HasColumnType("integer") - .HasColumnName("id_notification_transport") - .HasComment("Id способа доставки уведомления"); - b.Property("IdUser") .HasColumnType("integer") .HasColumnName("id_user") .HasComment("Id получателя"); - b.Property("IsRead") - .HasColumnType("boolean") - .HasColumnName("is_read") - .HasComment("Прочитано ли уведомление"); - - b.Property("SentDateAtUtc") - .HasColumnType("timestamp with time zone") - .HasColumnName("sent_date_at_utc") - .HasComment("Дата отправки уведомления"); - - b.Property("Subject") + b.Property("Message") .IsRequired() .HasColumnType("text") - .HasColumnName("subject") - .HasComment("Текст уведомления"); + .HasColumnName("message") + .HasComment("Сообщение уведомления"); + + b.Property("NotificationState") + .IsRequired() + .HasColumnType("text") + .HasColumnName("notification_state") + .HasComment("Состояние уведомления"); + + b.Property("NotificationTransport") + .IsRequired() + .HasColumnType("text") + .HasColumnName("notification_transport") + .HasComment("Метод доставки уведомления"); + + b.Property("SentDate") + .HasColumnType("timestamp with time zone") + .HasColumnName("sent_date") + .HasComment("Дата отправки уведомления"); b.Property("TimeToLife") .HasColumnType("interval") @@ -1209,13 +1211,11 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdNotificationCategory"); - b.HasIndex("IdNotificationTransport"); - b.HasIndex("IdUser"); b.ToTable("t_notification"); - b.HasComment("Уведомлений"); + b.HasComment("Уведомления"); }); modelBuilder.Entity("AsbCloudDb.Model.NotificationCategory", b => @@ -1246,35 +1246,6 @@ namespace AsbCloudDb.Migrations }); }); - modelBuilder.Entity("AsbCloudDb.Model.NotificationTransport", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnName("id"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Name") - .IsRequired() - .HasColumnType("text") - .HasColumnName("name") - .HasComment("Название способа доставки уведомлений"); - - b.HasKey("Id"); - - b.ToTable("t_notification_transport"); - - b.HasComment("Способ доставки уведомлений"); - - b.HasData( - new - { - Id = 1, - Name = "SignalR" - }); - }); - modelBuilder.Entity("AsbCloudDb.Model.OperationValue", b => { b.Property("Id") @@ -7690,12 +7661,6 @@ namespace AsbCloudDb.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("AsbCloudDb.Model.NotificationTransport", "NotificationTransport") - .WithMany("Notifications") - .HasForeignKey("IdNotificationTransport") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - b.HasOne("AsbCloudDb.Model.User", "User") .WithMany() .HasForeignKey("IdUser") @@ -7704,8 +7669,6 @@ namespace AsbCloudDb.Migrations b.Navigation("NotificationCategory"); - b.Navigation("NotificationTransport"); - b.Navigation("User"); }); @@ -8306,11 +8269,6 @@ namespace AsbCloudDb.Migrations b.Navigation("Notifications"); }); - modelBuilder.Entity("AsbCloudDb.Model.NotificationTransport", b => - { - b.Navigation("Notifications"); - }); - modelBuilder.Entity("AsbCloudDb.Model.Permission", b => { b.Navigation("RelationUserRolePermissions"); diff --git a/AsbCloudDb/Migrations/20230711130624_Add_Notification.cs b/AsbCloudDb/Migrations/20230711130624_Add_Notification.cs new file mode 100644 index 00000000..c4c447a8 --- /dev/null +++ b/AsbCloudDb/Migrations/20230711130624_Add_Notification.cs @@ -0,0 +1,85 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace AsbCloudDb.Migrations +{ + public partial class Add_Notification : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "t_notification_category", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + name = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_t_notification_category", x => x.id); + }, + comment: "Категории уведомлений"); + + migrationBuilder.CreateTable( + name: "t_notification", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + id_user = table.Column(type: "integer", nullable: false, comment: "Id получателя"), + id_notification_category = table.Column(type: "integer", nullable: false, comment: "Id категории уведомления"), + title = table.Column(type: "text", nullable: false, comment: "Заголовок уведомления"), + message = table.Column(type: "text", nullable: false, comment: "Сообщение уведомления"), + time_to_life = table.Column(type: "interval", nullable: false, comment: "Время жизни уведомления"), + sent_date = table.Column(type: "timestamp with time zone", nullable: true, comment: "Дата отправки уведомления"), + notification_state = table.Column(type: "text", nullable: false, comment: "Состояние уведомления"), + notification_transport = table.Column(type: "text", nullable: false, comment: "Метод доставки уведомления") + }, + constraints: table => + { + table.PrimaryKey("PK_t_notification", x => x.id); + table.ForeignKey( + name: "FK_t_notification_t_notification_category_id_notification_cate~", + column: x => x.id_notification_category, + principalTable: "t_notification_category", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_t_notification_t_user_id_user", + column: x => x.id_user, + principalTable: "t_user", + principalColumn: "id", + onDelete: ReferentialAction.Cascade); + }, + comment: "Уведомления"); + + migrationBuilder.InsertData( + table: "t_notification_category", + columns: new[] { "id", "name" }, + values: new object[] { 1, "Системные уведомления" }); + + migrationBuilder.CreateIndex( + name: "IX_t_notification_id_notification_category", + table: "t_notification", + column: "id_notification_category"); + + migrationBuilder.CreateIndex( + name: "IX_t_notification_id_user", + table: "t_notification", + column: "id_user"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "t_notification"); + + migrationBuilder.DropTable( + name: "t_notification_category"); + } + } +} diff --git a/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs b/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs index 7e46cc02..005c736a 100644 --- a/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs +++ b/AsbCloudDb/Migrations/AsbCloudDbContextModelSnapshot.cs @@ -1166,31 +1166,33 @@ namespace AsbCloudDb.Migrations .HasColumnName("id_notification_category") .HasComment("Id категории уведомления"); - b.Property("IdNotificationTransport") - .HasColumnType("integer") - .HasColumnName("id_notification_transport") - .HasComment("Id способа доставки уведомления"); - b.Property("IdUser") .HasColumnType("integer") .HasColumnName("id_user") .HasComment("Id получателя"); - b.Property("IsRead") - .HasColumnType("boolean") - .HasColumnName("is_read") - .HasComment("Прочитано ли уведомление"); - - b.Property("SentDateAtUtc") - .HasColumnType("timestamp with time zone") - .HasColumnName("sent_date_at_utc") - .HasComment("Дата отправки уведомления"); - - b.Property("Subject") + b.Property("Message") .IsRequired() .HasColumnType("text") - .HasColumnName("subject") - .HasComment("Текст уведомления"); + .HasColumnName("message") + .HasComment("Сообщение уведомления"); + + b.Property("NotificationState") + .IsRequired() + .HasColumnType("text") + .HasColumnName("notification_state") + .HasComment("Состояние уведомления"); + + b.Property("NotificationTransport") + .IsRequired() + .HasColumnType("text") + .HasColumnName("notification_transport") + .HasComment("Метод доставки уведомления"); + + b.Property("SentDate") + .HasColumnType("timestamp with time zone") + .HasColumnName("sent_date") + .HasComment("Дата отправки уведомления"); b.Property("TimeToLife") .HasColumnType("interval") @@ -1207,13 +1209,11 @@ namespace AsbCloudDb.Migrations b.HasIndex("IdNotificationCategory"); - b.HasIndex("IdNotificationTransport"); - b.HasIndex("IdUser"); b.ToTable("t_notification"); - b.HasComment("Уведомлений"); + b.HasComment("Уведомления"); }); modelBuilder.Entity("AsbCloudDb.Model.NotificationCategory", b => @@ -1244,35 +1244,6 @@ namespace AsbCloudDb.Migrations }); }); - modelBuilder.Entity("AsbCloudDb.Model.NotificationTransport", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("integer") - .HasColumnName("id"); - - NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); - - b.Property("Name") - .IsRequired() - .HasColumnType("text") - .HasColumnName("name") - .HasComment("Название способа доставки уведомлений"); - - b.HasKey("Id"); - - b.ToTable("t_notification_transport"); - - b.HasComment("Способ доставки уведомлений"); - - b.HasData( - new - { - Id = 1, - Name = "SignalR" - }); - }); - modelBuilder.Entity("AsbCloudDb.Model.OperationValue", b => { b.Property("Id") @@ -7688,12 +7659,6 @@ namespace AsbCloudDb.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("AsbCloudDb.Model.NotificationTransport", "NotificationTransport") - .WithMany("Notifications") - .HasForeignKey("IdNotificationTransport") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - b.HasOne("AsbCloudDb.Model.User", "User") .WithMany() .HasForeignKey("IdUser") @@ -7702,8 +7667,6 @@ namespace AsbCloudDb.Migrations b.Navigation("NotificationCategory"); - b.Navigation("NotificationTransport"); - b.Navigation("User"); }); @@ -8304,11 +8267,6 @@ namespace AsbCloudDb.Migrations b.Navigation("Notifications"); }); - modelBuilder.Entity("AsbCloudDb.Model.NotificationTransport", b => - { - b.Navigation("Notifications"); - }); - modelBuilder.Entity("AsbCloudDb.Model.Permission", b => { b.Navigation("RelationUserRolePermissions"); diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 7785b8ee..ea865336 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -77,7 +77,6 @@ namespace AsbCloudDb.Model public DbSet HelpPages => Set(); public DbSet Notifications => Set(); public DbSet NotificationCategories => Set(); - public DbSet NotificationTransports => Set(); public AsbCloudDbContext() : base() { diff --git a/AsbCloudDb/Model/DefaultData/DefaultContextData.cs b/AsbCloudDb/Model/DefaultData/DefaultContextData.cs index 637525f0..d749fc59 100644 --- a/AsbCloudDb/Model/DefaultData/DefaultContextData.cs +++ b/AsbCloudDb/Model/DefaultData/DefaultContextData.cs @@ -26,7 +26,6 @@ namespace AsbCloudDb.Model.DefaultData { typeof(CompanyType), new EntityFillerCompanyType()}, { typeof(Subsystems.Subsystem), new EntityFillerSubsystem() }, { typeof(NotificationCategory), new EntityNotificationCategory()}, - { typeof(NotificationTransport), new EntityNotificationTransport() } }; return fillers; } diff --git a/AsbCloudDb/Model/DefaultData/EntityNotificationTransport.cs b/AsbCloudDb/Model/DefaultData/EntityNotificationTransport.cs deleted file mode 100644 index 7a07611a..00000000 --- a/AsbCloudDb/Model/DefaultData/EntityNotificationTransport.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace AsbCloudDb.Model.DefaultData; - -public class EntityNotificationTransport : EntityFiller -{ - public override NotificationTransport[] GetData() => new NotificationTransport[] - { - new() { Id = 1, Name = "SignalR" } - }; -} \ No newline at end of file diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index 37f2919c..90fc7582 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -70,8 +70,6 @@ namespace AsbCloudDb.Model DbSet HelpPages { get; } DbSet Notifications { get; } DbSet NotificationCategories { get; } - DbSet NotificationTransports { get; } - DatabaseFacade Database { get; } Task RefreshMaterializedViewAsync(string mwName, CancellationToken token); diff --git a/AsbCloudDb/Model/Notification.cs b/AsbCloudDb/Model/Notification.cs index d9f63f7a..af5b55ee 100644 --- a/AsbCloudDb/Model/Notification.cs +++ b/AsbCloudDb/Model/Notification.cs @@ -5,7 +5,7 @@ using Microsoft.EntityFrameworkCore; namespace AsbCloudDb.Model; -[Table("t_notification"), Comment("Уведомлений")] +[Table("t_notification"), Comment("Уведомления")] public class Notification : IId { [Key] @@ -14,31 +14,28 @@ public class Notification : IId [Column("id_user"), Comment("Id получателя")] public int IdUser { get; set; } - - [Column("id_notification_transport"), Comment("Id способа доставки уведомления")] - public int IdNotificationTransport { get; set; } - + [Column("id_notification_category"), Comment("Id категории уведомления")] public int IdNotificationCategory { get; set; } [Column("title"), Comment("Заголовок уведомления")] public string Title { get; set; } = null!; - [Column("subject"), Comment("Текст уведомления")] - public string Subject { get; set; } = null!; + [Column("message"), Comment("Сообщение уведомления")] + public string Message { get; set; } = null!; [Column("time_to_life"), Comment("Время жизни уведомления")] public TimeSpan TimeToLife { get; set; } - [Column("sent_date_at_utc"), Comment("Дата отправки уведомления")] - public DateTime? SentDateAtUtc { get; set; } - - [Column("is_read"), Comment("Прочитано ли уведомление")] - public bool? IsRead { get; set; } + [Column("sent_date"), Comment("Дата отправки уведомления")] + public DateTime? SentDate { get; set; } + + [Column("notification_state"), Comment("Состояние уведомления")] + public string NotificationState { get; set; } = null!; + + [Column("notification_transport"), Comment("Метод доставки уведомления")] + public string NotificationTransport { get; set; } = null!; - [ForeignKey(nameof(IdNotificationTransport))] - public virtual NotificationTransport NotificationTransport { get; set; } = null!; - [ForeignKey(nameof(IdNotificationCategory))] public virtual NotificationCategory NotificationCategory { get; set; } = null!; diff --git a/AsbCloudInfrastructure/Repository/NotificationRepository.cs b/AsbCloudInfrastructure/Repository/NotificationRepository.cs index c14ba518..df6f4222 100644 --- a/AsbCloudInfrastructure/Repository/NotificationRepository.cs +++ b/AsbCloudInfrastructure/Repository/NotificationRepository.cs @@ -5,57 +5,54 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Repositories; +using AsbCloudApp.Requests; using AsbCloudDb; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Caching.Memory; namespace AsbCloudInfrastructure.Repository; -public class NotificationRepository : CrudRepositoryBase, INotificationRepository +public class NotificationRepository : CrudCacheRepositoryBase, INotificationRepository { - public NotificationRepository(IAsbCloudDbContext context) : base(context) - { - } + private static IQueryable MakeQueryNotification(DbSet dbSet) + => dbSet.AsNoTracking() + .Include(n => n.NotificationCategory); - public NotificationRepository(IAsbCloudDbContext context, - Func, IQueryable> makeQuery) : base(context, makeQuery) + public NotificationRepository(IAsbCloudDbContext dbContext, IMemoryCache memoryCache) + : base(dbContext, memoryCache, MakeQueryNotification) { } public async Task> GetUnsentNotificationsAsync(int idUser, - int idNotificationTransport, + NotificationTransport notificationTransport, CancellationToken cancellationToken) { - var notifications = await dbContext.Notifications + var notifications = (await GetCacheAsync(cancellationToken)) .Where(x => x.IdUser == idUser && - x.IdNotificationTransport == idNotificationTransport && - x.SentDateAtUtc == null) - .Include(x => x.NotificationTransport) - .Include(x => x.NotificationCategory) - .ToListAsync(cancellationToken); + x.NotificationTransport == notificationTransport.ToString() && + x.SentDate == null); - return notifications.Select(x => x.Adapt()); + return notifications.Select(Convert); } - public async Task> GetNotificationsAsync(int? skip, - int? take, - int idUser, - int idNotificationTransport, + public async Task> GetNotificationsAsync(int idUser, + NotificationRequest request, CancellationToken cancellationToken) { - skip ??= 0; - take ??= 10; + request.Skip ??= 0; + request.Take ??= 10; var query = dbContext.Notifications - .Where(x => x.IdNotificationTransport == idNotificationTransport && + .Where(x => x.NotificationTransport == request.NotificationTransport.ToString() && x.IdUser == idUser && - x.SentDateAtUtc != null); + x.SentDate != null); var result = new PaginationContainer() { - Skip = skip.Value, - Take = take.Value, + Skip = request.Skip.Value, + Take = request.Take.Value, Count = await query.CountAsync(cancellationToken), }; @@ -63,13 +60,32 @@ public class NotificationRepository : CrudRepositoryBase x.SentDateAtUtc) - .SkipTake(skip, take) + .SortBy(request.SortFields) + .SkipTake(request.Skip, request.Take) .Include(x => x.NotificationCategory) - .Include(x => x.NotificationTransport) .Select(x => x.Adapt()) .ToListAsync(cancellationToken); return result; } + + protected override Notification Convert(NotificationDto src) + { + var entity = src.Adapt(); + + entity.NotificationState = src.NotificationState.ToString(); + entity.NotificationTransport = src.NotificationTransport.ToString(); + + return entity; + } + + protected override NotificationDto Convert(Notification src) + { + var dto = src.Adapt(); + + dto.NotificationState = (NotificationState)Enum.Parse(typeof(NotificationState), src.NotificationState); + dto.NotificationTransport = (NotificationTransport)Enum.Parse(typeof(NotificationTransport), src.NotificationTransport); + + return dto; + } } \ No newline at end of file