From 4c06e83a546028990eb25eb8cef3ee41b93a3002 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Fri, 22 Sep 2023 19:44:41 +0300 Subject: [PATCH 01/22] feat: add HangFire settings to startup and app settings. feat: new task delayed notification --- .../HwProj.CoursesService.API.csproj | 3 +++ .../Services/TasksService.cs | 13 ++++++++++++- .../HwProj.CoursesService.API/Startup.cs | 19 ++++++++++++++++++- .../appsettings.json | 6 ++++-- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj b/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj index 0c82fe45c..9e1c91e6e 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj +++ b/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj @@ -13,6 +13,9 @@ + + + diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index e99ea1203..3e3fdd184 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -1,11 +1,14 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using AutoMapper; +using Hangfire; using HwProj.CoursesService.API.Events; using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.CoursesService.ViewModels; +using static System.TimeSpan; namespace HwProj.CoursesService.API.Services { @@ -45,7 +48,15 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var taskId = await _tasksRepository.AddAsync(task); if (task.PublicationDate <= DateTimeUtils.GetMoscowNow()) + { _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, courseModel)); + } + else + { + BackgroundJob.Schedule(() + => _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, courseModel)), + task.PublicationDate.Subtract(FromHours(3))); + } return taskId; } diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs index 15f05f2cd..9c88c4d91 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs @@ -1,4 +1,8 @@ -using HwProj.AuthService.Client; +using System; +using System.Threading.Channels; +using Hangfire; +using Hangfire.SqlServer; +using HwProj.AuthService.Client; using HwProj.CoursesService.API.Filters; using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; @@ -25,6 +29,17 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { var connectionString = ConnectionString.GetConnectionString(Configuration); + + // Add Hangfire services. + services.AddHangfire(configuration => configuration + .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) + .UseSimpleAssemblyNameTypeSerializer() + .UseRecommendedSerializerSettings() + .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))); + + // Add the processing server as IHostedService + services.AddHangfireServer(); + services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); @@ -50,6 +65,8 @@ public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IHostingEnvironment env, CourseContext context) { app.ConfigureHwProj(env, "Courses API", context); + app.UseHangfireDashboard(); + BackgroundJob.Enqueue(() => Console.WriteLine("Hello world !")); } } } \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json b/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json index cd71b77f6..705867c67 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json +++ b/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json @@ -1,11 +1,13 @@ { "ConnectionStrings": { "DefaultConnectionForWindows": "Server=(localdb)\\mssqllocaldb;Database=CoursesServiceDB;Trusted_Connection=True;", - "DefaultConnectionForLinux": "Server=localhost,1433;Database=CoursesServiceDB;User ID=SA;Password=password_1234;" + "DefaultConnectionForLinux": "Server=localhost,1433;Database=CoursesServiceDB;User ID=SA;Password=password_1234;", + "HangfireConnection": "Server=(localdb)\\mssqllocaldb;Database=HangfireDB;Trusted_Connection=True;" }, "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Warning", + "Hangfire": "Information" } }, "AllowedHosts": "*", From 248c31636cda3d85a0de9392e2803534a5beec6a Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Mon, 25 Sep 2023 21:44:38 +0300 Subject: [PATCH 02/22] feat: add delete and update delayed methods for tasks --- .../Services/TasksService.cs | 52 ++++++++++++++----- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index 3e3fdd184..e59c3cd2d 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading.Tasks; using AutoMapper; using Hangfire; @@ -22,7 +23,8 @@ public class TasksService : ITasksService private readonly ICoursesService _coursesService; public TasksService(ITasksRepository tasksRepository, IEventBus eventBus, IMapper mapper, - ICoursesRepository coursesRepository, IHomeworksRepository homeworksRepository, ICoursesService coursesService) + ICoursesRepository coursesRepository, IHomeworksRepository homeworksRepository, + ICoursesService coursesService) { _tasksRepository = tasksRepository; _homeworksRepository = homeworksRepository; @@ -46,23 +48,19 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var courseModel = _mapper.Map(course); var taskId = await _tasksRepository.AddAsync(task); - - if (task.PublicationDate <= DateTimeUtils.GetMoscowNow()) - { - _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, courseModel)); - } - else - { - BackgroundJob.Schedule(() - => _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, courseModel)), - task.PublicationDate.Subtract(FromHours(3))); - } + BackgroundJob.Schedule(() + => _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, + courseModel)), + task.PublicationDate.Subtract(FromHours(3))); return taskId; } public async Task DeleteTaskAsync(long taskId) { + var task = await _tasksRepository.GetAsync(taskId); + DeleteJobsByTask(task); + await _tasksRepository.DeleteAsync(taskId); } @@ -73,7 +71,6 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) var homework = await _homeworksRepository.GetAsync(task.HomeworkId); var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId); var courseModel = _mapper.Map(course); - _eventBus.Publish(new UpdateTaskMaxRatingEvent(courseModel, taskModel, update.MaxRating)); await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask() { @@ -85,6 +82,33 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) IsDeadlineStrict = update.IsDeadlineStrict, PublicationDate = update.PublicationDate }); + + DeleteJobsByTask(task); + + BackgroundJob.Schedule(() + => _eventBus.Publish(new UpdateTaskMaxRatingEvent(courseModel, taskModel, update.MaxRating)), + update.PublicationDate.Subtract(FromHours(3))); + } + + + private void DeleteJobsByTask(HomeworkTask task) + { + if (task.PublicationDate <= DateTimeUtils.GetMoscowNow()) return; + + var monitor = JobStorage.Current.GetMonitoringApi(); + var jobsScheduled = monitor.ScheduledJobs(0, int.MaxValue) + .Where(x => x.Value.Job.Method.Name == nameof(_eventBus.Publish)); + + foreach (var job in jobsScheduled) + { + if ((job.Value.Job.Args[0] is NewHomeworkTaskEvent newHomeworkTaskEvent + && newHomeworkTaskEvent.TaskId == task.Id) || + (job.Value.Job.Args[0] is UpdateTaskMaxRatingEvent updateTaskMaxRatingEvent + && updateTaskMaxRatingEvent.Task.Id == task.Id)) + { + BackgroundJob.Delete(job.Key); + } + } } } -} +} \ No newline at end of file From 4443ebbbd546453c9df626cf7ea471fa42b63f4a Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Fri, 6 Oct 2023 19:13:16 +0300 Subject: [PATCH 03/22] feat: Moved hangfire to NotificationsService --- .../HwProj.CoursesService.API.csproj | 3 -- .../Services/TasksService.cs | 39 ++----------------- .../HwProj.CoursesService.API/Startup.cs | 12 ------ .../appsettings.json | 4 +- .../HwProj.NotificationsService.API.csproj | 3 ++ .../Startup.cs | 16 +++++++- .../appsettings.json | 6 ++- 7 files changed, 27 insertions(+), 56 deletions(-) diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj b/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj index 9e1c91e6e..0c82fe45c 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj +++ b/HwProj.CoursesService/HwProj.CoursesService.API/HwProj.CoursesService.API.csproj @@ -13,9 +13,6 @@ - - - diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index e59c3cd2d..ac80baed6 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Threading.Tasks; using AutoMapper; -using Hangfire; using HwProj.CoursesService.API.Events; using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; @@ -48,19 +47,15 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var courseModel = _mapper.Map(course); var taskId = await _tasksRepository.AddAsync(task); - BackgroundJob.Schedule(() - => _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, - courseModel)), - task.PublicationDate.Subtract(FromHours(3))); + + if (task.PublicationDate <= DateTimeUtils.GetMoscowNow()) + _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, courseModel)); return taskId; } public async Task DeleteTaskAsync(long taskId) { - var task = await _tasksRepository.GetAsync(taskId); - DeleteJobsByTask(task); - await _tasksRepository.DeleteAsync(taskId); } @@ -71,6 +66,7 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) var homework = await _homeworksRepository.GetAsync(task.HomeworkId); var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId); var courseModel = _mapper.Map(course); + _eventBus.Publish(new UpdateTaskMaxRatingEvent(courseModel, taskModel, update.MaxRating)); await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask() { @@ -82,33 +78,6 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) IsDeadlineStrict = update.IsDeadlineStrict, PublicationDate = update.PublicationDate }); - - DeleteJobsByTask(task); - - BackgroundJob.Schedule(() - => _eventBus.Publish(new UpdateTaskMaxRatingEvent(courseModel, taskModel, update.MaxRating)), - update.PublicationDate.Subtract(FromHours(3))); - } - - - private void DeleteJobsByTask(HomeworkTask task) - { - if (task.PublicationDate <= DateTimeUtils.GetMoscowNow()) return; - - var monitor = JobStorage.Current.GetMonitoringApi(); - var jobsScheduled = monitor.ScheduledJobs(0, int.MaxValue) - .Where(x => x.Value.Job.Method.Name == nameof(_eventBus.Publish)); - - foreach (var job in jobsScheduled) - { - if ((job.Value.Job.Args[0] is NewHomeworkTaskEvent newHomeworkTaskEvent - && newHomeworkTaskEvent.TaskId == task.Id) || - (job.Value.Job.Args[0] is UpdateTaskMaxRatingEvent updateTaskMaxRatingEvent - && updateTaskMaxRatingEvent.Task.Id == task.Id)) - { - BackgroundJob.Delete(job.Key); - } - } } } } \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs index 9c88c4d91..e5afb9271 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs @@ -1,7 +1,5 @@ using System; using System.Threading.Channels; -using Hangfire; -using Hangfire.SqlServer; using HwProj.AuthService.Client; using HwProj.CoursesService.API.Filters; using HwProj.CoursesService.API.Models; @@ -30,16 +28,6 @@ public void ConfigureServices(IServiceCollection services) { var connectionString = ConnectionString.GetConnectionString(Configuration); - // Add Hangfire services. - services.AddHangfire(configuration => configuration - .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) - .UseSimpleAssemblyNameTypeSerializer() - .UseRecommendedSerializerSettings() - .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))); - - // Add the processing server as IHostedService - services.AddHangfireServer(); - services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json b/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json index 705867c67..2db942611 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json +++ b/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json @@ -1,13 +1,11 @@ { "ConnectionStrings": { "DefaultConnectionForWindows": "Server=(localdb)\\mssqllocaldb;Database=CoursesServiceDB;Trusted_Connection=True;", - "DefaultConnectionForLinux": "Server=localhost,1433;Database=CoursesServiceDB;User ID=SA;Password=password_1234;", - "HangfireConnection": "Server=(localdb)\\mssqllocaldb;Database=HangfireDB;Trusted_Connection=True;" + "DefaultConnectionForLinux": "Server=localhost,1433;Database=CoursesServiceDB;User ID=SA;Password=password_1234;" }, "Logging": { "LogLevel": { "Default": "Warning", - "Hangfire": "Information" } }, "AllowedHosts": "*", diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj b/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj index 24abc2bd8..a1e893393 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj @@ -15,6 +15,9 @@ + + + diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index c6abaddfe..d8f83795f 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -6,6 +6,8 @@ using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using HwProj.Utils.Configuration; +using Hangfire; +using Hangfire.SqlServer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; @@ -29,6 +31,17 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { var connectionString = ConnectionString.GetConnectionString(Configuration); + + // Add Hangfire services. + services.AddHangfire(configuration => configuration + .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) + .UseSimpleAssemblyNameTypeSerializer() + .UseRecommendedSerializerSettings() + .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))); + + // Add the processing server as IHostedService + services.AddHangfireServer(); + services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); @@ -73,7 +86,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); } - + + app.UseHangfireDashboard(); app.ConfigureHwProj(env, "Notifications API", context); } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/appsettings.json b/HwProj.NotificationsService/HwProj.NotificationsService.API/appsettings.json index 6f9707df9..4a4d36366 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/appsettings.json +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/appsettings.json @@ -1,11 +1,13 @@ { "ConnectionStrings": { "DefaultConnectionForWindows": "Server=(localdb)\\mssqllocaldb;Database=NotificationsServiceDB;Trusted_Connection=True;MultipleActiveResultSets=True", - "DefaultConnectionForLinux": "Server=localhost,1433;Database=NotificationsServiceDB;User ID=SA;Password=password_1234;" + "DefaultConnectionForLinux": "Server=localhost,1433;Database=NotificationsServiceDB;User ID=SA;Password=password_1234;", + "HangfireConnection": "Server=(localdb)\\mssqllocaldb;Database=HangfireDB;Trusted_Connection=True;" }, "Logging": { "LogLevel": { - "Default": "Warning" + "Default": "Warning", + "Hangfire": "Information" } }, "AllowedHosts": "*", From 8557b6c58d7246df3c48dc41f6271d2b4021279b Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Mon, 9 Oct 2023 00:40:38 +0300 Subject: [PATCH 04/22] feat: create ScheduleEvent feat: create ScheduleEventHandler + UpdateScheduleEventHandler abstract classes feat: Create ScheduleWorksRepository feat: Add schedule homework task --- .../NotificationsService/ScheduleWork.cs | 17 +++++++++++++ .../Events/NewHomeworkTaskEvent.cs | 8 +++--- .../Services/TasksService.cs | 4 +-- .../HwProj.EventBus.Client/Event.cs | 16 ++++++++++++ .../Interfaces/IEventHandler.cs | 12 +++++++++ .../NewHomeworkTaskEventHandler.cs | 25 ++++++++++++++++++- .../Models/NotificationsContext.cs | 6 +++++ .../Repositories/IScheduleWorksRepository.cs | 9 +++++++ .../Repositories/ScheduleWorksRepository.cs | 13 ++++++++++ .../Startup.cs | 3 ++- 10 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs create mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs create mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs new file mode 100644 index 000000000..926f41bde --- /dev/null +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -0,0 +1,17 @@ +using System; +using System.ComponentModel.DataAnnotations; +using HwProj.Repositories; + +namespace HwProj.Models.NotificationsService +{ + public class ScheduleWork : IEntity + { + [Key] public long Id { get; set; } + + public long ScheduleWorkId { get; set; } + + public string JobId { get; set; } + + public Type ScheduleWorkType { get; set; } + } +} \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs index 3a5ed857a..b3bdea6ea 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs @@ -4,9 +4,11 @@ namespace HwProj.CoursesService.API.Events { - public class NewHomeworkTaskEvent : Event + public class NewHomeworkTaskEvent : ScheduleEvent { - public NewHomeworkTaskEvent(string taskTitle, long taskId, DateTime? deadline, CourseDTO course) + public NewHomeworkTaskEvent(string taskTitle, long taskId, DateTime? deadline, + DateTime publicationDate,CourseDTO course, Type type) + : base(taskId, publicationDate, type) { TaskTitle = taskTitle; TaskId = taskId; @@ -19,4 +21,4 @@ public NewHomeworkTaskEvent(string taskTitle, long taskId, DateTime? deadline, C public DateTime? Deadline { get; set; } public CourseDTO Course { get; set; } } -} +} \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index ac80baed6..e0685c007 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -48,8 +48,8 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var taskId = await _tasksRepository.AddAsync(task); - if (task.PublicationDate <= DateTimeUtils.GetMoscowNow()) - _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, courseModel)); + if (task.PublicationDate > DateTimeUtils.GetMoscowNow()) + _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, task.PublicationDate, courseModel, typeof(HomeworkTask))); return taskId; } diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs index 8a62cbd96..0bc9eaf86 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs @@ -23,4 +23,20 @@ public Event(Guid id, DateTime data) CreationData = data; } } + + public class ScheduleEvent : Event + { + public long ScheduleWorkId { get; set; } + + public Type Type { get; set; } + + public DateTime PublicationDate { get; set; } + + protected ScheduleEvent(long scheduleWorkId, DateTime publicationDate, Type type) + { + ScheduleWorkId = scheduleWorkId; + PublicationDate = publicationDate; + Type = type; + } + } } diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs b/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs index 35ceac94b..18c4dbd95 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs @@ -14,4 +14,16 @@ public Task HandleAsync(Event @event) => public abstract Task HandleAsync(TEvent @event); } + + public abstract class ScheduleEventHandlerBase + : EventHandlerBase where TScheduleEvent : ScheduleEvent + { + public abstract Task ScheduleWorkAsync(TScheduleEvent @event); + } + + public abstract class UpdateScheduleEventHandlerBase + : ScheduleEventHandlerBase where TScheduleEvent : ScheduleEvent + { + public abstract Task DeletePreviousScheduleWorkAsync(TScheduleEvent @event); + } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index ba832df25..b85a32313 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -1,3 +1,4 @@ +using System; using System.Linq; using System.Threading.Tasks; using HwProj.AuthService.Client; @@ -6,31 +7,53 @@ using HwProj.NotificationsService.API.Repositories; using HwProj.CoursesService.API.Events; using HwProj.Models; +using Hangfire; +using HwProj.EventBus.Client; using HwProj.NotificationsService.API.Services; +using Microsoft.Azure.KeyVault.Models; using Microsoft.Extensions.Configuration; namespace HwProj.NotificationsService.API.EventHandlers { - public class NewHomeworkTaskEventHandler : EventHandlerBase + public class NewHomeworkTaskEventHandler : ScheduleEventHandlerBase { private readonly INotificationsRepository _notificationRepository; + private readonly IScheduleWorksRepository _scheduleWorksRepository; private readonly IAuthServiceClient _authServiceClient; private readonly IConfigurationSection _configuration; private readonly IEmailService _emailService; public NewHomeworkTaskEventHandler( INotificationsRepository notificationRepository, + IScheduleWorksRepository scheduleWorksRepository, IAuthServiceClient authServiceClient, IConfiguration configuration, IEmailService emailService) { _notificationRepository = notificationRepository; + _scheduleWorksRepository = scheduleWorksRepository; _authServiceClient = authServiceClient; _emailService = emailService; _configuration = configuration.GetSection("Notification"); } public override async Task HandleAsync(NewHomeworkTaskEvent @event) + { + var jobId = BackgroundJob.Schedule(() => ScheduleWorkAsync(@event), + @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); + + var scheduleWork = new ScheduleWork + { + ScheduleWorkId = @event.TaskId, + JobId = jobId, + ScheduleWorkType = @event.Type + }; + + await _scheduleWorksRepository.AddAsync(scheduleWork); + } + + + public override async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event) { var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index d5ae11961..d588cfdab 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -7,6 +7,7 @@ public sealed class NotificationsContext : DbContext { public DbSet Notifications { get; set; } public DbSet Settings { get; set; } + public DbSet ScheduleWorks { get; set; } public NotificationsContext(DbContextOptions options) : base(options) @@ -18,5 +19,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().HasIndex(n => n.UserId); modelBuilder.Entity().HasKey(n => new { n.UserId, n.Category }); } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().Ignore(work => work.ScheduleWorkType); + } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs new file mode 100644 index 000000000..354dddc14 --- /dev/null +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs @@ -0,0 +1,9 @@ +using HwProj.Models.NotificationsService; +using HwProj.Repositories; + +namespace HwProj.NotificationsService.API.Repositories; + +public interface IScheduleWorksRepository : ICrudRepository +{ + +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs new file mode 100644 index 000000000..0b525977a --- /dev/null +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs @@ -0,0 +1,13 @@ +using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Models; +using HwProj.Repositories; + +namespace HwProj.NotificationsService.API.Repositories +{ + public class ScheduleWorksRepository : CrudRepository, IScheduleWorksRepository + { + public ScheduleWorksRepository(NotificationsContext context) : base(context) + { + } + } +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index d8f83795f..8c8b1f798 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -45,6 +45,7 @@ public void ConfigureServices(IServiceCollection services) services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddEventBus(Configuration); services.AddTransient, RegisterEventHandler>(); services.AddTransient, RateEventHandler>(); @@ -63,7 +64,7 @@ public void ConfigureServices(IServiceCollection services) services.AddHttpClient(); services.AddAuthServiceClient(); - + services.ConfigureHwProjServices("Notifications API"); } From 6e0d6caf03f80a21c0b576fe578feba34701fcc8 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Mon, 9 Oct 2023 01:19:27 +0300 Subject: [PATCH 05/22] feat: add UpdateScheduleEvent class --- .../NotificationsService/ScheduleWork.cs | 10 +++---- .../Events/NewHomeworkTaskEvent.cs | 4 +-- .../Services/TasksService.cs | 2 +- .../HwProj.EventBus.Client/Event.cs | 26 ++++++++++++------- .../Interfaces/IEventHandler.cs | 8 +++--- .../NewHomeworkTaskEventHandler.cs | 6 ++--- .../Models/NotificationsContext.cs | 2 +- 7 files changed, 32 insertions(+), 26 deletions(-) diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index 926f41bde..94dbb7ff1 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -6,12 +6,12 @@ namespace HwProj.Models.NotificationsService { public class ScheduleWork : IEntity { - [Key] public long Id { get; set; } - - public long ScheduleWorkId { get; set; } + [Key] + public long Id { get; set; } + + [Key] + public Type Type { get; set; } public string JobId { get; set; } - - public Type ScheduleWorkType { get; set; } } } \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs index b3bdea6ea..d00d7d0af 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs @@ -7,8 +7,8 @@ namespace HwProj.CoursesService.API.Events public class NewHomeworkTaskEvent : ScheduleEvent { public NewHomeworkTaskEvent(string taskTitle, long taskId, DateTime? deadline, - DateTime publicationDate,CourseDTO course, Type type) - : base(taskId, publicationDate, type) + DateTime publicationDate,CourseDTO course) + : base(taskId, publicationDate) { TaskTitle = taskTitle; TaskId = taskId; diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index e0685c007..f14ca3434 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -49,7 +49,7 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var taskId = await _tasksRepository.AddAsync(task); if (task.PublicationDate > DateTimeUtils.GetMoscowNow()) - _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, task.PublicationDate, courseModel, typeof(HomeworkTask))); + _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, task.PublicationDate, courseModel)); return taskId; } diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs index 0bc9eaf86..426b0d907 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs @@ -5,11 +5,9 @@ namespace HwProj.EventBus.Client { public class Event { - [JsonProperty] - public Guid Id { get; set; } + [JsonProperty] public Guid Id { get; set; } - [JsonProperty] - public DateTime CreationData { get; set; } + [JsonProperty] public DateTime CreationData { get; set; } public Event() { @@ -27,16 +25,24 @@ public Event(Guid id, DateTime data) public class ScheduleEvent : Event { public long ScheduleWorkId { get; set; } - - public Type Type { get; set; } - + public DateTime PublicationDate { get; set; } - protected ScheduleEvent(long scheduleWorkId, DateTime publicationDate, Type type) + protected ScheduleEvent(long scheduleWorkId, DateTime publicationDate) { ScheduleWorkId = scheduleWorkId; PublicationDate = publicationDate; - Type = type; } } -} + + public class UpdateScheduleEvent : ScheduleEvent + { + public Type PreviousScheduleEventType { get; set; } + + protected UpdateScheduleEvent(long scheduleWorkId, DateTime publicationDate, Type previousScheduleEventType) + : base(scheduleWorkId, publicationDate) + { + PreviousScheduleEventType = previousScheduleEventType; + } + } +} \ No newline at end of file diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs b/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs index 18c4dbd95..cc7c1f959 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs @@ -18,12 +18,12 @@ public Task HandleAsync(Event @event) => public abstract class ScheduleEventHandlerBase : EventHandlerBase where TScheduleEvent : ScheduleEvent { - public abstract Task ScheduleWorkAsync(TScheduleEvent @event); + protected abstract Task ScheduleWorkAsync(TScheduleEvent @event); } - public abstract class UpdateScheduleEventHandlerBase - : ScheduleEventHandlerBase where TScheduleEvent : ScheduleEvent + public abstract class UpdateScheduleEventHandlerBase + : ScheduleEventHandlerBase where TUpdateScheduleEvent : UpdateScheduleEvent { - public abstract Task DeletePreviousScheduleWorkAsync(TScheduleEvent @event); + protected abstract Task DeletePreviousScheduleWorkAsync(TUpdateScheduleEvent @event); } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index b85a32313..4ffffc33f 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -44,16 +44,16 @@ public override async Task HandleAsync(NewHomeworkTaskEvent @event) var scheduleWork = new ScheduleWork { - ScheduleWorkId = @event.TaskId, + Id = @event.TaskId, JobId = jobId, - ScheduleWorkType = @event.Type + Type = typeof(NewHomeworkTaskEvent) }; await _scheduleWorksRepository.AddAsync(scheduleWork); } - public override async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event) + protected override async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event) { var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index d588cfdab..03337e785 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -22,7 +22,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().Ignore(work => work.ScheduleWorkType); + modelBuilder.Entity().Ignore(work => work.Type); } } } From 20d7fbf0d7223bf46f6091ffd1f6976dc77ef67f Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Mon, 23 Oct 2023 22:32:21 +0300 Subject: [PATCH 06/22] fix: delete delayed logic from CourseService fix: schedule works not implement IRepository interfaces fix: change ScheduleWork primary key --- .../NotificationsService/ScheduleWork.cs | 36 +++++++++++++------ HwProj.Common/HwProj.Repositories/IEntity.cs | 2 +- .../Events/NewHomeworkTaskEvent.cs | 22 +++++++----- .../Services/TasksService.cs | 3 ++ .../HwProj.EventBus.Client/Event.cs | 24 ------------- .../Interfaces/IEventHandler.cs | 12 ------- .../NewHomeworkTaskEventHandler.cs | 27 +++++++++----- .../Models/NotificationsContext.cs | 6 ++-- .../Repositories/IScheduleWorksRepository.cs | 11 +++--- .../Repositories/ScheduleWorksRepository.cs | 33 ++++++++++++++--- .../Startup.cs | 2 +- 11 files changed, 101 insertions(+), 77 deletions(-) diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index 94dbb7ff1..d336b64d0 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -1,16 +1,30 @@ -using System; -using System.ComponentModel.DataAnnotations; -using HwProj.Repositories; - -namespace HwProj.Models.NotificationsService +namespace HwProj.Models.NotificationsService { - public class ScheduleWork : IEntity + public class ScheduleWork { - [Key] - public long Id { get; set; } - - [Key] - public Type Type { get; set; } + private long _taskId; + private long _homeworkId; + private long _courseId; + + public long? TaskId + { + get => _taskId; + set => _taskId = value ?? -1; + } + + public long? HomeworkId + { + get => _homeworkId; + set => _homeworkId = value ?? -1; + } + + public long? CourseId + { + get => _courseId; + set => _courseId = value ?? -1; + } + + public string CategoryId { get; set; } public string JobId { get; set; } } diff --git a/HwProj.Common/HwProj.Repositories/IEntity.cs b/HwProj.Common/HwProj.Repositories/IEntity.cs index 3a9ebbad0..4938858af 100644 --- a/HwProj.Common/HwProj.Repositories/IEntity.cs +++ b/HwProj.Common/HwProj.Repositories/IEntity.cs @@ -2,7 +2,7 @@ namespace HwProj.Repositories { - public interface IEntity + public interface IEntity where TKey : IEquatable { TKey Id { get; set; } diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs index d00d7d0af..d33b5dbe9 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs @@ -4,21 +4,27 @@ namespace HwProj.CoursesService.API.Events { - public class NewHomeworkTaskEvent : ScheduleEvent + public class NewHomeworkTaskEvent : Event { + public string TaskTitle { get; set; } + + public long TaskId { get; set; } + + public DateTime? Deadline { get; set; } + + public DateTime PublicationDate { get; set; } + + public CourseDTO Course { get; set; } + + public NewHomeworkTaskEvent(string taskTitle, long taskId, DateTime? deadline, - DateTime publicationDate,CourseDTO course) - : base(taskId, publicationDate) + DateTime publicationDate, CourseDTO course) { TaskTitle = taskTitle; TaskId = taskId; Deadline = deadline; + PublicationDate = publicationDate; Course = course; } - - public string TaskTitle { get; set; } - public long TaskId { get; set; } - public DateTime? Deadline { get; set; } - public CourseDTO Course { get; set; } } } \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index f14ca3434..faec7da03 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using AutoMapper; using HwProj.CoursesService.API.Events; using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; +using HwProj.EventBus.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.CoursesService.ViewModels; @@ -56,6 +58,7 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) public async Task DeleteTaskAsync(long taskId) { + var task = await _tasksRepository.GetAsync(taskId); await _tasksRepository.DeleteAsync(taskId); } diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs index 426b0d907..1260e0299 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs @@ -21,28 +21,4 @@ public Event(Guid id, DateTime data) CreationData = data; } } - - public class ScheduleEvent : Event - { - public long ScheduleWorkId { get; set; } - - public DateTime PublicationDate { get; set; } - - protected ScheduleEvent(long scheduleWorkId, DateTime publicationDate) - { - ScheduleWorkId = scheduleWorkId; - PublicationDate = publicationDate; - } - } - - public class UpdateScheduleEvent : ScheduleEvent - { - public Type PreviousScheduleEventType { get; set; } - - protected UpdateScheduleEvent(long scheduleWorkId, DateTime publicationDate, Type previousScheduleEventType) - : base(scheduleWorkId, publicationDate) - { - PreviousScheduleEventType = previousScheduleEventType; - } - } } \ No newline at end of file diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs b/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs index cc7c1f959..35ceac94b 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Interfaces/IEventHandler.cs @@ -14,16 +14,4 @@ public Task HandleAsync(Event @event) => public abstract Task HandleAsync(TEvent @event); } - - public abstract class ScheduleEventHandlerBase - : EventHandlerBase where TScheduleEvent : ScheduleEvent - { - protected abstract Task ScheduleWorkAsync(TScheduleEvent @event); - } - - public abstract class UpdateScheduleEventHandlerBase - : ScheduleEventHandlerBase where TUpdateScheduleEvent : UpdateScheduleEvent - { - protected abstract Task DeletePreviousScheduleWorkAsync(TUpdateScheduleEvent @event); - } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index 4ffffc33f..141f2dd08 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -1,5 +1,7 @@ using System; using System.Linq; +using System.Net.Sockets; +using System.Threading; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; @@ -15,7 +17,7 @@ namespace HwProj.NotificationsService.API.EventHandlers { - public class NewHomeworkTaskEventHandler : ScheduleEventHandlerBase + public class NewHomeworkTaskEventHandler : EventHandlerBase { private readonly INotificationsRepository _notificationRepository; private readonly IScheduleWorksRepository _scheduleWorksRepository; @@ -39,21 +41,26 @@ public NewHomeworkTaskEventHandler( public override async Task HandleAsync(NewHomeworkTaskEvent @event) { - var jobId = BackgroundJob.Schedule(() => ScheduleWorkAsync(@event), + (long? taskId, long? homeworkId, long? courseId, string categoryId) id + = (@event.TaskId, null, @event.Course.Id, "Task"); + + var jobId = BackgroundJob.Schedule(() => ScheduleWorkAsync(@event, id), @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); var scheduleWork = new ScheduleWork { - Id = @event.TaskId, - JobId = jobId, - Type = typeof(NewHomeworkTaskEvent) + TaskId = id.taskId, + HomeworkId = id.homeworkId, + CourseId = id.courseId, + CategoryId = id.categoryId, + JobId = jobId }; - await _scheduleWorksRepository.AddAsync(scheduleWork); } - - protected override async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event) + + public async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event, + (long? taskId, long? homeworkId, long? courseId, string categoryId) id) { var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); @@ -79,6 +86,8 @@ protected override async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } + + await _scheduleWorksRepository.DeleteAsync(id); } } -} +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index 03337e785..67789965e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -1,4 +1,5 @@ -using HwProj.Models.NotificationsService; +using HwProj.EventBus.Client; +using HwProj.Models.NotificationsService; using Microsoft.EntityFrameworkCore; namespace HwProj.NotificationsService.API.Models @@ -22,7 +23,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity().Ignore(work => work.Type); + modelBuilder.Entity().HasKey(work => new { work.TaskId, work.HomeworkId, + work.CourseId, work.CategoryId }); } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs index 354dddc14..7d05888f7 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs @@ -1,9 +1,12 @@ -using HwProj.Models.NotificationsService; -using HwProj.Repositories; +using System.Threading.Tasks; +using HwProj.Models.NotificationsService; + namespace HwProj.NotificationsService.API.Repositories; -public interface IScheduleWorksRepository : ICrudRepository +public interface IScheduleWorksRepository { - + Task AddAsync(ScheduleWork work); + + Task DeleteAsync((long? taskId, long? homeworkId, long? courseId, string categoryId) id); } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs index 0b525977a..bcf5a53c9 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs @@ -1,13 +1,36 @@ -using HwProj.Models.NotificationsService; +using System.Linq; +using System.Threading.Tasks; +using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Models; -using HwProj.Repositories; +using Microsoft.EntityFrameworkCore; +using Z.EntityFramework.Plus; namespace HwProj.NotificationsService.API.Repositories { - public class ScheduleWorksRepository : CrudRepository, IScheduleWorksRepository + public class ScheduleWorksRepository : IScheduleWorksRepository { - public ScheduleWorksRepository(NotificationsContext context) : base(context) + private readonly DbContext _context; + + public ScheduleWorksRepository(NotificationsContext context) + { + _context = context; + } + + public async Task AddAsync(ScheduleWork work) + { + await _context.AddAsync(work).ConfigureAwait(false); + await _context.SaveChangesAsync().ConfigureAwait(false); + } + + public async Task DeleteAsync((long? taskId, long? homeworkId, long? courseId, string categoryId) id) { + await _context.Set() + .Where(work => work.TaskId.Equals(id.taskId) && + work.HomeworkId.Equals(id.homeworkId) && + work.CourseId.Equals(id.courseId) && + work.CategoryId.Equals(id.categoryId)) + .DeleteAsync() + .ConfigureAwait(false); } } -} \ No newline at end of file +} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index 8c8b1f798..c41cbe710 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -14,6 +14,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using HwProj.CoursesService.API.Events; +using HwProj.EventBus.Client; using HwProj.SolutionsService.API.Events; using UpdateTaskMaxRatingEvent = HwProj.CoursesService.API.Events.UpdateTaskMaxRatingEvent; @@ -41,7 +42,6 @@ public void ConfigureServices(IServiceCollection services) // Add the processing server as IHostedService services.AddHangfireServer(); - services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); From 635920d0ed3c1e38fe5b968eb0d9df9dd72a7ef1 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Tue, 24 Oct 2023 01:29:10 +0300 Subject: [PATCH 07/22] feat: change ScheduleWork id to string --- .../HwProj.Models/HwProj.Models.csproj | 1 + .../NotificationsService/ScheduleWork.cs | 46 +++++++++---------- .../Events/LecturerAcceptToCourseEvent.cs | 0 .../Events/LecturerInvitedToCourseEvent.cs | 0 .../Events/LecturerRejectToCourseEvent.cs | 0 .../Events/NewCourseMateEvent.cs | 0 .../Events/NewHomeworkEvent.cs | 0 .../Events/NewHomeworkTaskEvent.cs | 0 .../Events/UpdateHomeworkEvent.cs | 0 .../Events/UpdateSolutionMaxRatingEvent.cs | 0 .../Events/UpdateTaskMaxRatingEvent.cs | 0 .../NewHomeworkTaskEventHandler.cs | 16 ++----- .../Models/NotificationsContext.cs | 6 --- .../Repositories/IScheduleWorksRepository.cs | 6 +-- .../Repositories/ScheduleWorksRepository.cs | 31 ++----------- 15 files changed, 34 insertions(+), 72 deletions(-) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/LecturerAcceptToCourseEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/LecturerInvitedToCourseEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/LecturerRejectToCourseEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/NewCourseMateEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/NewHomeworkEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/NewHomeworkTaskEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/UpdateHomeworkEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/UpdateSolutionMaxRatingEvent.cs (100%) rename HwProj.CoursesService/HwProj.CoursesService.API/{ => Controllers}/Events/UpdateTaskMaxRatingEvent.cs (100%) diff --git a/HwProj.Common/HwProj.Models/HwProj.Models.csproj b/HwProj.Common/HwProj.Models/HwProj.Models.csproj index 0ec3918d8..b2cb3dcce 100644 --- a/HwProj.Common/HwProj.Models/HwProj.Models.csproj +++ b/HwProj.Common/HwProj.Models/HwProj.Models.csproj @@ -14,6 +14,7 @@ + diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index d336b64d0..ac009b605 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -1,31 +1,31 @@ -namespace HwProj.Models.NotificationsService +using System.ComponentModel.DataAnnotations; +using HwProj.Repositories; + + +namespace HwProj.Models.NotificationsService { - public class ScheduleWork + public class ScheduleWork : IEntity { - private long _taskId; - private long _homeworkId; - private long _courseId; - - public long? TaskId - { - get => _taskId; - set => _taskId = value ?? -1; - } + [Key] public string Id { get; set; } + public string JobId { get; set; } + } - public long? HomeworkId + public static class ScheduleWorkIdBuilder + { + public static string Build(string eventName, long id) { - get => _homeworkId; - set => _homeworkId = value ?? -1; - } + //TODO: fill + var category = eventName switch + { + _ when eventName.Equals("NewHomeworkTaskEvent") || eventName.Equals("UpdateTaskMaxRatingEvent") || + eventName.Equals("UpdateSolutionMaxRatingEvent") + => "Task", + _ when eventName.Equals("NewHomeworkEvent") + => "Homework", + _ => "Unknown" + }; - public long? CourseId - { - get => _courseId; - set => _courseId = value ?? -1; + return $"{category}/{id}"; } - - public string CategoryId { get; set; } - - public string JobId { get; set; } } } \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerAcceptToCourseEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerAcceptToCourseEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerAcceptToCourseEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerAcceptToCourseEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerInvitedToCourseEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerInvitedToCourseEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerInvitedToCourseEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerInvitedToCourseEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerRejectToCourseEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerRejectToCourseEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/LecturerRejectToCourseEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerRejectToCourseEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewCourseMateEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewCourseMateEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/NewCourseMateEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewCourseMateEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkTaskEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/NewHomeworkTaskEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkTaskEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/UpdateHomeworkEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateHomeworkEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/UpdateHomeworkEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateHomeworkEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/UpdateSolutionMaxRatingEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateSolutionMaxRatingEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/UpdateSolutionMaxRatingEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateSolutionMaxRatingEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Events/UpdateTaskMaxRatingEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateTaskMaxRatingEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Events/UpdateTaskMaxRatingEvent.cs rename to HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateTaskMaxRatingEvent.cs diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index 141f2dd08..d799e346d 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -1,7 +1,5 @@ using System; using System.Linq; -using System.Net.Sockets; -using System.Threading; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; @@ -10,9 +8,7 @@ using HwProj.CoursesService.API.Events; using HwProj.Models; using Hangfire; -using HwProj.EventBus.Client; using HwProj.NotificationsService.API.Services; -using Microsoft.Azure.KeyVault.Models; using Microsoft.Extensions.Configuration; namespace HwProj.NotificationsService.API.EventHandlers @@ -41,26 +37,20 @@ public NewHomeworkTaskEventHandler( public override async Task HandleAsync(NewHomeworkTaskEvent @event) { - (long? taskId, long? homeworkId, long? courseId, string categoryId) id - = (@event.TaskId, null, @event.Course.Id, "Task"); - + var id = ScheduleWorkIdBuilder.Build(nameof(NewHomeworkTaskEvent), @event.TaskId); var jobId = BackgroundJob.Schedule(() => ScheduleWorkAsync(@event, id), @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); var scheduleWork = new ScheduleWork { - TaskId = id.taskId, - HomeworkId = id.homeworkId, - CourseId = id.courseId, - CategoryId = id.categoryId, + Id = id, JobId = jobId }; await _scheduleWorksRepository.AddAsync(scheduleWork); } - public async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event, - (long? taskId, long? homeworkId, long? courseId, string categoryId) id) + public async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event, string id) { var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index 67789965e..f06756506 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -20,11 +20,5 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().HasIndex(n => n.UserId); modelBuilder.Entity().HasKey(n => new { n.UserId, n.Category }); } - - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.Entity().HasKey(work => new { work.TaskId, work.HomeworkId, - work.CourseId, work.CategoryId }); - } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs index 7d05888f7..ccf54708e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs @@ -1,12 +1,10 @@ using System.Threading.Tasks; using HwProj.Models.NotificationsService; +using HwProj.Repositories; namespace HwProj.NotificationsService.API.Repositories; -public interface IScheduleWorksRepository +public interface IScheduleWorksRepository : ICrudRepository { - Task AddAsync(ScheduleWork work); - - Task DeleteAsync((long? taskId, long? homeworkId, long? courseId, string categoryId) id); } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs index bcf5a53c9..3d1584dcb 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs @@ -1,36 +1,15 @@ -using System.Linq; -using System.Threading.Tasks; + using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Models; +using HwProj.Repositories; using Microsoft.EntityFrameworkCore; -using Z.EntityFramework.Plus; namespace HwProj.NotificationsService.API.Repositories { - public class ScheduleWorksRepository : IScheduleWorksRepository + public class ScheduleWorksRepository : CrudRepository, IScheduleWorksRepository { - private readonly DbContext _context; - - public ScheduleWorksRepository(NotificationsContext context) - { - _context = context; - } - - public async Task AddAsync(ScheduleWork work) - { - await _context.AddAsync(work).ConfigureAwait(false); - await _context.SaveChangesAsync().ConfigureAwait(false); - } - - public async Task DeleteAsync((long? taskId, long? homeworkId, long? courseId, string categoryId) id) + public ScheduleWorksRepository(NotificationsContext context) : base(context) { - await _context.Set() - .Where(work => work.TaskId.Equals(id.taskId) && - work.HomeworkId.Equals(id.homeworkId) && - work.CourseId.Equals(id.courseId) && - work.CategoryId.Equals(id.categoryId)) - .DeleteAsync() - .ConfigureAwait(false); } } -} +} \ No newline at end of file From f3a7d6a21661dcb20bf71c8f15b7b721f84c182e Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Fri, 27 Oct 2023 01:42:41 +0300 Subject: [PATCH 08/22] feat: add and delete delayed task events and eventhandlers --- .../HwProj.Events/HwProj.Events.csproj | 13 +++++++++ .../Events/CourseEvents/DeleteTaskEvent.cs | 14 +++++++++ .../LecturerAcceptToCourseEvent.cs | 2 +- .../LecturerInvitedToCourseEvent.cs | 2 +- .../LecturerRejectToCourseEvent.cs | 2 +- .../CourseEvents}/NewCourseMateEvent.cs | 2 +- .../Events/CourseEvents}/NewHomeworkEvent.cs | 2 +- .../Events/CourseEvents/NewTaskEvent.cs | 6 ++-- .../CourseEvents}/UpdateHomeworkEvent.cs | 0 .../UpdateSolutionMaxRatingEvent.cs | 0 .../Events/CourseEvents/UpdateTaskEvent.cs | 25 ++++++++++++++++ .../HwProj.Models/HwProj.Models.csproj | 3 +- .../NotificationsService/ScheduleWork.cs | 13 +++++---- .../Events/UpdateTaskMaxRatingEvent.cs | 19 ------------ .../Services/CoursesService.cs | 1 + .../Services/HomeworksService.cs | 1 + .../Services/TasksService.cs | 12 ++++---- .../HwProj.EventBus.Client.csproj | 5 +--- .../EventHandlers/DeleteTaskEventHandler.cs | 29 +++++++++++++++++++ .../LecturerAcceptToCourseEventHandler.cs | 1 + .../LecturerInvitedToCourseEventHandler.cs | 1 + .../LecturerRejectToCourseEventHandler.cs | 1 + .../EventHandlers/NewCourseMateHandler.cs | 1 + .../EventHandlers/NewHomeworkEventHandler.cs | 1 + .../NewHomeworkTaskEventHandler.cs | 15 ++++++---- ...ntHandler.cs => UpdateTaskEventHandler.cs} | 27 ++++++++++++----- .../Repositories/IScheduleWorksRepository.cs | 1 + .../Repositories/ScheduleWorksRepository.cs | 8 ++++- .../Startup.cs | 10 +++---- 29 files changed, 155 insertions(+), 62 deletions(-) create mode 100644 HwProj.Common/HwProj.Events/HwProj.Events.csproj create mode 100644 HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/LecturerAcceptToCourseEvent.cs (86%) rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/LecturerInvitedToCourseEvent.cs (86%) rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/LecturerRejectToCourseEvent.cs (86%) rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/NewCourseMateEvent.cs (88%) rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/NewHomeworkEvent.cs (89%) rename HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkTaskEvent.cs => HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs (78%) rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/UpdateHomeworkEvent.cs (100%) rename {HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events => HwProj.Common/HwProj.Models/Events/CourseEvents}/UpdateSolutionMaxRatingEvent.cs (100%) create mode 100644 HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs delete mode 100644 HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateTaskMaxRatingEvent.cs create mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs rename HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/{UpdateTaskMaxRatingEventHandler.cs => UpdateTaskEventHandler.cs} (76%) diff --git a/HwProj.Common/HwProj.Events/HwProj.Events.csproj b/HwProj.Common/HwProj.Events/HwProj.Events.csproj new file mode 100644 index 000000000..3ae664db1 --- /dev/null +++ b/HwProj.Common/HwProj.Events/HwProj.Events.csproj @@ -0,0 +1,13 @@ + + + + netcoreapp2.2 + HwProj.Events2 + + + + + + + + diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs new file mode 100644 index 000000000..1c0acb6d0 --- /dev/null +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs @@ -0,0 +1,14 @@ +using HwProj.EventBus.Client; + +namespace HwProj.Events.CourseEvents +{ + public class DeleteTaskEvent: Event + { + public long TaskId { get; set; } + + public DeleteTaskEvent(long taskId) + { + TaskId = taskId; + } + } +} \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerAcceptToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs similarity index 86% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerAcceptToCourseEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs index 163af8672..d16028f78 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerAcceptToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Events.CourseEvents { public class LecturerAcceptToCourseEvent : Event { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerInvitedToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs similarity index 86% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerInvitedToCourseEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs index 31be36694..907b14226 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerInvitedToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Events.CourseEvents { public class LecturerInvitedToCourseEvent : Event { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerRejectToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs similarity index 86% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerRejectToCourseEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs index 9386a6cb1..2eeb273c3 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/LecturerRejectToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Events.CourseEvents { public class LecturerRejectToCourseEvent : Event { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewCourseMateEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs similarity index 88% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewCourseMateEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs index 747917731..9f2c8db56 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewCourseMateEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Events.CourseEvents { public class NewCourseMateEvent : Event { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs similarity index 89% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs index b78429c18..fd34790a4 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs @@ -1,7 +1,7 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Events.CourseEvents { public class NewHomeworkEvent : Event { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs similarity index 78% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkTaskEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs index d33b5dbe9..5d33acc03 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/NewHomeworkTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs @@ -2,9 +2,9 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Events.CourseEvents { - public class NewHomeworkTaskEvent : Event + public class NewTaskEvent : Event { public string TaskTitle { get; set; } @@ -17,7 +17,7 @@ public class NewHomeworkTaskEvent : Event public CourseDTO Course { get; set; } - public NewHomeworkTaskEvent(string taskTitle, long taskId, DateTime? deadline, + public NewTaskEvent(string taskTitle, long taskId, DateTime? deadline, DateTime publicationDate, CourseDTO course) { TaskTitle = taskTitle; diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateHomeworkEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateSolutionMaxRatingEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs similarity index 100% rename from HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateSolutionMaxRatingEvent.cs rename to HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs new file mode 100644 index 000000000..903f4a545 --- /dev/null +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs @@ -0,0 +1,25 @@ +using System; +using HwProj.EventBus.Client; +using HwProj.Models.CoursesService.ViewModels; + +namespace HwProj.Events.CourseEvents +{ + public class UpdateTaskEvent : Event + { + public CourseDTO Course { get; set; } + + public string TaskTitle { get; set; } + + public long TaskId { get; set; } + + public DateTime PublicationDate { get; set; } + + public UpdateTaskEvent(CourseDTO course, string taskTitle, long taskId, DateTime publicationDate) + { + Course = course; + TaskTitle = taskTitle; + TaskId = taskId; + PublicationDate = publicationDate; + } + } +} diff --git a/HwProj.Common/HwProj.Models/HwProj.Models.csproj b/HwProj.Common/HwProj.Models/HwProj.Models.csproj index b2cb3dcce..ecab40b8c 100644 --- a/HwProj.Common/HwProj.Models/HwProj.Models.csproj +++ b/HwProj.Common/HwProj.Models/HwProj.Models.csproj @@ -10,7 +10,7 @@ - + @@ -20,6 +20,7 @@ + diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index ac009b605..cf4153734 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -1,4 +1,6 @@ using System.ComponentModel.DataAnnotations; +using HwProj.EventBus.Client; +using HwProj.Events.CourseEvents; using HwProj.Repositories; @@ -12,15 +14,16 @@ public class ScheduleWork : IEntity public static class ScheduleWorkIdBuilder { - public static string Build(string eventName, long id) + public static string Build(Event @event, long id) { //TODO: fill - var category = eventName switch + var eventType = @event.GetType(); + var category = eventType switch { - _ when eventName.Equals("NewHomeworkTaskEvent") || eventName.Equals("UpdateTaskMaxRatingEvent") || - eventName.Equals("UpdateSolutionMaxRatingEvent") + _ when eventType == typeof(NewTaskEvent) || eventType == typeof(UpdateTaskEvent) || + eventType == typeof(DeleteTaskEvent) => "Task", - _ when eventName.Equals("NewHomeworkEvent") + _ when eventType == typeof(NewHomeworkEvent) => "Homework", _ => "Unknown" }; diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateTaskMaxRatingEvent.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateTaskMaxRatingEvent.cs deleted file mode 100644 index 4f20de847..000000000 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/Events/UpdateTaskMaxRatingEvent.cs +++ /dev/null @@ -1,19 +0,0 @@ -using HwProj.EventBus.Client; -using HwProj.Models.CoursesService.ViewModels; - -namespace HwProj.CoursesService.API.Events -{ - public class UpdateTaskMaxRatingEvent : Event - { - public CourseDTO Course { get; set; } - public HomeworkTaskViewModel Task { get; set; } - public int MaxRating { get; set; } - - public UpdateTaskMaxRatingEvent(CourseDTO course, HomeworkTaskViewModel task, int rating) - { - Course = course; - Task = task; - MaxRating = rating; - } - } -} diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs index e7034650b..093aa282b 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs @@ -12,6 +12,7 @@ using HwProj.Models.CoursesService.ViewModels; using HwProj.Models.Roles; using Microsoft.EntityFrameworkCore; +using HwProj.Events.CourseEvents; namespace HwProj.CoursesService.API.Services { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs index 2558e64fc..1f874effb 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs @@ -6,6 +6,7 @@ using HwProj.CoursesService.API.Events; using HwProj.Models; using HwProj.Models.CoursesService.ViewModels; +using HwProj.Events.CourseEvents; namespace HwProj.CoursesService.API.Services { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index faec7da03..cafe1d0a4 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -11,6 +11,7 @@ using HwProj.Models; using HwProj.Models.CoursesService.ViewModels; using static System.TimeSpan; +using HwProj.Events.CourseEvents; namespace HwProj.CoursesService.API.Services { @@ -50,26 +51,27 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var taskId = await _tasksRepository.AddAsync(task); - if (task.PublicationDate > DateTimeUtils.GetMoscowNow()) - _eventBus.Publish(new NewHomeworkTaskEvent(task.Title, taskId, task.DeadlineDate, task.PublicationDate, courseModel)); + _eventBus.Publish(new NewTaskEvent(task.Title, taskId, task.DeadlineDate, task.PublicationDate, + courseModel)); return taskId; } public async Task DeleteTaskAsync(long taskId) { - var task = await _tasksRepository.GetAsync(taskId); + _eventBus.Publish(new DeleteTaskEvent(taskId)); await _tasksRepository.DeleteAsync(taskId); } public async Task UpdateTaskAsync(long taskId, HomeworkTask update) { var task = await _tasksRepository.GetAsync(taskId); - var taskModel = _mapper.Map(task); var homework = await _homeworksRepository.GetAsync(task.HomeworkId); var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId); var courseModel = _mapper.Map(course); - _eventBus.Publish(new UpdateTaskMaxRatingEvent(courseModel, taskModel, update.MaxRating)); + + + _eventBus.Publish(new UpdateTaskEvent(courseModel, update.Title, taskId, update.PublicationDate)); await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask() { diff --git a/HwProj.EventBus/HwProj.EventBus.Client/HwProj.EventBus.Client.csproj b/HwProj.EventBus/HwProj.EventBus.Client/HwProj.EventBus.Client.csproj index 7932fcc9d..01fb17fd8 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/HwProj.EventBus.Client.csproj +++ b/HwProj.EventBus/HwProj.EventBus.Client/HwProj.EventBus.Client.csproj @@ -18,9 +18,6 @@ - - - - + diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs new file mode 100644 index 000000000..1c09398bb --- /dev/null +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; +using Hangfire; +using HwProj.EventBus.Client.Interfaces; +using HwProj.Events.CourseEvents; +using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Repositories; + +namespace HwProj.NotificationsService.API.EventHandlers +{ + public class DeleteTaskEventHandler : EventHandlerBase + { + private readonly IScheduleWorksRepository _scheduleWorksRepository; + + public DeleteTaskEventHandler(IScheduleWorksRepository scheduleWorksRepository) + { + _scheduleWorksRepository = scheduleWorksRepository; + } + + public override async Task HandleAsync(DeleteTaskEvent @event) + { + var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); + var scheduleWork = await _scheduleWorksRepository.GetAsync(id); + + BackgroundJob.Delete(scheduleWork.JobId); + + await _scheduleWorksRepository.DeleteAsync(id); + } + } +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs index 6949e1854..8e0da28c1 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs @@ -7,6 +7,7 @@ using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; +using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs index 22d0bb7a2..aa3754bfa 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs @@ -6,6 +6,7 @@ using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; +using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs index cd9b28ab9..ac4fe5f7b 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs @@ -7,6 +7,7 @@ using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; +using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs index e9e3b1a55..e573f95e8 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs @@ -7,6 +7,7 @@ using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; +using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs index 716374aca..c9c4f9dba 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs @@ -8,6 +8,7 @@ using HwProj.Models; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; +using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index d799e346d..7ee065b6d 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -8,12 +8,13 @@ using HwProj.CoursesService.API.Events; using HwProj.Models; using Hangfire; +using HwProj.Events.CourseEvents; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; namespace HwProj.NotificationsService.API.EventHandlers { - public class NewHomeworkTaskEventHandler : EventHandlerBase + public class NewHomeworkTaskEventHandler : EventHandlerBase { private readonly INotificationsRepository _notificationRepository; private readonly IScheduleWorksRepository _scheduleWorksRepository; @@ -35,11 +36,13 @@ public NewHomeworkTaskEventHandler( _configuration = configuration.GetSection("Notification"); } - public override async Task HandleAsync(NewHomeworkTaskEvent @event) + public override async Task HandleAsync(NewTaskEvent @event) { - var id = ScheduleWorkIdBuilder.Build(nameof(NewHomeworkTaskEvent), @event.TaskId); - var jobId = BackgroundJob.Schedule(() => ScheduleWorkAsync(@event, id), - @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); + var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); + var jobId = BackgroundJob.Schedule( () => ScheduleWorkAsync(@event, id), + @event.PublicationDate >= DateTimeUtils.GetMoscowNow() + ? @event.PublicationDate.Subtract(TimeSpan.FromHours(3)) + : DateTimeUtils.GetMoscowNow().Subtract(TimeSpan.FromHours(3))); var scheduleWork = new ScheduleWork { @@ -50,7 +53,7 @@ public override async Task HandleAsync(NewHomeworkTaskEvent @event) } - public async Task ScheduleWorkAsync(NewHomeworkTaskEvent @event, string id) + public async Task ScheduleWorkAsync(NewTaskEvent @event, string id) { var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskMaxRatingEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs similarity index 76% rename from HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskMaxRatingEventHandler.cs rename to HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index 1289a1333..26550441a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskMaxRatingEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -2,40 +2,46 @@ using AutoMapper; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models.NotificationsService; -using HwProj.NotificationsService.API.Repositories; -using HwProj.CoursesService.API.Events; +using HwProj.Events.CourseEvents; using HwProj.Models; using HwProj.Models.AuthService.DTO; +using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; namespace HwProj.NotificationsService.API.EventHandlers { - public class UpdateTaskMaxRatingEventHandler : EventHandlerBase + public class UpdateTaskEventHandler : EventHandlerBase { private readonly INotificationsRepository _notificationRepository; private readonly IAuthServiceClient _authServiceClient; private readonly IMapper _mapper; private readonly IConfigurationSection _configuration; private readonly IEmailService _emailService; + private readonly IScheduleWorksRepository _scheduleWorksRepository; - public UpdateTaskMaxRatingEventHandler( + public UpdateTaskEventHandler( INotificationsRepository notificationRepository, IMapper mapper, IAuthServiceClient authServiceClient, IConfiguration configuration, - IEmailService emailService) + IEmailService emailService, + IScheduleWorksRepository scheduleWorksRepository) { _notificationRepository = notificationRepository; _mapper = mapper; _authServiceClient = authServiceClient; _emailService = emailService; _configuration = configuration.GetSection("Notification"); + _scheduleWorksRepository = scheduleWorksRepository; } - public override async Task HandleAsync(UpdateTaskMaxRatingEvent @event) + public override async Task HandleAsync(UpdateTaskEvent @event) { + //TODO: event types + var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); + foreach (var student in @event.Course.CourseMates) { var studentAccount = await _authServiceClient.GetAccountData(student.StudentId); @@ -43,7 +49,7 @@ public override async Task HandleAsync(UpdateTaskMaxRatingEvent @event) var notification = new Notification { Sender = "CourseService", - Body = $"Задача {@event.Task.Title}" + + Body = $"Задача {@event.TaskTitle}" + $" из курса {@event.Course.Name} обновлена.", Category = CategoryState.Courses, Date = DateTimeUtils.GetMoscowNow(), @@ -57,5 +63,10 @@ public override async Task HandleAsync(UpdateTaskMaxRatingEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } } + + public async Task ScheduleWorkAsync() + { + + } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs index ccf54708e..ed8ee46e8 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs @@ -7,4 +7,5 @@ namespace HwProj.NotificationsService.API.Repositories; public interface IScheduleWorksRepository : ICrudRepository { + bool Contains(string id); } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs index 3d1584dcb..6f8e8360a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs @@ -1,4 +1,5 @@ - +using System.Linq; +using System.Threading.Tasks; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Models; using HwProj.Repositories; @@ -11,5 +12,10 @@ public class ScheduleWorksRepository : CrudRepository, ISc public ScheduleWorksRepository(NotificationsContext context) : base(context) { } + + public bool Contains(string id) + { + return Context.Set().AsNoTracking().Any(work => work.Id.Equals(id)); + } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index c41cbe710..7e1e02f0d 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -16,7 +16,7 @@ using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client; using HwProj.SolutionsService.API.Events; -using UpdateTaskMaxRatingEvent = HwProj.CoursesService.API.Events.UpdateTaskMaxRatingEvent; +using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API { @@ -51,12 +51,12 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient, RateEventHandler>(); services.AddTransient, StudentPassTaskEventHandler>(); services.AddTransient, UpdateHomeworkEventHandler>(); - services.AddTransient, UpdateTaskMaxRatingEventHandler>(); + services.AddTransient, UpdateTaskEventHandler>(); services.AddTransient, LecturerAcceptToCourseEventHandler>(); services.AddTransient, LecturerRejectToCourseEventHandler>(); services.AddTransient, LecturerInvitedToCourseEventHandler>(); services.AddTransient, NewHomeworkEventHandler>(); - services.AddTransient, NewHomeworkTaskEventHandler>(); + services.AddTransient, NewHomeworkTaskEventHandler>(); services.AddTransient, InviteLecturerEventHandler>(); services.AddTransient, NewCourseMateHandler>(); services.AddTransient, PasswordRecoveryEventHandler>(); @@ -77,12 +77,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); - eventBustSubscriber.Subscribe(); + eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); - eventBustSubscriber.Subscribe(); + eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); From ac1eaa02b270f83279232d6e78699939f841b6b7 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Fri, 27 Oct 2023 23:01:31 +0300 Subject: [PATCH 09/22] feat: add delete update task events and eventhandlers are written --- .../Events/CourseEvents/UpdateTaskEvent.cs | 9 +++- .../NotificationsService/ScheduleWork.cs | 2 +- .../Services/TasksService.cs | 4 +- .../EventHandlers/DeleteTaskEventHandler.cs | 1 - .../NewHomeworkTaskEventHandler.cs | 24 ++++++----- .../EventHandlers/UpdateTaskEventHandler.cs | 43 +++++++++++++++---- .../Repositories/IScheduleWorksRepository.cs | 1 - .../Repositories/ScheduleWorksRepository.cs | 4 -- .../Startup.cs | 14 +++--- 9 files changed, 65 insertions(+), 37 deletions(-) diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs index 903f4a545..3932b1133 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs @@ -12,14 +12,21 @@ public class UpdateTaskEvent : Event public long TaskId { get; set; } + //закладываюсь, что дату публикации не будут менять после публикации + public DateTime PreviousPublicationDate { get; set; } + public DateTime PublicationDate { get; set; } - public UpdateTaskEvent(CourseDTO course, string taskTitle, long taskId, DateTime publicationDate) + public DateTime? Deadline { get; set; } + + public UpdateTaskEvent(CourseDTO course, string taskTitle, long taskId, DateTime previousPublicationDate, DateTime publicationDate, DateTime? deadline) { Course = course; TaskTitle = taskTitle; TaskId = taskId; + PreviousPublicationDate = previousPublicationDate; PublicationDate = publicationDate; + Deadline = deadline; } } } diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index cf4153734..258eaba83 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -23,7 +23,7 @@ public static string Build(Event @event, long id) _ when eventType == typeof(NewTaskEvent) || eventType == typeof(UpdateTaskEvent) || eventType == typeof(DeleteTaskEvent) => "Task", - _ when eventType == typeof(NewHomeworkEvent) + _ when eventType == typeof(NewHomeworkEvent) => "Homework", _ => "Unknown" }; diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index cafe1d0a4..8a470110e 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -70,8 +70,8 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId); var courseModel = _mapper.Map(course); - - _eventBus.Publish(new UpdateTaskEvent(courseModel, update.Title, taskId, update.PublicationDate)); + _eventBus.Publish(new UpdateTaskEvent(courseModel, update.Title, taskId, task.PublicationDate, + update.PublicationDate, update.DeadlineDate)); await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask() { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs index 1c09398bb..c4eeff81b 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs @@ -22,7 +22,6 @@ public override async Task HandleAsync(DeleteTaskEvent @event) var scheduleWork = await _scheduleWorksRepository.GetAsync(id); BackgroundJob.Delete(scheduleWork.JobId); - await _scheduleWorksRepository.DeleteAsync(id); } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index 7ee065b6d..c419c4d03 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -1,14 +1,13 @@ using System; using System.Linq; using System.Threading.Tasks; +using Hangfire; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; +using HwProj.Events.CourseEvents; +using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; -using HwProj.CoursesService.API.Events; -using HwProj.Models; -using Hangfire; -using HwProj.Events.CourseEvents; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; @@ -39,10 +38,15 @@ public NewHomeworkTaskEventHandler( public override async Task HandleAsync(NewTaskEvent @event) { var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); - var jobId = BackgroundJob.Schedule( () => ScheduleWorkAsync(@event, id), - @event.PublicationDate >= DateTimeUtils.GetMoscowNow() - ? @event.PublicationDate.Subtract(TimeSpan.FromHours(3)) - : DateTimeUtils.GetMoscowNow().Subtract(TimeSpan.FromHours(3))); + + if (@event.PublicationDate <= DateTimeUtils.GetMoscowNow()) + { + await AddNotificationsAsync(@event); + return; + } + + var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event), + @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); var scheduleWork = new ScheduleWork { @@ -53,7 +57,7 @@ public override async Task HandleAsync(NewTaskEvent @event) } - public async Task ScheduleWorkAsync(NewTaskEvent @event, string id) + public async Task AddNotificationsAsync(NewTaskEvent @event) { var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); @@ -79,8 +83,6 @@ public async Task ScheduleWorkAsync(NewTaskEvent @event, string id) await Task.WhenAll(addNotificationTask, sendEmailTask); } - - await _scheduleWorksRepository.DeleteAsync(id); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index 26550441a..2e2c6f999 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -1,5 +1,7 @@ +using System; using System.Threading.Tasks; using AutoMapper; +using Hangfire; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Events.CourseEvents; @@ -39,8 +41,37 @@ public UpdateTaskEventHandler( public override async Task HandleAsync(UpdateTaskEvent @event) { - //TODO: event types var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); + var isTaskPublished = @event.PreviousPublicationDate <= DateTimeUtils.GetMoscowNow(); + if (isTaskPublished) + { + await AddNotificationsAsync(@event); + return; + } + + var previousWork = await _scheduleWorksRepository.GetAsync(id); + BackgroundJob.Delete(previousWork.JobId); + + var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event), + @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); + + await _scheduleWorksRepository.UpdateAsync(id, work => new ScheduleWork() + { + Id = id, + JobId = jobId + }); + } + + public async Task AddNotificationsAsync(UpdateTaskEvent @event) + { + var url = _configuration["Url"]; + var isTaskPublished = @event.PreviousPublicationDate < DateTimeUtils.GetMoscowNow(); + var message = isTaskPublished + ? $"Задача {@event.TaskTitle}" + + $" из курса {@event.Course.Name} обновлена." + : $"В курсе {@event.Course.Name}" + + $" опубликована новая задача {@event.TaskTitle}." + + (@event.Deadline is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); foreach (var student in @event.Course.CourseMates) { @@ -49,8 +80,7 @@ public override async Task HandleAsync(UpdateTaskEvent @event) var notification = new Notification { Sender = "CourseService", - Body = $"Задача {@event.TaskTitle}" + - $" из курса {@event.Course.Name} обновлена.", + Body = message, Category = CategoryState.Courses, Date = DateTimeUtils.GetMoscowNow(), HasSeen = false, @@ -63,10 +93,5 @@ public override async Task HandleAsync(UpdateTaskEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } } - - public async Task ScheduleWorkAsync() - { - - } } -} +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs index ed8ee46e8..ccf54708e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs @@ -7,5 +7,4 @@ namespace HwProj.NotificationsService.API.Repositories; public interface IScheduleWorksRepository : ICrudRepository { - bool Contains(string id); } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs index 6f8e8360a..22ebab9a8 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs @@ -13,9 +13,5 @@ public ScheduleWorksRepository(NotificationsContext context) : base(context) { } - public bool Contains(string id) - { - return Context.Set().AsNoTracking().Any(work => work.Id.Equals(id)); - } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index 7e1e02f0d..7fb0c2515 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -1,22 +1,20 @@ -using HwProj.AuthService.API.Events; +using Hangfire; +using HwProj.AuthService.API.Events; using HwProj.AuthService.Client; +using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client.Interfaces; +using HwProj.Events.CourseEvents; using HwProj.NotificationsService.API.EventHandlers; using HwProj.NotificationsService.API.Models; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; +using HwProj.SolutionsService.API.Events; using HwProj.Utils.Configuration; -using Hangfire; -using Hangfire.SqlServer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using HwProj.CoursesService.API.Events; -using HwProj.EventBus.Client; -using HwProj.SolutionsService.API.Events; -using HwProj.Events.CourseEvents; namespace HwProj.NotificationsService.API { @@ -52,6 +50,7 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient, StudentPassTaskEventHandler>(); services.AddTransient, UpdateHomeworkEventHandler>(); services.AddTransient, UpdateTaskEventHandler>(); + services.AddTransient, DeleteTaskEventHandler>(); services.AddTransient, LecturerAcceptToCourseEventHandler>(); services.AddTransient, LecturerRejectToCourseEventHandler>(); services.AddTransient, LecturerInvitedToCourseEventHandler>(); @@ -78,6 +77,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); + eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); From 53e261607f851899de1c58c3703d18c345bad30c Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Wed, 15 Nov 2023 21:55:24 +0300 Subject: [PATCH 10/22] fix: style fixes --- .../Events/CourseEvents/DeleteTaskEvent.cs | 2 +- .../CourseEvents/LecturerRejectToCourseEvent.cs | 4 ++-- .../Events/CourseEvents/NewCourseMateEvent.cs | 2 +- .../Events/CourseEvents/NewHomeworkEvent.cs | 4 ++-- .../Events/CourseEvents/NewTaskEvent.cs | 10 +++++----- .../NotificationsService/ScheduleWork.cs | 2 +- HwProj.Common/HwProj.Repositories/IEntity.cs | 4 ++-- .../HwProj.CoursesService.API/Startup.cs | 2 -- .../EventHandlers/NewHomeworkTaskEventHandler.cs | 2 +- .../EventHandlers/UpdateTaskEventHandler.cs | 2 +- .../Models/NotificationsContext.cs | 2 +- .../Repositories/IScheduleWorksRepository.cs | 10 ---------- .../Repositories/ScheduleWorksRepository.cs | 13 +++++++------ 13 files changed, 24 insertions(+), 35 deletions(-) delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs index 1c0acb6d0..231fa0a11 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs @@ -2,7 +2,7 @@ namespace HwProj.Events.CourseEvents { - public class DeleteTaskEvent: Event + public class DeleteTaskEvent : Event { public long TaskId { get; set; } diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs index 2eeb273c3..afbaf6853 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Events.CourseEvents { public class LecturerRejectToCourseEvent : Event { @@ -9,4 +9,4 @@ public class LecturerRejectToCourseEvent : Event public string MentorIds { get; set; } public string StudentId { get; set; } } -} +} \ No newline at end of file diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs index 9f2c8db56..ba87155f7 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Events.CourseEvents { public class NewCourseMateEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs index fd34790a4..3e8432ccb 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs @@ -1,7 +1,7 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.Events.CourseEvents +namespace HwProj.Events.CourseEvents { public class NewHomeworkEvent : Event { @@ -14,4 +14,4 @@ public NewHomeworkEvent(string homework, CourseDTO course) Course = course; } } -} +} \ No newline at end of file diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs index 5d33acc03..f0f76b6bd 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs @@ -7,15 +7,15 @@ namespace HwProj.Events.CourseEvents public class NewTaskEvent : Event { public string TaskTitle { get; set; } - + public long TaskId { get; set; } - + public DateTime? Deadline { get; set; } - + public DateTime PublicationDate { get; set; } - + public CourseDTO Course { get; set; } - + public NewTaskEvent(string taskTitle, long taskId, DateTime? deadline, DateTime publicationDate, CourseDTO course) diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index 258eaba83..7072e2e1d 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -6,7 +6,7 @@ namespace HwProj.Models.NotificationsService { - public class ScheduleWork : IEntity + public class ScheduleJob : IEntity { [Key] public string Id { get; set; } public string JobId { get; set; } diff --git a/HwProj.Common/HwProj.Repositories/IEntity.cs b/HwProj.Common/HwProj.Repositories/IEntity.cs index 4938858af..1581df22a 100644 --- a/HwProj.Common/HwProj.Repositories/IEntity.cs +++ b/HwProj.Common/HwProj.Repositories/IEntity.cs @@ -2,9 +2,9 @@ namespace HwProj.Repositories { - public interface IEntity + public interface IEntity where TKey : IEquatable { TKey Id { get; set; } } -} +} \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs index e5afb9271..a73057041 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Startup.cs @@ -53,8 +53,6 @@ public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IHostingEnvironment env, CourseContext context) { app.ConfigureHwProj(env, "Courses API", context); - app.UseHangfireDashboard(); - BackgroundJob.Enqueue(() => Console.WriteLine("Hello world !")); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index c419c4d03..ace1bf29c 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -48,7 +48,7 @@ public override async Task HandleAsync(NewTaskEvent @event) var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event), @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); - var scheduleWork = new ScheduleWork + var scheduleWork = new ScheduleJob { Id = id, JobId = jobId diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index 2e2c6f999..70a0ca3a0 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -55,7 +55,7 @@ public override async Task HandleAsync(UpdateTaskEvent @event) var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event), @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); - await _scheduleWorksRepository.UpdateAsync(id, work => new ScheduleWork() + await _scheduleWorksRepository.UpdateAsync(id, work => new ScheduleJob() { Id = id, JobId = jobId diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index f06756506..16ab15a72 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -8,7 +8,7 @@ public sealed class NotificationsContext : DbContext { public DbSet Notifications { get; set; } public DbSet Settings { get; set; } - public DbSet ScheduleWorks { get; set; } + public DbSet ScheduleJobs { get; set; } public NotificationsContext(DbContextOptions options) : base(options) diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs deleted file mode 100644 index ccf54708e..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System.Threading.Tasks; -using HwProj.Models.NotificationsService; -using HwProj.Repositories; - - -namespace HwProj.NotificationsService.API.Repositories; - -public interface IScheduleWorksRepository : ICrudRepository -{ -} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs index 22ebab9a8..4b1a9c155 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs @@ -1,17 +1,18 @@ -using System.Linq; -using System.Threading.Tasks; -using HwProj.Models.NotificationsService; +using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Models; using HwProj.Repositories; -using Microsoft.EntityFrameworkCore; namespace HwProj.NotificationsService.API.Repositories { - public class ScheduleWorksRepository : CrudRepository, IScheduleWorksRepository + public interface IScheduleWorksRepository : ICrudRepository + { + } + + + public class ScheduleWorksRepository : CrudRepository, IScheduleWorksRepository { public ScheduleWorksRepository(NotificationsContext context) : base(context) { } - } } \ No newline at end of file From 2cb1af126ac67e98cbf4c82c0eddfbf46c0291bc Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Mon, 20 Nov 2023 02:11:27 +0300 Subject: [PATCH 11/22] feat: bear main schedule event handler logic --- .../ViewModels/HomeworkTaskViewModels.cs | 14 ++++- .../Events/CourseEvents/DeleteTaskEvent.cs | 2 +- .../LecturerAcceptToCourseEvent.cs | 2 +- .../LecturerInvitedToCourseEvent.cs | 2 +- .../LecturerRejectToCourseEvent.cs | 2 +- .../Events/CourseEvents/NewCourseMateEvent.cs | 2 +- .../Events/CourseEvents/NewHomeworkEvent.cs | 2 +- .../Events/CourseEvents/NewTaskEvent.cs | 17 ++---- .../CourseEvents/UpdateHomeworkEvent.cs | 2 +- .../UpdateSolutionMaxRatingEvent.cs | 2 +- .../Events/CourseEvents/UpdateTaskEvent.cs | 24 +++----- .../NotificationsService/ScheduleWork.cs | 57 ++++++++++++++--- .../ApplicationProfile.cs | 1 + .../Services/CoursesService.cs | 3 +- .../Services/HomeworksService.cs | 3 +- .../Services/TasksService.cs | 21 +++---- .../EventHandlers/DeleteTaskEventHandler.cs | 17 +++--- .../EventHandlers/EventHandlerExtensions.cs | 61 +++++++++++++++++++ .../LecturerAcceptToCourseEventHandler.cs | 3 +- .../LecturerInvitedToCourseEventHandler.cs | 3 +- .../LecturerRejectToCourseEventHandler.cs | 3 +- .../EventHandlers/NewCourseMateHandler.cs | 3 +- .../EventHandlers/NewHomeworkEventHandler.cs | 3 +- .../NewHomeworkTaskEventHandler.cs | 32 +++++----- .../UpdateHomeworkEventHandler.cs | 2 +- .../EventHandlers/UpdateTaskEventHandler.cs | 39 +++++------- .../Repositories/ScheduleJobsRepository.cs | 49 +++++++++++++++ .../Repositories/ScheduleWorksRepository.cs | 18 ------ .../Startup.cs | 5 +- 29 files changed, 249 insertions(+), 145 deletions(-) create mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs create mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs diff --git a/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs b/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs index 3535f5b10..1f6967b5b 100644 --- a/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs +++ b/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs @@ -29,6 +29,18 @@ public class HomeworkTaskViewModel public bool IsDeferred { get; set; } } + + public class HomeworkTaskDTO + { + public long Id { get; set; } + + public string Title { get; set; } + + public DateTime PublicationDate { get; set; } + + public DateTime? DeadlineDate { get; set; } + } + public class CreateTaskViewModel { [Required] @@ -57,4 +69,4 @@ public void InitializeDeadline() } } } -} +} \ No newline at end of file diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs index 231fa0a11..8272dedc7 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class DeleteTaskEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs index d16028f78..9277351d4 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class LecturerAcceptToCourseEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs index 907b14226..65e8be08c 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class LecturerInvitedToCourseEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs index afbaf6853..4b7355a8a 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class LecturerRejectToCourseEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs index ba87155f7..7ec5bd627 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class NewCourseMateEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs index 3e8432ccb..9b8e79656 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs @@ -1,7 +1,7 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class NewHomeworkEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs index f0f76b6bd..d6e99b4df 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs @@ -2,28 +2,21 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class NewTaskEvent : Event { - public string TaskTitle { get; set; } - public long TaskId { get; set; } - public DateTime? Deadline { get; set; } - - public DateTime PublicationDate { get; set; } - + public HomeworkTaskDTO Task { get; set; } + public CourseDTO Course { get; set; } - public NewTaskEvent(string taskTitle, long taskId, DateTime? deadline, - DateTime publicationDate, CourseDTO course) + public NewTaskEvent(long taskId, HomeworkTaskDTO task, CourseDTO course) { - TaskTitle = taskTitle; TaskId = taskId; - Deadline = deadline; - PublicationDate = publicationDate; + Task = task; Course = course; } } diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs index 9045f256d..f05a180aa 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs @@ -1,7 +1,7 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Models.Events.CourseEvents { public class UpdateHomeworkEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs index 59f77ce4c..5ebaa2eed 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.CoursesService.API.Events +namespace HwProj.Models.Events.CourseEvents { public class UpdateSolutionMaxRatingEvent : Event { diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs index 3932b1133..8bfa55f32 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs @@ -2,31 +2,25 @@ using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; -namespace HwProj.Events.CourseEvents +namespace HwProj.Models.Events.CourseEvents { public class UpdateTaskEvent : Event { - public CourseDTO Course { get; set; } - - public string TaskTitle { get; set; } - public long TaskId { get; set; } - //закладываюсь, что дату публикации не будут менять после публикации - public DateTime PreviousPublicationDate { get; set; } + public HomeworkTaskDTO PreviousEvent { get; set; } - public DateTime PublicationDate { get; set; } + public HomeworkTaskDTO NewEvent { get; set; } - public DateTime? Deadline { get; set; } + public CourseDTO Course { get; set; } - public UpdateTaskEvent(CourseDTO course, string taskTitle, long taskId, DateTime previousPublicationDate, DateTime publicationDate, DateTime? deadline) + + public UpdateTaskEvent(long taskId, HomeworkTaskDTO previousEvent, HomeworkTaskDTO newEvent, CourseDTO course) { - Course = course; - TaskTitle = taskTitle; TaskId = taskId; - PreviousPublicationDate = previousPublicationDate; - PublicationDate = publicationDate; - Deadline = deadline; + PreviousEvent = previousEvent; + NewEvent = newEvent; + Course = course; } } } diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index 7072e2e1d..92edd3a54 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -1,24 +1,60 @@ -using System.ComponentModel.DataAnnotations; +using System; +using System.ComponentModel.DataAnnotations; using HwProj.EventBus.Client; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; using HwProj.Repositories; - namespace HwProj.Models.NotificationsService { public class ScheduleJob : IEntity { [Key] public string Id { get; set; } public string JobId { get; set; } + + public ScheduleJob(ScheduleWorkId id, string jobId) + { + Id = id.ToString(); + JobId = jobId; + } + + public ScheduleJob() + { + } } - public static class ScheduleWorkIdBuilder + public class ScheduleWorkId { - public static string Build(Event @event, long id) + public string Category { get; set; } + + public Type? EventType { get; set; } + + public long CategoryId { get; set; } + + public ScheduleWorkId(string category, Type? eventType, long categoryId) + { + Category = category; + EventType = eventType; + CategoryId = categoryId; + } + + + public override string ToString() + { + return $"{Category}/{EventType}/{CategoryId}"; + } + } + + public static class ScheduleJobIdHelper + { + public static ScheduleWorkId BuildId(Event @event, long categoryId) + { + return new ScheduleWorkId(GetCategory(@event), @event.GetType(), categoryId); + } + + public static string GetCategory(Event @event) { - //TODO: fill var eventType = @event.GetType(); - var category = eventType switch + return @event.GetType() switch { _ when eventType == typeof(NewTaskEvent) || eventType == typeof(UpdateTaskEvent) || eventType == typeof(DeleteTaskEvent) @@ -27,8 +63,13 @@ public static string Build(Event @event, long id) => "Homework", _ => "Unknown" }; + } + + public static ScheduleWorkId ParseId(string id) + { + var parts = id.Split('/'); - return $"{category}/{id}"; + return new ScheduleWorkId(parts[0], Type.GetType(parts[1]), int.Parse(parts[2])); } } } \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/ApplicationProfile.cs b/HwProj.CoursesService/HwProj.CoursesService.API/ApplicationProfile.cs index ab3954c03..55247cb2c 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/ApplicationProfile.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/ApplicationProfile.cs @@ -28,6 +28,7 @@ public ApplicationProfile() CreateMap() .ForMember("IsDeferred", cm => cm.MapFrom(g => DateTimeUtils.GetMoscowNow() < g.PublicationDate)); CreateMap().ReverseMap(); + CreateMap(); } } } diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs index 093aa282b..cd91cf1fc 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using AutoMapper; using HwProj.AuthService.Client; -using HwProj.CoursesService.API.Events; using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; using HwProj.CoursesService.API.Repositories.Groups; @@ -12,7 +11,7 @@ using HwProj.Models.CoursesService.ViewModels; using HwProj.Models.Roles; using Microsoft.EntityFrameworkCore; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.CoursesService.API.Services { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs index 1f874effb..242b47744 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/HomeworksService.cs @@ -3,10 +3,9 @@ using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; using HwProj.EventBus.Client.Interfaces; -using HwProj.CoursesService.API.Events; using HwProj.Models; using HwProj.Models.CoursesService.ViewModels; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.CoursesService.API.Services { diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index 8a470110e..83f5c86d3 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -1,17 +1,10 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; +using System.Threading.Tasks; using AutoMapper; -using HwProj.CoursesService.API.Events; using HwProj.CoursesService.API.Models; using HwProj.CoursesService.API.Repositories; -using HwProj.EventBus.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.CoursesService.ViewModels; -using static System.TimeSpan; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.CoursesService.API.Services { @@ -48,11 +41,10 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var homework = await _homeworksRepository.GetAsync(task.HomeworkId); var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId); var courseModel = _mapper.Map(course); - + var taskModel = _mapper.Map(task); var taskId = await _tasksRepository.AddAsync(task); - _eventBus.Publish(new NewTaskEvent(task.Title, taskId, task.DeadlineDate, task.PublicationDate, - courseModel)); + _eventBus.Publish(new NewTaskEvent(taskId, taskModel, courseModel)); return taskId; } @@ -69,9 +61,10 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) var homework = await _homeworksRepository.GetAsync(task.HomeworkId); var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId); var courseModel = _mapper.Map(course); + var previousTaskModel = _mapper.Map(task); + var newTaskModel = _mapper.Map(update); - _eventBus.Publish(new UpdateTaskEvent(courseModel, update.Title, taskId, task.PublicationDate, - update.PublicationDate, update.DeadlineDate)); + _eventBus.Publish(new UpdateTaskEvent(taskId, previousTaskModel, newTaskModel, courseModel)); await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask() { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs index c4eeff81b..57ce5ff3a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs @@ -1,28 +1,25 @@ using System.Threading.Tasks; using Hangfire; using HwProj.EventBus.Client.Interfaces; -using HwProj.Events.CourseEvents; -using HwProj.Models.NotificationsService; +using HwProj.Models.Events.CourseEvents; using HwProj.NotificationsService.API.Repositories; +using Swashbuckle.AspNetCore.SwaggerGen; namespace HwProj.NotificationsService.API.EventHandlers { public class DeleteTaskEventHandler : EventHandlerBase { - private readonly IScheduleWorksRepository _scheduleWorksRepository; + private readonly IScheduleJobsRepository _scheduleJobsRepository; - public DeleteTaskEventHandler(IScheduleWorksRepository scheduleWorksRepository) + public DeleteTaskEventHandler(IScheduleJobsRepository scheduleJobsRepository) { - _scheduleWorksRepository = scheduleWorksRepository; + _scheduleJobsRepository = scheduleJobsRepository; } public override async Task HandleAsync(DeleteTaskEvent @event) { - var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); - var scheduleWork = await _scheduleWorksRepository.GetAsync(id); - - BackgroundJob.Delete(scheduleWork.JobId); - await _scheduleWorksRepository.DeleteAsync(id); + await EventHandlerExtensions.DeleteScheduleJobsAsync(@event, @event.TaskId, + _scheduleJobsRepository); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs new file mode 100644 index 000000000..52a616195 --- /dev/null +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs @@ -0,0 +1,61 @@ +using System; +using System.Linq.Expressions; +using System.Threading.Tasks; +using Hangfire; +using HwProj.EventBus.Client; +using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Repositories; + +namespace HwProj.NotificationsService.API.EventHandlers +{ + public static class EventHandlerExtensions where TEvent : Event + { + public static async Task AddScheduleJobAsync(TEvent @event, long categoryId, DateTime publicationDate, + Expression> jobFunc, IScheduleJobsRepository jobsRepository) + { + var id = ScheduleJobIdHelper.BuildId(@event, categoryId); + var jobId = BackgroundJob.Schedule(jobFunc, publicationDate.Subtract(TimeSpan.FromHours(3))); + var scheduleJob = new ScheduleJob(id, jobId); + + await jobsRepository.AddAsync(scheduleJob); + } + + + public static async Task DeleteScheduleJobsAsync(TEvent @event, long categoryId, IScheduleJobsRepository jobsRepository) + { + var category = ScheduleJobIdHelper.GetCategory(@event); + var scheduleJobs = jobsRepository.GetAllByCategoryAsync(category, categoryId); + foreach (var scheduleJob in scheduleJobs) + { + BackgroundJob.Delete(scheduleJob.JobId); + } + + await jobsRepository.DeleteAllByCategoryAsync(category, categoryId); + } + + + public static async Task UpdateScheduleJobAsync(TEvent @event, long categoryId, DateTime publicationDate, + Expression> jobFunc, IScheduleJobsRepository jobsRepository) + { + await DeleteScheduleJobsAsync(@event, categoryId, jobsRepository); + + var id = ScheduleJobIdHelper.BuildId(@event, categoryId); + var jobId = BackgroundJob.Schedule(jobFunc, publicationDate.Subtract(TimeSpan.FromHours(3))); + var scheduleJob = new ScheduleJob(id, jobId); + + await jobsRepository.AddAsync(scheduleJob); + } + + + public static async Task DeleteScheduleJobAsync(TEvent @event, long categoryId, IScheduleJobsRepository jobsRepository) + { + var id = ScheduleJobIdHelper.BuildId(@event, categoryId); + var scheduleJob = await jobsRepository.GetAsync(id.ToString()); + + BackgroundJob.Delete(scheduleJob.JobId); + + await jobsRepository.DeleteAsync(id.ToString()); + } + + } +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs index 8e0da28c1..bd78f3e39 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs @@ -1,13 +1,12 @@ using System.Threading.Tasks; using HwProj.AuthService.Client; -using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs index aa3754bfa..d8ead54a8 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs @@ -1,12 +1,11 @@ using System.Threading.Tasks; -using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs index ac4fe5f7b..ba47a8143 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs @@ -1,13 +1,12 @@ using System.Threading.Tasks; using HwProj.AuthService.Client; -using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs index e573f95e8..8580bdbc6 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs @@ -1,13 +1,12 @@ using System.Threading.Tasks; using HwProj.AuthService.Client; -using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs index c9c4f9dba..cec6508f3 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs @@ -4,11 +4,10 @@ using HwProj.EventBus.Client.Interfaces; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; -using HwProj.CoursesService.API.Events; using HwProj.Models; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index ace1bf29c..826576a53 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -3,9 +3,10 @@ using System.Threading.Tasks; using Hangfire; using HwProj.AuthService.Client; +using HwProj.EventBus.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Events.CourseEvents; using HwProj.Models; +using HwProj.Models.Events.CourseEvents; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -16,20 +17,20 @@ namespace HwProj.NotificationsService.API.EventHandlers public class NewHomeworkTaskEventHandler : EventHandlerBase { private readonly INotificationsRepository _notificationRepository; - private readonly IScheduleWorksRepository _scheduleWorksRepository; + private readonly IScheduleJobsRepository _scheduleJobsRepository; private readonly IAuthServiceClient _authServiceClient; private readonly IConfigurationSection _configuration; private readonly IEmailService _emailService; public NewHomeworkTaskEventHandler( INotificationsRepository notificationRepository, - IScheduleWorksRepository scheduleWorksRepository, + IScheduleJobsRepository scheduleJobsRepository, IAuthServiceClient authServiceClient, IConfiguration configuration, IEmailService emailService) { _notificationRepository = notificationRepository; - _scheduleWorksRepository = scheduleWorksRepository; + _scheduleJobsRepository = scheduleJobsRepository; _authServiceClient = authServiceClient; _emailService = emailService; _configuration = configuration.GetSection("Notification"); @@ -37,23 +38,15 @@ public NewHomeworkTaskEventHandler( public override async Task HandleAsync(NewTaskEvent @event) { - var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); - - if (@event.PublicationDate <= DateTimeUtils.GetMoscowNow()) + if (@event.Task.PublicationDate <= DateTimeUtils.GetMoscowNow()) { await AddNotificationsAsync(@event); return; } - var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event), - @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); - - var scheduleWork = new ScheduleJob - { - Id = id, - JobId = jobId - }; - await _scheduleWorksRepository.AddAsync(scheduleWork); + await EventHandlerExtensions.AddScheduleJobAsync(@event, @event.TaskId, + @event.Task.PublicationDate, + () => AddNotificationsAsync(@event), _scheduleJobsRepository); } @@ -70,8 +63,8 @@ public async Task AddNotificationsAsync(NewTaskEvent @event) Sender = "CourseService", Body = $"В курсе {@event.Course.Name}" + - $" опубликована новая задача {@event.TaskTitle}." + - (@event.Deadline is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""), + $" опубликована новая задача {@event.Task.Title}." + + (@event.Task.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""), Category = CategoryState.Homeworks, Date = DateTimeUtils.GetMoscowNow(), @@ -83,6 +76,9 @@ public async Task AddNotificationsAsync(NewTaskEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } + + await EventHandlerExtensions.DeleteScheduleJobAsync(@event, @event.TaskId, + _scheduleJobsRepository); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs index d2bbc4de3..f62cb8ddc 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs @@ -3,8 +3,8 @@ using HwProj.EventBus.Client.Interfaces; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; -using HwProj.CoursesService.API.Events; using HwProj.Models; +using HwProj.Models.Events.CourseEvents; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index 70a0ca3a0..1c1420745 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -4,9 +4,9 @@ using Hangfire; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Events.CourseEvents; using HwProj.Models; using HwProj.Models.AuthService.DTO; +using HwProj.Models.Events.CourseEvents; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -21,7 +21,7 @@ public class UpdateTaskEventHandler : EventHandlerBase private readonly IMapper _mapper; private readonly IConfigurationSection _configuration; private readonly IEmailService _emailService; - private readonly IScheduleWorksRepository _scheduleWorksRepository; + private readonly IScheduleJobsRepository _scheduleJobsRepository; public UpdateTaskEventHandler( INotificationsRepository notificationRepository, @@ -29,50 +29,40 @@ public UpdateTaskEventHandler( IAuthServiceClient authServiceClient, IConfiguration configuration, IEmailService emailService, - IScheduleWorksRepository scheduleWorksRepository) + IScheduleJobsRepository scheduleJobsRepository) { _notificationRepository = notificationRepository; _mapper = mapper; _authServiceClient = authServiceClient; _emailService = emailService; _configuration = configuration.GetSection("Notification"); - _scheduleWorksRepository = scheduleWorksRepository; + _scheduleJobsRepository = scheduleJobsRepository; } public override async Task HandleAsync(UpdateTaskEvent @event) { - var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId); - var isTaskPublished = @event.PreviousPublicationDate <= DateTimeUtils.GetMoscowNow(); - if (isTaskPublished) + if (@event.PreviousEvent.PublicationDate <= DateTimeUtils.GetMoscowNow()) { await AddNotificationsAsync(@event); return; } - var previousWork = await _scheduleWorksRepository.GetAsync(id); - BackgroundJob.Delete(previousWork.JobId); - - var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event), - @event.PublicationDate.Subtract(TimeSpan.FromHours(3))); - - await _scheduleWorksRepository.UpdateAsync(id, work => new ScheduleJob() - { - Id = id, - JobId = jobId - }); + await EventHandlerExtensions.UpdateScheduleJobAsync(@event, @event.TaskId, + @event.NewEvent.PublicationDate, + () => AddNotificationsAsync(@event), _scheduleJobsRepository); } public async Task AddNotificationsAsync(UpdateTaskEvent @event) { var url = _configuration["Url"]; - var isTaskPublished = @event.PreviousPublicationDate < DateTimeUtils.GetMoscowNow(); + var isTaskPublished = @event.PreviousEvent.PublicationDate < DateTimeUtils.GetMoscowNow(); var message = isTaskPublished - ? $"Задача {@event.TaskTitle}" + + ? $"Задача {@event.PreviousEvent.Title}" + $" из курса {@event.Course.Name} обновлена." : $"В курсе {@event.Course.Name}" + - $" опубликована новая задача {@event.TaskTitle}." + - (@event.Deadline is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); - + $" опубликована новая задача {@event.NewEvent.Title}." + + (@event.NewEvent.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); + foreach (var student in @event.Course.CourseMates) { var studentAccount = await _authServiceClient.GetAccountData(student.StudentId); @@ -92,6 +82,9 @@ public async Task AddNotificationsAsync(UpdateTaskEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } + + await EventHandlerExtensions.DeleteScheduleJobAsync(@event, @event.TaskId, + _scheduleJobsRepository); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs new file mode 100644 index 000000000..2ee5bf76b --- /dev/null +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs @@ -0,0 +1,49 @@ +using System.Linq; +using System.Threading.Tasks; +using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Models; +using HwProj.Repositories; +using Z.EntityFramework.Plus; + +namespace HwProj.NotificationsService.API.Repositories +{ + public interface IScheduleJobsRepository : ICrudRepository + { + public Task DeleteAllByCategoryAsync(string category, long categoryId); + + public IQueryable GetAllByCategoryAsync(string category, long categoryId); + } + + + public class ScheduleJobsRepository : CrudRepository, IScheduleJobsRepository + { + public ScheduleJobsRepository(NotificationsContext context) : base(context) + { + } + + public async Task DeleteAllByCategoryAsync(string category, long categoryId) + { + var predicate = (ScheduleJob scheduleJob) => + { + var id = ScheduleJobIdHelper.ParseId(scheduleJob.Id); + return id.CategoryId == categoryId && id.Category.Equals(category); + }; + + await Context.Set() + .Where(scheduleJob => predicate(scheduleJob)) + .DeleteAsync() + .ConfigureAwait(true); + } + + public IQueryable GetAllByCategoryAsync(string category, long categoryId) + { + var predicate = (ScheduleJob scheduleJob) => + { + var id = ScheduleJobIdHelper.ParseId(scheduleJob.Id); + return id.CategoryId == categoryId && id.Category.Equals(category); + }; + + return FindAll(scheduleJob => predicate(scheduleJob)); + } + } +} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs deleted file mode 100644 index 4b1a9c155..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs +++ /dev/null @@ -1,18 +0,0 @@ -using HwProj.Models.NotificationsService; -using HwProj.NotificationsService.API.Models; -using HwProj.Repositories; - -namespace HwProj.NotificationsService.API.Repositories -{ - public interface IScheduleWorksRepository : ICrudRepository - { - } - - - public class ScheduleWorksRepository : CrudRepository, IScheduleWorksRepository - { - public ScheduleWorksRepository(NotificationsContext context) : base(context) - { - } - } -} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index 7fb0c2515..b8ac581ea 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -1,9 +1,8 @@ using Hangfire; using HwProj.AuthService.API.Events; using HwProj.AuthService.Client; -using HwProj.CoursesService.API.Events; using HwProj.EventBus.Client.Interfaces; -using HwProj.Events.CourseEvents; +using HwProj.Models.Events.CourseEvents; using HwProj.NotificationsService.API.EventHandlers; using HwProj.NotificationsService.API.Models; using HwProj.NotificationsService.API.Repositories; @@ -43,7 +42,7 @@ public void ConfigureServices(IServiceCollection services) services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); - services.AddScoped(); + services.AddScoped(); services.AddEventBus(Configuration); services.AddTransient, RegisterEventHandler>(); services.AddTransient, RateEventHandler>(); From 1a9e97821c0d952d6df692a4607b1f9ff9217e9f Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Wed, 22 Nov 2023 19:36:30 +0300 Subject: [PATCH 12/22] feat: transfer CourseDataFilterAttribute.cs to ApiGateway --- .../Controllers/CoursesController.cs | 2 ++ .../Filters/CourseDataFilterAttribute.cs | 24 +++++++++---------- .../Controllers/CoursesController.cs | 9 +++---- 3 files changed, 17 insertions(+), 18 deletions(-) rename {HwProj.CoursesService/HwProj.CoursesService.API => HwProj.APIGateway/HwProj.APIGateway.API}/Filters/CourseDataFilterAttribute.cs (60%) diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CoursesController.cs b/HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CoursesController.cs index 17725e28a..10ded91fa 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CoursesController.cs +++ b/HwProj.APIGateway/HwProj.APIGateway.API/Controllers/CoursesController.cs @@ -2,6 +2,7 @@ using System.Linq; using System.Net; using System.Threading.Tasks; +using HwProj.APIGateway.API.Filters; using HwProj.APIGateway.API.Models; using HwProj.AuthService.Client; using HwProj.CoursesService.Client; @@ -34,6 +35,7 @@ public async Task GetAllCourses() return result; } + [CourseDataFilter] [HttpGet("{courseId}")] [ProducesResponseType(typeof(CourseViewModel), (int)HttpStatusCode.OK)] public async Task GetCourseData(long courseId) diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Filters/CourseDataFilterAttribute.cs b/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs similarity index 60% rename from HwProj.CoursesService/HwProj.CoursesService.API/Filters/CourseDataFilterAttribute.cs rename to HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs index e584f2a61..8ae3de0a7 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Filters/CourseDataFilterAttribute.cs +++ b/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs @@ -1,19 +1,22 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using HwProj.Models; +using HwProj.Models.AuthService.DTO; using HwProj.Models.CoursesService.ViewModels; -using HwProj.Utils.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; -namespace HwProj.CoursesService.API.Filters +namespace HwProj.APIGateway.API.Filters { public class CourseDataFilterAttribute : ResultFilterAttribute { public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - var userId = context.HttpContext.Request.GetUserIdFromHeader(); + var userId = context.HttpContext.User.Claims + .FirstOrDefault(claim => claim.Type.ToString() == "_id") + ?.Value;; if (userId == null) { @@ -22,25 +25,22 @@ public override async Task OnResultExecutionAsync(ResultExecutingContext context else { var result = context.Result as ObjectResult; - if (result?.Value is CourseDTO courseDto && !courseDto.MentorIds.Contains(userId)) + if (result?.Value is CourseViewModel courseViewModel && + courseViewModel.Mentors.All(mentor => mentor.UserId != userId)) { var currentDate = DateTimeUtils.GetMoscowNow(); - foreach (var homework in courseDto.Homeworks) + foreach (var homework in courseViewModel.Homeworks) { homework.Tasks = new List(homework.Tasks.Where(t => currentDate >= t.PublicationDate)); } - courseDto.CourseMates = courseDto.CourseMates - .Where(t => t.IsAccepted || t.StudentId == userId) - .ToArray(); - - courseDto.Groups = courseDto.Groups.Where(g => g.StudentsIds.Contains(userId)).ToArray(); + courseViewModel.NewStudents = Array.Empty(); } } await next(); } } -} +} \ No newline at end of file diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs index d3c6095f1..5d25c888f 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Controllers/CoursesController.cs @@ -43,8 +43,7 @@ public async Task GetAll() var courses = _mapper.Map(coursesFromDb).ToArray(); return courses; } - - [CourseDataFilter] + [HttpGet("{courseId}")] public async Task Get(long courseId) { @@ -53,8 +52,7 @@ public async Task Get(long courseId) return Ok(course); } - - [CourseDataFilter] + [HttpGet("getByTask/{taskId}")] public async Task GetByTask(long taskId) { @@ -122,8 +120,7 @@ public async Task RejectStudent(long courseId, [FromQuery] string ? Ok() as IActionResult : NotFound(); } - - [CourseDataFilter] + [HttpGet("userCourses")] public async Task GetUserCourses(string role) { From 323dfd21206bc15cbc6a1b751baf0631422eb37e Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Wed, 22 Nov 2023 19:57:54 +0300 Subject: [PATCH 13/22] feat: add lazy getting course data in task event-handlers --- .../HwProj.HttpUtils/RequestHeaderBuilder.cs | 2 +- .../NotificationCategoryState.cs | 3 +- .../NewHomeworkTaskEventHandler.cs | 21 ++++-- .../EventHandlers/UpdateTaskEventHandler.cs | 28 +++++--- ...023000606_NotificationSettings.Designer.cs | 64 ------------------- .../20231023000606_NotificationSettings.cs | 28 -------- .../NotificationsContextModelSnapshot.cs | 62 ------------------ .../Startup.cs | 2 + .../20231025212103_SolutionsIndex.Designer.cs | 57 ----------------- .../20231025212103_SolutionsIndex.cs | 15 ----- .../SolutionContextModelSnapshot.cs | 55 ---------------- 11 files changed, 37 insertions(+), 300 deletions(-) delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.Designer.cs delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.cs delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/NotificationsContextModelSnapshot.cs delete mode 100644 HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.Designer.cs delete mode 100644 HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.cs delete mode 100644 HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/SolutionContextModelSnapshot.cs diff --git a/HwProj.Common/HwProj.HttpUtils/RequestHeaderBuilder.cs b/HwProj.Common/HwProj.HttpUtils/RequestHeaderBuilder.cs index 5ac4c68e2..cd1e3a995 100644 --- a/HwProj.Common/HwProj.HttpUtils/RequestHeaderBuilder.cs +++ b/HwProj.Common/HwProj.HttpUtils/RequestHeaderBuilder.cs @@ -7,7 +7,7 @@ public static class RequestHeaderBuilder { public static void TryAddUserId(this HttpRequestMessage request, IHttpContextAccessor httpContextAccessor) { - var userId = httpContextAccessor.HttpContext.User.FindFirst("_id"); + var userId = httpContextAccessor.HttpContext?.User.FindFirst("_id"); if (userId != null) request.Headers.Add("UserId", userId.Value); } } diff --git a/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs b/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs index af6d354f8..b55974612 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs @@ -5,6 +5,7 @@ public enum CategoryState None, Profile, Courses, - Homeworks + Homeworks, + Tasks } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index 826576a53..84ad2042a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Hangfire; using HwProj.AuthService.Client; +using HwProj.CoursesService.Client; using HwProj.EventBus.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; @@ -16,6 +17,7 @@ namespace HwProj.NotificationsService.API.EventHandlers { public class NewHomeworkTaskEventHandler : EventHandlerBase { + private readonly ICoursesServiceClient _coursesServiceClient; private readonly INotificationsRepository _notificationRepository; private readonly IScheduleJobsRepository _scheduleJobsRepository; private readonly IAuthServiceClient _authServiceClient; @@ -23,12 +25,14 @@ public class NewHomeworkTaskEventHandler : EventHandlerBase private readonly IEmailService _emailService; public NewHomeworkTaskEventHandler( + ICoursesServiceClient coursesServiceClient, INotificationsRepository notificationRepository, IScheduleJobsRepository scheduleJobsRepository, IAuthServiceClient authServiceClient, IConfiguration configuration, IEmailService emailService) { + _coursesServiceClient = coursesServiceClient; _notificationRepository = notificationRepository; _scheduleJobsRepository = scheduleJobsRepository; _authServiceClient = authServiceClient; @@ -52,21 +56,24 @@ await EventHandlerExtensions.AddScheduleJobAsync(@event, @event.Ta public async Task AddNotificationsAsync(NewTaskEvent @event) { - var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray(); + var course = await _coursesServiceClient.GetCourseById(@event.Course.Id); + if (course == null) return; + + var studentIds = course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); + var url = _configuration["Url"]; + var message = $"В курсе {course.Name}" + + $" опубликована новая задача {@event.Task.Title}." + + (@event.Task.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); foreach (var student in accountsData) { var notification = new Notification { Sender = "CourseService", - Body = - $"В курсе {@event.Course.Name}" + - $" опубликована новая задача {@event.Task.Title}." + - (@event.Task.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""), - - Category = CategoryState.Homeworks, + Body = message, + Category = CategoryState.Tasks, Date = DateTimeUtils.GetMoscowNow(), Owner = student!.UserId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index 1c1420745..bf734ab91 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -1,8 +1,10 @@ using System; +using System.Linq; using System.Threading.Tasks; using AutoMapper; using Hangfire; using HwProj.AuthService.Client; +using HwProj.CoursesService.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.AuthService.DTO; @@ -16,6 +18,7 @@ namespace HwProj.NotificationsService.API.EventHandlers { public class UpdateTaskEventHandler : EventHandlerBase { + private readonly ICoursesServiceClient _coursesServiceClient; private readonly INotificationsRepository _notificationRepository; private readonly IAuthServiceClient _authServiceClient; private readonly IMapper _mapper; @@ -24,6 +27,7 @@ public class UpdateTaskEventHandler : EventHandlerBase private readonly IScheduleJobsRepository _scheduleJobsRepository; public UpdateTaskEventHandler( + ICoursesServiceClient coursesServiceClient, INotificationsRepository notificationRepository, IMapper mapper, IAuthServiceClient authServiceClient, @@ -31,6 +35,7 @@ public UpdateTaskEventHandler( IEmailService emailService, IScheduleJobsRepository scheduleJobsRepository) { + _coursesServiceClient = coursesServiceClient; _notificationRepository = notificationRepository; _mapper = mapper; _authServiceClient = authServiceClient; @@ -54,35 +59,38 @@ await EventHandlerExtensions.UpdateScheduleJobAsync(@event, @ev public async Task AddNotificationsAsync(UpdateTaskEvent @event) { + var course = await _coursesServiceClient.GetCourseById(@event.Course.Id); + if (course == null) return; + + var studentIds = course.CourseMates.Select(t => t.StudentId).ToArray(); + var accountsData = await _authServiceClient.GetAccountsData(studentIds); + var url = _configuration["Url"]; var isTaskPublished = @event.PreviousEvent.PublicationDate < DateTimeUtils.GetMoscowNow(); var message = isTaskPublished ? $"Задача {@event.PreviousEvent.Title}" + - $" из курса {@event.Course.Name} обновлена." - : $"В курсе {@event.Course.Name}" + + $" из курса {course.Name} обновлена." + : $"В курсе {course.Name}" + $" опубликована новая задача {@event.NewEvent.Title}." + (@event.NewEvent.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); - foreach (var student in @event.Course.CourseMates) + foreach (var student in accountsData) { - var studentAccount = await _authServiceClient.GetAccountData(student.StudentId); - var studentModel = _mapper.Map(studentAccount); var notification = new Notification { Sender = "CourseService", Body = message, - Category = CategoryState.Courses, + Category = CategoryState.Tasks, Date = DateTimeUtils.GetMoscowNow(), - HasSeen = false, - Owner = student.StudentId + Owner = student!.UserId }; var addNotificationTask = _notificationRepository.AddAsync(notification); - var sendEmailTask = _emailService.SendEmailAsync(notification, studentModel.Email, "Домашняя работа"); + var sendEmailTask = _emailService.SendEmailAsync(notification, student.Email, "Новая задача"); await Task.WhenAll(addNotificationTask, sendEmailTask); } - + await EventHandlerExtensions.DeleteScheduleJobAsync(@event, @event.TaskId, _scheduleJobsRepository); } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.Designer.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.Designer.cs deleted file mode 100644 index 71e04909a..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.Designer.cs +++ /dev/null @@ -1,64 +0,0 @@ -// -using System; -using HwProj.NotificationsService.API.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace HwProj.NotificationsService.API.Migrations -{ - [DbContext(typeof(NotificationsContext))] - [Migration("20231023000606_NotificationSettings")] - partial class NotificationSettings - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "2.2.6-servicing-10079") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("HwProj.Models.NotificationsService.Notification", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - b.Property("Body"); - - b.Property("Category"); - - b.Property("Date"); - - b.Property("HasSeen"); - - b.Property("Owner"); - - b.Property("Sender"); - - b.HasKey("Id"); - - b.ToTable("Notifications"); - }); - - modelBuilder.Entity("HwProj.NotificationsService.API.Models.NotificationsSetting", b => - { - b.Property("UserId"); - - b.Property("Category"); - - b.Property("IsEnabled"); - - b.HasKey("UserId", "Category"); - - b.HasIndex("UserId"); - - b.ToTable("Settings"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.cs deleted file mode 100644 index 8970dda79..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/20231023000606_NotificationSettings.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -namespace HwProj.NotificationsService.API.Migrations -{ - public partial class NotificationSettings : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Settings", - columns: table => new - { - UserId = table.Column(nullable: false), - Category = table.Column(nullable: false), - IsEnabled = table.Column(nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Settings", x => new { x.UserId, x.Category }); - }); - - migrationBuilder.CreateIndex( - name: "IX_Settings_UserId", - table: "Settings", - column: "UserId"); - } - } -} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/NotificationsContextModelSnapshot.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/NotificationsContextModelSnapshot.cs deleted file mode 100644 index 2e38006a5..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Migrations/NotificationsContextModelSnapshot.cs +++ /dev/null @@ -1,62 +0,0 @@ -// -using System; -using HwProj.NotificationsService.API.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace HwProj.NotificationsService.API.Migrations -{ - [DbContext(typeof(NotificationsContext))] - partial class NotificationsContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "2.2.6-servicing-10079") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("HwProj.Models.NotificationsService.Notification", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - b.Property("Body"); - - b.Property("Category"); - - b.Property("Date"); - - b.Property("HasSeen"); - - b.Property("Owner"); - - b.Property("Sender"); - - b.HasKey("Id"); - - b.ToTable("Notifications"); - }); - - modelBuilder.Entity("HwProj.NotificationsService.API.Models.NotificationsSetting", b => - { - b.Property("UserId"); - - b.Property("Category"); - - b.Property("IsEnabled"); - - b.HasKey("UserId", "Category"); - - b.HasIndex("UserId"); - - b.ToTable("Settings"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index b8ac581ea..b72cc5b6b 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -1,6 +1,7 @@ using Hangfire; using HwProj.AuthService.API.Events; using HwProj.AuthService.Client; +using HwProj.CoursesService.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Models.Events.CourseEvents; using HwProj.NotificationsService.API.EventHandlers; @@ -62,6 +63,7 @@ public void ConfigureServices(IServiceCollection services) services.AddHttpClient(); services.AddAuthServiceClient(); + services.AddCoursesServiceClient(); services.ConfigureHwProjServices("Notifications API"); } diff --git a/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.Designer.cs b/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.Designer.cs deleted file mode 100644 index efc89b036..000000000 --- a/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.Designer.cs +++ /dev/null @@ -1,57 +0,0 @@ -// -using System; -using HwProj.SolutionsService.API.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace HwProj.SolutionsService.API.Migrations -{ - [DbContext(typeof(SolutionContext))] - [Migration("20231025212103_SolutionsIndex")] - partial class SolutionsIndex - { - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "2.2.6-servicing-10079") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("HwProj.Models.SolutionsService.Solution", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - b.Property("Comment"); - - b.Property("GithubUrl"); - - b.Property("GroupId"); - - b.Property("LecturerComment"); - - b.Property("PublicationDate"); - - b.Property("Rating"); - - b.Property("State"); - - b.Property("StudentId"); - - b.Property("TaskId"); - - b.HasKey("Id"); - - b.HasIndex("TaskId"); - - b.ToTable("Solutions"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.cs b/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.cs deleted file mode 100644 index 4b3e5e7f3..000000000 --- a/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/20231025212103_SolutionsIndex.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -namespace HwProj.SolutionsService.API.Migrations -{ - public partial class SolutionsIndex : Migration - { - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateIndex( - name: "IX_Solutions_TaskId", - table: "Solutions", - column: "TaskId"); - } - } -} diff --git a/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/SolutionContextModelSnapshot.cs b/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/SolutionContextModelSnapshot.cs deleted file mode 100644 index 2111fe91c..000000000 --- a/HwProj.SolutionsService/HwProj.SolutionsService.API/Migrations/SolutionContextModelSnapshot.cs +++ /dev/null @@ -1,55 +0,0 @@ -// -using System; -using HwProj.SolutionsService.API.Models; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -namespace HwProj.SolutionsService.API.Migrations -{ - [DbContext(typeof(SolutionContext))] - partial class SolutionContextModelSnapshot : ModelSnapshot - { - protected override void BuildModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "2.2.6-servicing-10079") - .HasAnnotation("Relational:MaxIdentifierLength", 128) - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - modelBuilder.Entity("HwProj.Models.SolutionsService.Solution", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); - - b.Property("Comment"); - - b.Property("GithubUrl"); - - b.Property("GroupId"); - - b.Property("LecturerComment"); - - b.Property("PublicationDate"); - - b.Property("Rating"); - - b.Property("State"); - - b.Property("StudentId"); - - b.Property("TaskId"); - - b.HasKey("Id"); - - b.HasIndex("TaskId"); - - b.ToTable("Solutions"); - }); -#pragma warning restore 612, 618 - } - } -} From 1d4d5b555112e12498d927711118b721da996ce8 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Wed, 22 Nov 2023 20:10:04 +0300 Subject: [PATCH 14/22] feat: change part of ScheduleWorkId --- .../NotificationsService/ScheduleWork.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index 92edd3a54..e76a34576 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -26,21 +26,21 @@ public class ScheduleWorkId { public string Category { get; set; } - public Type? EventType { get; set; } + public string EventTypeName { get; set; } public long CategoryId { get; set; } - public ScheduleWorkId(string category, Type? eventType, long categoryId) + public ScheduleWorkId(string category, string eventTypeName, long categoryId) { Category = category; - EventType = eventType; + EventTypeName = eventTypeName; CategoryId = categoryId; } public override string ToString() { - return $"{Category}/{EventType}/{CategoryId}"; + return $"{Category}/{EventTypeName}/{CategoryId}"; } } @@ -48,7 +48,7 @@ public static class ScheduleJobIdHelper { public static ScheduleWorkId BuildId(Event @event, long categoryId) { - return new ScheduleWorkId(GetCategory(@event), @event.GetType(), categoryId); + return new ScheduleWorkId(GetCategory(@event), @event.GetType().Name, categoryId); } public static string GetCategory(Event @event) @@ -69,7 +69,7 @@ public static ScheduleWorkId ParseId(string id) { var parts = id.Split('/'); - return new ScheduleWorkId(parts[0], Type.GetType(parts[1]), int.Parse(parts[2])); + return new ScheduleWorkId(parts[0], parts[1], int.Parse(parts[2])); } } } \ No newline at end of file From d220d2164ea90a8b51d4b897d4a2b7f859c04f2d Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Sun, 3 Dec 2023 15:33:08 +0300 Subject: [PATCH 15/22] feat: transfer hangfire dashboard to apigateway --- .../HwProj.APIGateway.API.csproj | 5 +++++ .../HwProj.APIGateway.API/Startup.cs | 20 ++++++++++++++++--- .../HwProj.APIGateway.API/appsettings.json | 4 ++++ .../Startup.cs | 2 -- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj b/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj index 29e5d8b3c..15c49b2cd 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj +++ b/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj @@ -10,6 +10,10 @@ + + + + @@ -20,6 +24,7 @@ + diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs b/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs index 7f353c780..699c9f735 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs +++ b/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs @@ -1,4 +1,6 @@ -using HwProj.AuthService.Client; +using Hangfire; +using Hangfire.SqlServer; +using HwProj.AuthService.Client; using HwProj.CoursesService.Client; using HwProj.NotificationsService.Client; using HwProj.SolutionsService.Client; @@ -25,8 +27,15 @@ public void ConfigureServices(IServiceCollection services) { services.ConfigureHwProjServices("API Gateway"); - const string authenticationProviderKey = "GatewayKey"; + services.AddHangfire(configuration => configuration + .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) + .UseSimpleAssemblyNameTypeSerializer() + .UseRecommendedSerializerSettings() + .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))); + + const string authenticationProviderKey = "GatewayKey"; + services.AddAuthentication() .AddJwtBearer(authenticationProviderKey, x => { @@ -54,6 +63,11 @@ public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.ConfigureHwProj(env, "API Gateway"); + + if (env.IsDevelopment()) + { + app.UseHangfireDashboard("/jobs"); + } } } -} +} \ No newline at end of file diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json b/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json index 37190eed9..855c75228 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json +++ b/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json @@ -4,5 +4,9 @@ "Courses": "http://localhost:5002", "Notifications": "http://localhost:5006", "Solutions": "http://localhost:5007" + }, + + "ConnectionStrings": { + "HangfireConnection": "Server=(localdb)\\mssqllocaldb;Database=HangfireDB;Trusted_Connection=True;" } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index b72cc5b6b..9108df52e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -31,7 +31,6 @@ public void ConfigureServices(IServiceCollection services) { var connectionString = ConnectionString.GetConnectionString(Configuration); - // Add Hangfire services. services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) .UseSimpleAssemblyNameTypeSerializer() @@ -89,7 +88,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu eventBustSubscriber.Subscribe(); } - app.UseHangfireDashboard(); app.ConfigureHwProj(env, "Notifications API", context); } } From 6476105787739d6d4f6550f023fd5a543756bc23 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor <117931736+BakhorikovEgor@users.noreply.github.com> Date: Sun, 7 Jan 2024 23:19:30 +0300 Subject: [PATCH 16/22] Revert "feat: transfer hangfire dashboard to apigateway" This reverts commit d220d2164ea90a8b51d4b897d4a2b7f859c04f2d. --- .../HwProj.APIGateway.API.csproj | 5 ----- .../HwProj.APIGateway.API/Startup.cs | 20 +++---------------- .../HwProj.APIGateway.API/appsettings.json | 4 ---- .../Startup.cs | 2 ++ 4 files changed, 5 insertions(+), 26 deletions(-) diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj b/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj index 15c49b2cd..29e5d8b3c 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj +++ b/HwProj.APIGateway/HwProj.APIGateway.API/HwProj.APIGateway.API.csproj @@ -10,10 +10,6 @@ - - - - @@ -24,7 +20,6 @@ - diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs b/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs index 699c9f735..7f353c780 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs +++ b/HwProj.APIGateway/HwProj.APIGateway.API/Startup.cs @@ -1,6 +1,4 @@ -using Hangfire; -using Hangfire.SqlServer; -using HwProj.AuthService.Client; +using HwProj.AuthService.Client; using HwProj.CoursesService.Client; using HwProj.NotificationsService.Client; using HwProj.SolutionsService.Client; @@ -27,15 +25,8 @@ public void ConfigureServices(IServiceCollection services) { services.ConfigureHwProjServices("API Gateway"); - services.AddHangfire(configuration => configuration - .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) - .UseSimpleAssemblyNameTypeSerializer() - .UseRecommendedSerializerSettings() - .UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"))); - - const string authenticationProviderKey = "GatewayKey"; - + services.AddAuthentication() .AddJwtBearer(authenticationProviderKey, x => { @@ -63,11 +54,6 @@ public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.ConfigureHwProj(env, "API Gateway"); - - if (env.IsDevelopment()) - { - app.UseHangfireDashboard("/jobs"); - } } } -} \ No newline at end of file +} diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json b/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json index 855c75228..37190eed9 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json +++ b/HwProj.APIGateway/HwProj.APIGateway.API/appsettings.json @@ -4,9 +4,5 @@ "Courses": "http://localhost:5002", "Notifications": "http://localhost:5006", "Solutions": "http://localhost:5007" - }, - - "ConnectionStrings": { - "HangfireConnection": "Server=(localdb)\\mssqllocaldb;Database=HangfireDB;Trusted_Connection=True;" } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index 9108df52e..b72cc5b6b 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -31,6 +31,7 @@ public void ConfigureServices(IServiceCollection services) { var connectionString = ConnectionString.GetConnectionString(Configuration); + // Add Hangfire services. services.AddHangfire(configuration => configuration .SetDataCompatibilityLevel(CompatibilityLevel.Version_180) .UseSimpleAssemblyNameTypeSerializer() @@ -88,6 +89,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu eventBustSubscriber.Subscribe(); } + app.UseHangfireDashboard(); app.ConfigureHwProj(env, "Notifications API", context); } } From 7ca52e40c2053bfe07d267bd3137972b71027041 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor Date: Sat, 17 Feb 2024 01:05:40 +0300 Subject: [PATCH 17/22] fix: composite delayed job key = --- .../Filters/CourseDataFilterAttribute.cs | 4 +- .../NotificationCategoryState.cs | 3 +- .../NotificationsService/ScheduleWork.cs | 55 ++++----------- .../EventHandlers/EventHandlerExtensions.cs | 53 ++++++--------- .../NewHomeworkTaskEventHandler.cs | 5 +- .../EventHandlers/UpdateTaskEventHandler.cs | 5 +- .../Models/NotificationsContext.cs | 1 + .../Repositories/ScheduleJobsRepository.cs | 68 ++++++++++++------- 8 files changed, 83 insertions(+), 111 deletions(-) diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs b/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs index 8ae3de0a7..aab1893a0 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs +++ b/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs @@ -14,9 +14,7 @@ public class CourseDataFilterAttribute : ResultFilterAttribute { public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) { - var userId = context.HttpContext.User.Claims - .FirstOrDefault(claim => claim.Type.ToString() == "_id") - ?.Value;; + var userId = context.HttpContext.User.FindFirst("_id").Value; if (userId == null) { diff --git a/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs b/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs index b55974612..af6d354f8 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/NotificationCategoryState.cs @@ -5,7 +5,6 @@ public enum CategoryState None, Profile, Courses, - Homeworks, - Tasks + Homeworks } } \ No newline at end of file diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs index e76a34576..9629b9080 100644 --- a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs +++ b/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs @@ -1,56 +1,33 @@ using System; -using System.ComponentModel.DataAnnotations; using HwProj.EventBus.Client; using HwProj.Models.Events.CourseEvents; -using HwProj.Repositories; namespace HwProj.Models.NotificationsService { - public class ScheduleJob : IEntity - { - [Key] public string Id { get; set; } - public string JobId { get; set; } - - public ScheduleJob(ScheduleWorkId id, string jobId) - { - Id = id.ToString(); - JobId = jobId; - } - - public ScheduleJob() - { - } - } - - public class ScheduleWorkId + public class ScheduleJob { public string Category { get; set; } - public string EventTypeName { get; set; } - - public long CategoryId { get; set; } + public string EventName { get; set; } - public ScheduleWorkId(string category, string eventTypeName, long categoryId) - { - Category = category; - EventTypeName = eventTypeName; - CategoryId = categoryId; - } + public long ItemId { get; set; } + public string JobId { get; set; } - public override string ToString() + public ScheduleJob(Event @event, long itemId, string jobId) { - return $"{Category}/{EventTypeName}/{CategoryId}"; + Category = ScheduleJobIdHelper.GetCategory(@event); + EventName = ScheduleJobIdHelper.GetEventName(@event); + ItemId = itemId; + JobId = jobId; } + + public ScheduleJob(){} } + public static class ScheduleJobIdHelper { - public static ScheduleWorkId BuildId(Event @event, long categoryId) - { - return new ScheduleWorkId(GetCategory(@event), @event.GetType().Name, categoryId); - } - public static string GetCategory(Event @event) { var eventType = @event.GetType(); @@ -65,11 +42,7 @@ public static string GetCategory(Event @event) }; } - public static ScheduleWorkId ParseId(string id) - { - var parts = id.Split('/'); - - return new ScheduleWorkId(parts[0], parts[1], int.Parse(parts[2])); - } + public static string GetEventName(Event @event) + => @event.ToString(); } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs index 52a616195..b161da295 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs @@ -10,52 +10,39 @@ namespace HwProj.NotificationsService.API.EventHandlers { public static class EventHandlerExtensions where TEvent : Event { - public static async Task AddScheduleJobAsync(TEvent @event, long categoryId, DateTime publicationDate, + public static async Task AddScheduleJobAsync(TEvent @event, long itemId, DateTime publicationDate, Expression> jobFunc, IScheduleJobsRepository jobsRepository) { - var id = ScheduleJobIdHelper.BuildId(@event, categoryId); - var jobId = BackgroundJob.Schedule(jobFunc, publicationDate.Subtract(TimeSpan.FromHours(3))); - var scheduleJob = new ScheduleJob(id, jobId); - + var jobId = BackgroundJob.Schedule(jobFunc, publicationDate); + var scheduleJob = new ScheduleJob(@event, itemId, jobId); + + BackgroundJob.Schedule(() => jobsRepository.DeleteAsync(@event, itemId), publicationDate); + await jobsRepository.AddAsync(scheduleJob); } + + public static async Task UpdateScheduleJobAsync(TEvent @event, long itemId, DateTime publicationDate, + Expression> jobFunc, IScheduleJobsRepository jobsRepository) + { + await DeleteScheduleJobsAsync(@event, itemId, jobsRepository); + await AddScheduleJobAsync(@event, itemId, publicationDate, jobFunc, jobsRepository); + } + - public static async Task DeleteScheduleJobsAsync(TEvent @event, long categoryId, IScheduleJobsRepository jobsRepository) + public static async Task DeleteScheduleJobsAsync(TEvent @event, long itemId, + IScheduleJobsRepository jobsRepository) { var category = ScheduleJobIdHelper.GetCategory(@event); - var scheduleJobs = jobsRepository.GetAllByCategoryAsync(category, categoryId); + var scheduleJobs = jobsRepository.FindAll(scheduleJob => + scheduleJob.Category.Equals(category) && scheduleJob.ItemId == itemId); + foreach (var scheduleJob in scheduleJobs) { BackgroundJob.Delete(scheduleJob.JobId); } - await jobsRepository.DeleteAllByCategoryAsync(category, categoryId); - } - - - public static async Task UpdateScheduleJobAsync(TEvent @event, long categoryId, DateTime publicationDate, - Expression> jobFunc, IScheduleJobsRepository jobsRepository) - { - await DeleteScheduleJobsAsync(@event, categoryId, jobsRepository); - - var id = ScheduleJobIdHelper.BuildId(@event, categoryId); - var jobId = BackgroundJob.Schedule(jobFunc, publicationDate.Subtract(TimeSpan.FromHours(3))); - var scheduleJob = new ScheduleJob(id, jobId); - - await jobsRepository.AddAsync(scheduleJob); - } - - - public static async Task DeleteScheduleJobAsync(TEvent @event, long categoryId, IScheduleJobsRepository jobsRepository) - { - var id = ScheduleJobIdHelper.BuildId(@event, categoryId); - var scheduleJob = await jobsRepository.GetAsync(id.ToString()); - - BackgroundJob.Delete(scheduleJob.JobId); - - await jobsRepository.DeleteAsync(id.ToString()); + await jobsRepository.DeleteAllInCategoryByItemIdAsync(@event, itemId); } - } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index 84ad2042a..4577278bd 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -73,7 +73,7 @@ public async Task AddNotificationsAsync(NewTaskEvent @event) { Sender = "CourseService", Body = message, - Category = CategoryState.Tasks, + Category = CategoryState.Homeworks, Date = DateTimeUtils.GetMoscowNow(), Owner = student!.UserId }; @@ -83,9 +83,6 @@ public async Task AddNotificationsAsync(NewTaskEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } - - await EventHandlerExtensions.DeleteScheduleJobAsync(@event, @event.TaskId, - _scheduleJobsRepository); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index bf734ab91..d3ba5cc6a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -80,7 +80,7 @@ public async Task AddNotificationsAsync(UpdateTaskEvent @event) { Sender = "CourseService", Body = message, - Category = CategoryState.Tasks, + Category = CategoryState.Homeworks, Date = DateTimeUtils.GetMoscowNow(), Owner = student!.UserId }; @@ -90,9 +90,6 @@ public async Task AddNotificationsAsync(UpdateTaskEvent @event) await Task.WhenAll(addNotificationTask, sendEmailTask); } - - await EventHandlerExtensions.DeleteScheduleJobAsync(@event, @event.TaskId, - _scheduleJobsRepository); } } } \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index 16ab15a72..68170c549 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -19,6 +19,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasIndex(n => n.UserId); modelBuilder.Entity().HasKey(n => new { n.UserId, n.Category }); + modelBuilder.Entity().HasKey(s => new {s.Category, s.EventName, s.ItemId}); } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs index 2ee5bf76b..17a935c9e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs @@ -1,49 +1,69 @@ -using System.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; using System.Threading.Tasks; +using HwProj.EventBus.Client; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Models; -using HwProj.Repositories; +using Microsoft.EntityFrameworkCore; using Z.EntityFramework.Plus; namespace HwProj.NotificationsService.API.Repositories { - public interface IScheduleJobsRepository : ICrudRepository + public interface IScheduleJobsRepository { - public Task DeleteAllByCategoryAsync(string category, long categoryId); + public Task AddAsync(ScheduleJob scheduleJob); - public IQueryable GetAllByCategoryAsync(string category, long categoryId); + public Task DeleteAsync(Event @event, long itemId); + + public Task DeleteAllInCategoryByItemIdAsync(Event @event, long itemId); + + public IEnumerable FindAll(Expression> predicate); } - public class ScheduleJobsRepository : CrudRepository, IScheduleJobsRepository + public class ScheduleJobsRepository : IScheduleJobsRepository { - public ScheduleJobsRepository(NotificationsContext context) : base(context) + private readonly NotificationsContext _context; + + public ScheduleJobsRepository(NotificationsContext context) { + _context = context; } - public async Task DeleteAllByCategoryAsync(string category, long categoryId) + public async Task AddAsync(ScheduleJob scheduleJob) { - var predicate = (ScheduleJob scheduleJob) => - { - var id = ScheduleJobIdHelper.ParseId(scheduleJob.Id); - return id.CategoryId == categoryId && id.Category.Equals(category); - }; - - await Context.Set() - .Where(scheduleJob => predicate(scheduleJob)) + await _context.AddAsync(scheduleJob).ConfigureAwait(false); + await _context.SaveChangesAsync().ConfigureAwait(false); + } + + public async Task DeleteAsync(Event @event, long itemId) + { + var category = ScheduleJobIdHelper.GetCategory(@event); + var eventName = ScheduleJobIdHelper.GetEventName(@event); + + await _context.Set() + .Where(scheduleJob => + scheduleJob.Category.Equals(category) && scheduleJob.EventName.Equals(eventName) && + scheduleJob.ItemId == itemId) .DeleteAsync() - .ConfigureAwait(true); + .ConfigureAwait(false); } - public IQueryable GetAllByCategoryAsync(string category, long categoryId) + public async Task DeleteAllInCategoryByItemIdAsync(Event @event, long itemId) { - var predicate = (ScheduleJob scheduleJob) => - { - var id = ScheduleJobIdHelper.ParseId(scheduleJob.Id); - return id.CategoryId == categoryId && id.Category.Equals(category); - }; + var category = ScheduleJobIdHelper.GetCategory(@event); - return FindAll(scheduleJob => predicate(scheduleJob)); + await _context.Set() + .Where(scheduleJob => scheduleJob.Category.Equals(category) && scheduleJob.ItemId == itemId) + .DeleteAsync() + .ConfigureAwait(true); + } + + public IEnumerable FindAll(Expression> predicate) + { + return _context.Set().AsNoTracking().Where(predicate); } } } \ No newline at end of file From 5d50b4a9a8933f893b36f73e1dcc7b9486b0048a Mon Sep 17 00:00:00 2001 From: BakhorikovEgor Date: Sat, 17 Feb 2024 01:30:37 +0300 Subject: [PATCH 18/22] feat: better choice for continuation --- .../EventHandlers/EventHandlerExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs index b161da295..562a10ae6 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs @@ -16,7 +16,7 @@ public static async Task AddScheduleJobAsync(TEvent @event, long itemId, DateTim var jobId = BackgroundJob.Schedule(jobFunc, publicationDate); var scheduleJob = new ScheduleJob(@event, itemId, jobId); - BackgroundJob.Schedule(() => jobsRepository.DeleteAsync(@event, itemId), publicationDate); + BackgroundJob.ContinueJobWith(jobId, () => jobsRepository.DeleteAsync(@event, itemId), JobContinuationOptions.OnAnyFinishedState); await jobsRepository.AddAsync(scheduleJob); } From 3017b8f6497b7555e05ec32ce282ac6bdd29c291 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor Date: Sat, 17 Feb 2024 01:51:23 +0300 Subject: [PATCH 19/22] fix: style fixes --- .../Filters/CourseDataFilterAttribute.cs | 2 +- .../CoursesService/ViewModels/HomeworkTaskViewModels.cs | 2 +- .../Events/CourseEvents/LecturerRejectToCourseEvent.cs | 2 +- .../HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs | 2 +- HwProj.Common/HwProj.Repositories/IEntity.cs | 2 +- .../HwProj.CoursesService.API/Services/TasksService.cs | 2 +- .../HwProj.CoursesService.API/appsettings.json | 2 +- HwProj.EventBus/HwProj.EventBus.Client/Event.cs | 8 +++++--- .../EventHandlers/NewHomeworkTaskEventHandler.cs | 2 +- .../HwProj.NotificationsService.API/Models/ScheduleJob.cs | 0 10 files changed, 13 insertions(+), 11 deletions(-) rename HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs => HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs (100%) diff --git a/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs b/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs index aab1893a0..92a8db78e 100644 --- a/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs +++ b/HwProj.APIGateway/HwProj.APIGateway.API/Filters/CourseDataFilterAttribute.cs @@ -41,4 +41,4 @@ public override async Task OnResultExecutionAsync(ResultExecutingContext context await next(); } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs b/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs index 1f6967b5b..d4508c4f2 100644 --- a/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs +++ b/HwProj.Common/HwProj.Models/CoursesService/ViewModels/HomeworkTaskViewModels.cs @@ -69,4 +69,4 @@ public void InitializeDeadline() } } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs index 4b7355a8a..516db5a3b 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs @@ -9,4 +9,4 @@ public class LecturerRejectToCourseEvent : Event public string MentorIds { get; set; } public string StudentId { get; set; } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs index 9b8e79656..f88365221 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs @@ -14,4 +14,4 @@ public NewHomeworkEvent(string homework, CourseDTO course) Course = course; } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Repositories/IEntity.cs b/HwProj.Common/HwProj.Repositories/IEntity.cs index 1581df22a..3a9ebbad0 100644 --- a/HwProj.Common/HwProj.Repositories/IEntity.cs +++ b/HwProj.Common/HwProj.Repositories/IEntity.cs @@ -7,4 +7,4 @@ public interface IEntity { TKey Id { get; set; } } -} \ No newline at end of file +} diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index 83f5c86d3..6a02c3274 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -78,4 +78,4 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) }); } } -} \ No newline at end of file +} diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json b/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json index 2db942611..cd71b77f6 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json +++ b/HwProj.CoursesService/HwProj.CoursesService.API/appsettings.json @@ -5,7 +5,7 @@ }, "Logging": { "LogLevel": { - "Default": "Warning", + "Default": "Warning" } }, "AllowedHosts": "*", diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs index 1260e0299..8ba45c4b7 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs @@ -5,9 +5,11 @@ namespace HwProj.EventBus.Client { public class Event { - [JsonProperty] public Guid Id { get; set; } + [JsonProperty] + public Guid Id { get; set; } - [JsonProperty] public DateTime CreationData { get; set; } + [JsonProperty] + public DateTime CreationData { get; set; } public Event() { @@ -21,4 +23,4 @@ public Event(Guid id, DateTime data) CreationData = data; } } -} \ No newline at end of file +} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index 4577278bd..cf5fee558 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -85,4 +85,4 @@ public async Task AddNotificationsAsync(NewTaskEvent @event) } } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs similarity index 100% rename from HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs rename to HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs From 8d8cec2fe2b8c819fee61adc598bf193c2afee91 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor Date: Fri, 23 Feb 2024 13:10:04 +0300 Subject: [PATCH 20/22] fix: duplicate reference fix --- HwProj.AuthService/HwProj.AuthService.API/RoleInitializer.cs | 2 +- .../HwProj.AuthService.API/Services/AccountService.cs | 2 +- .../HwProj.Models/Events/AuthEvents}/AdminRegisterEvent.cs | 2 +- .../HwProj.Models/Events/AuthEvents}/InviteLecturerEvent.cs | 2 +- .../HwProj.Models/Events/AuthEvents}/PasswordRecoveryEvent.cs | 2 +- .../HwProj.Models/Events/AuthEvents}/RegisterEvent.cs | 2 +- .../HwProj.Models/Events/AuthEvents}/StudentRegisterEvent.cs | 2 +- .../HwProj.Models/Events/SolutionEvents}/RateEvent.cs | 2 +- .../Events/SolutionEvents}/StudentPassTaskEvent.cs | 2 +- .../EventHandlers/InviteLecturerEventHandler.cs | 2 +- .../EventHandlers/PasswordRecoveryEventHandler.cs | 2 +- .../EventHandlers/RateEventHandler.cs | 2 +- .../EventHandlers/RegisterEventHandler.cs | 2 +- .../EventHandlers/StudentPassTaskEventHandler.cs | 2 +- .../HwProj.NotificationsService.API.csproj | 4 +--- .../HwProj.NotificationsService.API/Startup.cs | 4 ++-- .../HwProj.SolutionsService.API/Services/SolutionsService.cs | 2 +- 17 files changed, 18 insertions(+), 20 deletions(-) rename {HwProj.AuthService/HwProj.AuthService.API/Events => HwProj.Common/HwProj.Models/Events/AuthEvents}/AdminRegisterEvent.cs (85%) rename {HwProj.AuthService/HwProj.AuthService.API/Events => HwProj.Common/HwProj.Models/Events/AuthEvents}/InviteLecturerEvent.cs (81%) rename {HwProj.AuthService/HwProj.AuthService.API/Events => HwProj.Common/HwProj.Models/Events/AuthEvents}/PasswordRecoveryEvent.cs (88%) rename {HwProj.AuthService/HwProj.AuthService.API/Events => HwProj.Common/HwProj.Models/Events/AuthEvents}/RegisterEvent.cs (93%) rename {HwProj.AuthService/HwProj.AuthService.API/Events => HwProj.Common/HwProj.Models/Events/AuthEvents}/StudentRegisterEvent.cs (85%) rename {HwProj.SolutionsService/HwProj.SolutionsService.API/Events => HwProj.Common/HwProj.Models/Events/SolutionEvents}/RateEvent.cs (90%) rename {HwProj.SolutionsService/HwProj.SolutionsService.API/Events => HwProj.Common/HwProj.Models/Events/SolutionEvents}/StudentPassTaskEvent.cs (93%) diff --git a/HwProj.AuthService/HwProj.AuthService.API/RoleInitializer.cs b/HwProj.AuthService/HwProj.AuthService.API/RoleInitializer.cs index f3bcd0143..dd83433e4 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/RoleInitializer.cs +++ b/HwProj.AuthService/HwProj.AuthService.API/RoleInitializer.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Identity; using System.Threading.Tasks; -using HwProj.AuthService.API.Events; +using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; using HwProj.Models.Roles; using HwProj.Models.AuthService.ViewModels; diff --git a/HwProj.AuthService/HwProj.AuthService.API/Services/AccountService.cs b/HwProj.AuthService/HwProj.AuthService.API/Services/AccountService.cs index f65b5d28c..84ada40a1 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/Services/AccountService.cs +++ b/HwProj.AuthService/HwProj.AuthService.API/Services/AccountService.cs @@ -5,7 +5,7 @@ using AutoMapper; using HwProj.AuthService.API.Extensions; using HwProj.Models.Roles; -using HwProj.AuthService.API.Events; +using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; using HwProj.Models.AuthService.DTO; using HwProj.Models.AuthService.ViewModels; diff --git a/HwProj.AuthService/HwProj.AuthService.API/Events/AdminRegisterEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs similarity index 85% rename from HwProj.AuthService/HwProj.AuthService.API/Events/AdminRegisterEvent.cs rename to HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs index 09fc75942..050f8844a 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/Events/AdminRegisterEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs @@ -1,4 +1,4 @@ -namespace HwProj.AuthService.API.Events +namespace HwProj.Models.Events.AuthEvents { public class AdminRegisterEvent : RegisterEvent { diff --git a/HwProj.AuthService/HwProj.AuthService.API/Events/InviteLecturerEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs similarity index 81% rename from HwProj.AuthService/HwProj.AuthService.API/Events/InviteLecturerEvent.cs rename to HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs index 345d6cc40..4982d1aad 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/Events/InviteLecturerEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.AuthService.API.Events +namespace HwProj.Models.Events.AuthEvents { public class InviteLecturerEvent : Event { diff --git a/HwProj.AuthService/HwProj.AuthService.API/Events/PasswordRecoveryEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs similarity index 88% rename from HwProj.AuthService/HwProj.AuthService.API/Events/PasswordRecoveryEvent.cs rename to HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs index c751d6521..dab4f1fcf 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/Events/PasswordRecoveryEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.AuthService.API.Events +namespace HwProj.Models.Events.AuthEvents { public class PasswordRecoveryEvent : Event { diff --git a/HwProj.AuthService/HwProj.AuthService.API/Events/RegisterEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/RegisterEvent.cs similarity index 93% rename from HwProj.AuthService/HwProj.AuthService.API/Events/RegisterEvent.cs rename to HwProj.Common/HwProj.Models/Events/AuthEvents/RegisterEvent.cs index c2625e5ba..9aba0fac7 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/Events/RegisterEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/RegisterEvent.cs @@ -1,6 +1,6 @@ using HwProj.EventBus.Client; -namespace HwProj.AuthService.API.Events +namespace HwProj.Models.Events.AuthEvents { public abstract class RegisterEvent : Event { diff --git a/HwProj.AuthService/HwProj.AuthService.API/Events/StudentRegisterEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs similarity index 85% rename from HwProj.AuthService/HwProj.AuthService.API/Events/StudentRegisterEvent.cs rename to HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs index 5c894d2b8..cf3f2f438 100644 --- a/HwProj.AuthService/HwProj.AuthService.API/Events/StudentRegisterEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs @@ -1,4 +1,4 @@ -namespace HwProj.AuthService.API.Events +namespace HwProj.Models.Events.AuthEvents { public class StudentRegisterEvent : RegisterEvent { diff --git a/HwProj.SolutionsService/HwProj.SolutionsService.API/Events/RateEvent.cs b/HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs similarity index 90% rename from HwProj.SolutionsService/HwProj.SolutionsService.API/Events/RateEvent.cs rename to HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs index 9a1d88091..3fe70855f 100644 --- a/HwProj.SolutionsService/HwProj.SolutionsService.API/Events/RateEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs @@ -2,7 +2,7 @@ using HwProj.Models.CoursesService.ViewModels; using HwProj.Models.SolutionsService; -namespace HwProj.SolutionsService.API.Events +namespace HwProj.Models.Events.SolutionEvents { public class RateEvent : Event { diff --git a/HwProj.SolutionsService/HwProj.SolutionsService.API/Events/StudentPassTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs similarity index 93% rename from HwProj.SolutionsService/HwProj.SolutionsService.API/Events/StudentPassTaskEvent.cs rename to HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs index e9798b28d..3708e12a5 100644 --- a/HwProj.SolutionsService/HwProj.SolutionsService.API/Events/StudentPassTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs @@ -3,7 +3,7 @@ using HwProj.Models.CoursesService.ViewModels; using HwProj.Models.SolutionsService; -namespace HwProj.SolutionsService.API.Events +namespace HwProj.Models.Events.SolutionEvents { public class StudentPassTaskEvent : Event { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs index f8ea9fcff..9a08f994c 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs @@ -1,5 +1,5 @@ using System.Threading.Tasks; -using HwProj.AuthService.API.Events; +using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs index c77512fb1..7e748ba51 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; using System.Web; -using HwProj.AuthService.API.Events; +using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs index 55d83bcf1..d9b979278 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs @@ -5,7 +5,7 @@ using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; -using HwProj.SolutionsService.API.Events; +using HwProj.Models.Events.SolutionEvents; using Microsoft.Extensions.Configuration; namespace HwProj.NotificationsService.API.EventHandlers diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs index e094fdc05..fe42b0d47 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs @@ -1,5 +1,5 @@ using System.Threading.Tasks; -using HwProj.AuthService.API.Events; +using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; using HwProj.Models; using HwProj.Models.NotificationsService; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/StudentPassTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/StudentPassTaskEventHandler.cs index 6af48b207..e995bcb8c 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/StudentPassTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/StudentPassTaskEventHandler.cs @@ -5,7 +5,7 @@ using HwProj.NotificationsService.API.Models; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; -using HwProj.SolutionsService.API.Events; +using HwProj.Models.Events.SolutionEvents; using Microsoft.Extensions.Configuration; namespace HwProj.NotificationsService.API.EventHandlers diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj b/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj index a1e893393..7a5b10658 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/HwProj.NotificationsService.API.csproj @@ -26,12 +26,10 @@ - + - - diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index b72cc5b6b..9d0921d98 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -1,5 +1,5 @@ using Hangfire; -using HwProj.AuthService.API.Events; +using HwProj.Models.Events.AuthEvents; using HwProj.AuthService.Client; using HwProj.CoursesService.Client; using HwProj.EventBus.Client.Interfaces; @@ -8,7 +8,7 @@ using HwProj.NotificationsService.API.Models; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; -using HwProj.SolutionsService.API.Events; +using HwProj.Models.Events.SolutionEvents; using HwProj.Utils.Configuration; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; diff --git a/HwProj.SolutionsService/HwProj.SolutionsService.API/Services/SolutionsService.cs b/HwProj.SolutionsService/HwProj.SolutionsService.API/Services/SolutionsService.cs index c9f7d1603..92662c645 100644 --- a/HwProj.SolutionsService/HwProj.SolutionsService.API/Services/SolutionsService.cs +++ b/HwProj.SolutionsService/HwProj.SolutionsService.API/Services/SolutionsService.cs @@ -10,7 +10,7 @@ using HwProj.Models.CoursesService.ViewModels; using HwProj.Models.SolutionsService; using HwProj.Models.StatisticsService; -using HwProj.SolutionsService.API.Events; +using HwProj.Models.Events.SolutionEvents; using HwProj.SolutionsService.API.Repositories; using Microsoft.EntityFrameworkCore; From 70d9ec898475e7c7ad37a9379c592234e7a56b59 Mon Sep 17 00:00:00 2001 From: "Alexey.Berezhnykh" Date: Sun, 25 Feb 2024 23:37:59 +0300 Subject: [PATCH 21/22] wip --- Directory.Build.props | 2 +- .../Events/AuthEvents/AdminRegisterEvent.cs | 10 +++- .../Events/AuthEvents/InviteLecturerEvent.cs | 2 + .../AuthEvents/PasswordRecoveryEvent.cs | 2 + .../Events/AuthEvents/StudentRegisterEvent.cs | 11 +++- .../Events/CourseEvents/DeleteTaskEvent.cs | 4 +- .../LecturerAcceptToCourseEvent.cs | 3 + .../LecturerInvitedToCourseEvent.cs | 3 + .../LecturerRejectToCourseEvent.cs | 4 +- .../Events/CourseEvents/NewCourseMateEvent.cs | 5 +- .../Events/CourseEvents/NewHomeworkEvent.cs | 3 + .../Events/CourseEvents/NewTaskEvent.cs | 7 +-- .../CourseEvents/UpdateHomeworkEvent.cs | 3 + .../UpdateSolutionMaxRatingEvent.cs | 18 ------ .../Events/CourseEvents/UpdateTaskEvent.cs | 13 +++-- .../Events/SolutionEvents/RateEvent.cs | 3 + .../SolutionEvents/StudentPassTaskEvent.cs | 8 ++- .../Services/CoursesService.cs | 1 - .../HwProj.EventBus.Client/Event.cs | 22 +++++--- .../HwProj.EventBus.Tests/TestEvent.cs | 5 +- .../NotificationSettingsController.cs | 1 - .../EventHandlers/DeleteTaskEventHandler.cs | 3 +- .../InviteLecturerEventHandler.cs | 4 +- .../LecturerAcceptToCourseEventHandler.cs | 4 +- .../LecturerInvitedToCourseEventHandler.cs | 4 +- .../LecturerRejectToCourseEventHandler.cs | 4 +- .../EventHandlers/NewCourseMateHandler.cs | 4 +- .../EventHandlers/NewHomeworkEventHandler.cs | 6 +- .../NewHomeworkTaskEventHandler.cs | 24 ++++---- .../PasswordRecoveryEventHandler.cs | 8 +-- .../EventHandlers/RateEventHandler.cs | 4 +- .../EventHandlers/RegisterEventHandler.cs | 6 +- .../UpdateHomeworkEventHandler.cs | 4 +- .../EventHandlers/UpdateTaskEventHandler.cs | 10 ++-- .../EventHandlerExtensions.cs | 40 +++++++------ .../Jobs/ScheduleJob.cs | 24 ++++++++ .../Models/NotificationsContext.cs | 6 +- .../Models/ScheduleJob.cs | 48 ---------------- .../Repositories/ScheduleJobsRepository.cs | 56 ++++++++++--------- .../Startup.cs | 2 +- 40 files changed, 201 insertions(+), 190 deletions(-) delete mode 100644 HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs rename HwProj.NotificationsService/HwProj.NotificationsService.API/{EventHandlers => Jobs}/EventHandlerExtensions.cs (54%) create mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/ScheduleJob.cs delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs diff --git a/Directory.Build.props b/Directory.Build.props index 208b791ca..134b7ea71 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - preview + 8 diff --git a/HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs index 050f8844a..e13ee52f8 100644 --- a/HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/AdminRegisterEvent.cs @@ -1,11 +1,15 @@ -namespace HwProj.Models.Events.AuthEvents +using HwProj.EventBus.Client; + +namespace HwProj.Models.Events.AuthEvents { public class AdminRegisterEvent : RegisterEvent { - public AdminRegisterEvent(string userId, string email, string name, string surname = "", string middleName = "") + public AdminRegisterEvent(string userId, string email, string name, string surname = "", string middleName = "") : base(userId, email, name, surname, middleName) { - } + + public override string EventName => "AdminRegisterEvent"; + public override EventCategory Category => EventCategory.Users; } } diff --git a/HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs index 4982d1aad..dcc4a4b0a 100644 --- a/HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/InviteLecturerEvent.cs @@ -6,5 +6,7 @@ public class InviteLecturerEvent : Event { public string UserId { get; set; } public string UserEmail { get; set; } + public override string EventName => "InviteLecturerEvent"; + public override EventCategory Category => EventCategory.Users; } } diff --git a/HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs index dab4f1fcf..d394c7616 100644 --- a/HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/PasswordRecoveryEvent.cs @@ -9,5 +9,7 @@ public class PasswordRecoveryEvent : Event public string Surname { get; set; } public string Email { get; set; } public string Token { get; set; } + public override string EventName => "PasswordRecoveryEvent"; + public override EventCategory Category => EventCategory.Users; } } diff --git a/HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs b/HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs index cf3f2f438..acf4a6c0b 100644 --- a/HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/AuthEvents/StudentRegisterEvent.cs @@ -1,11 +1,16 @@ -namespace HwProj.Models.Events.AuthEvents +using HwProj.EventBus.Client; + +namespace HwProj.Models.Events.AuthEvents { public class StudentRegisterEvent : RegisterEvent { - public StudentRegisterEvent(string userId, string email, string name, string surname = "", string middleName = "") + public StudentRegisterEvent(string userId, string email, string name, string surname = "", + string middleName = "") : base(userId, email, name, surname, middleName) { - } + + public override string EventName => "StudentRegisterEvent"; + public override EventCategory Category => EventCategory.Users; } } diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs index 8272dedc7..102cc4353 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/DeleteTaskEvent.cs @@ -5,10 +5,12 @@ namespace HwProj.Models.Events.CourseEvents public class DeleteTaskEvent : Event { public long TaskId { get; set; } + public override string EventName => "DeleteTaskEvent"; + public override EventCategory Category => EventCategory.Tasks; public DeleteTaskEvent(long taskId) { TaskId = taskId; } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs index 9277351d4..6165affa6 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerAcceptToCourseEvent.cs @@ -8,5 +8,8 @@ public class LecturerAcceptToCourseEvent : Event public string CourseName { get; set; } public string MentorIds { get; set; } public string StudentId { get; set; } + + public override string EventName => "AcceptToCourseEvent"; + public override EventCategory Category => EventCategory.Courses; } } diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs index 65e8be08c..2908ef306 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerInvitedToCourseEvent.cs @@ -8,5 +8,8 @@ public class LecturerInvitedToCourseEvent : Event public string CourseName { get; set; } public string MentorId { get; set; } public string MentorEmail { get; set; } + + public override string EventName => "LecturerInvitedToCourseEvent"; + public override EventCategory Category => EventCategory.Courses; } } diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs index 516db5a3b..8db607e4f 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/LecturerRejectToCourseEvent.cs @@ -6,7 +6,9 @@ public class LecturerRejectToCourseEvent : Event { public long CourseId { get; set; } public string CourseName { get; set; } - public string MentorIds { get; set; } public string StudentId { get; set; } + + public override string EventName => "RejectToCourseEvent"; + public override EventCategory Category => EventCategory.Courses; } } diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs index 7ec5bd627..b33b6a135 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewCourseMateEvent.cs @@ -9,5 +9,8 @@ public class NewCourseMateEvent : Event public string MentorIds { get; set; } public string StudentId { get; set; } public bool IsAccepted { get; set; } + + public override string EventName => "NewCourseMateEvent"; + public override EventCategory Category => EventCategory.Courses; } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs index f88365221..f2e80f155 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewHomeworkEvent.cs @@ -8,6 +8,9 @@ public class NewHomeworkEvent : Event public string Homework { get; set; } public CourseDTO Course { get; set; } + public override string EventName => "NewHomeworkEvent"; + public override EventCategory Category => EventCategory.Homeworks; + public NewHomeworkEvent(string homework, CourseDTO course) { Homework = homework; diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs index d6e99b4df..d907b4e9e 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs @@ -1,4 +1,3 @@ -using System; using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; @@ -7,11 +6,11 @@ namespace HwProj.Models.Events.CourseEvents public class NewTaskEvent : Event { public long TaskId { get; set; } - public HomeworkTaskDTO Task { get; set; } - public CourseDTO Course { get; set; } + public override string EventName => "NewTaskEvent"; + public override EventCategory Category => EventCategory.Tasks; public NewTaskEvent(long taskId, HomeworkTaskDTO task, CourseDTO course) { @@ -20,4 +19,4 @@ public NewTaskEvent(long taskId, HomeworkTaskDTO task, CourseDTO course) Course = course; } } -} \ No newline at end of file +} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs index f05a180aa..e867a5e27 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateHomeworkEvent.cs @@ -8,6 +8,9 @@ public class UpdateHomeworkEvent : Event public HomeworkViewModel Homework { get; set; } public CourseDTO Course { get; set; } + public override string EventName => "UpdateHomeworkEvent"; + public override EventCategory Category => EventCategory.Homeworks; + public UpdateHomeworkEvent(HomeworkViewModel homework, CourseDTO course) { Homework = homework; diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs deleted file mode 100644 index 5ebaa2eed..000000000 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateSolutionMaxRatingEvent.cs +++ /dev/null @@ -1,18 +0,0 @@ -using HwProj.EventBus.Client; - -namespace HwProj.Models.Events.CourseEvents -{ - public class UpdateSolutionMaxRatingEvent : Event - { - public long TaskId { get; set; } - public long SolutionId { get; set; } - public int MaxRating { get; set; } - - public UpdateSolutionMaxRatingEvent(long taskId, long solutionId, int rating) - { - TaskId = taskId; - MaxRating = rating; - SolutionId = solutionId; - } - } -} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs index 8bfa55f32..f3731d15e 100644 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs @@ -1,4 +1,3 @@ -using System; using HwProj.EventBus.Client; using HwProj.Models.CoursesService.ViewModels; @@ -7,14 +6,16 @@ namespace HwProj.Models.Events.CourseEvents public class UpdateTaskEvent : Event { public long TaskId { get; set; } - + public HomeworkTaskDTO PreviousEvent { get; set; } - + public HomeworkTaskDTO NewEvent { get; set; } - + public CourseDTO Course { get; set; } - - + + public override string EventName => "UpdateTaskEvent"; + public override EventCategory Category => EventCategory.Tasks; + public UpdateTaskEvent(long taskId, HomeworkTaskDTO previousEvent, HomeworkTaskDTO newEvent, CourseDTO course) { TaskId = taskId; diff --git a/HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs b/HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs index 3fe70855f..ce041d443 100644 --- a/HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/SolutionEvents/RateEvent.cs @@ -9,6 +9,9 @@ public class RateEvent : Event public HomeworkTaskViewModel Task { get; set; } public SolutionViewModel Solution { get; set; } + public override string EventName => "RateEvent"; + public override EventCategory Category => EventCategory.Solutions; + public RateEvent(HomeworkTaskViewModel task, SolutionViewModel solution) { Task = task; diff --git a/HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs index 3708e12a5..438f1195a 100644 --- a/HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs +++ b/HwProj.Common/HwProj.Models/Events/SolutionEvents/StudentPassTaskEvent.cs @@ -12,11 +12,15 @@ public class StudentPassTaskEvent : Event public AccountDataDto Student { get; set; } public HomeworkTaskViewModel Task { get; set; } - public StudentPassTaskEvent(CourseDTO course, SolutionViewModel solution, AccountDataDto student, HomeworkTaskViewModel task) + public override string EventName => "StudentPassTaskEvent"; + public override EventCategory Category => EventCategory.Solutions; + + public StudentPassTaskEvent(CourseDTO course, SolutionViewModel solution, AccountDataDto student, + HomeworkTaskViewModel task) { Course = course; Solution = solution; - Student= student; + Student = student; Task = task; } } diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs index cd91cf1fc..dd969a283 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/CoursesService.cs @@ -189,7 +189,6 @@ public async Task RejectCourseMateAsync(long courseId, string studentId) { CourseId = courseId, CourseName = course.Name, - MentorIds = course.MentorIds, StudentId = studentId }); return true; diff --git a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs index 8ba45c4b7..0da3278a4 100644 --- a/HwProj.EventBus/HwProj.EventBus.Client/Event.cs +++ b/HwProj.EventBus/HwProj.EventBus.Client/Event.cs @@ -3,21 +3,29 @@ namespace HwProj.EventBus.Client { - public class Event + public enum EventCategory { - [JsonProperty] - public Guid Id { get; set; } + Users, + Courses, + Homeworks, + Tasks, + Solutions + } - [JsonProperty] - public DateTime CreationData { get; set; } + public abstract class Event + { + [JsonProperty] public Guid Id { get; set; } + [JsonProperty] public DateTime CreationData { get; set; } + public abstract string EventName { get; } + public abstract EventCategory Category { get; } - public Event() + protected Event() { Id = Guid.NewGuid(); CreationData = DateTime.UtcNow; } - public Event(Guid id, DateTime data) + protected Event(Guid id, DateTime data) { Id = id; CreationData = data; diff --git a/HwProj.EventBus/HwProj.EventBus.Tests/TestEvent.cs b/HwProj.EventBus/HwProj.EventBus.Tests/TestEvent.cs index 62acb15b3..7420174e7 100644 --- a/HwProj.EventBus/HwProj.EventBus.Tests/TestEvent.cs +++ b/HwProj.EventBus/HwProj.EventBus.Tests/TestEvent.cs @@ -8,10 +8,13 @@ public class TestEvent : Event public int NewPrice { get; set; } - public TestEvent(int newPrice, int oldPrice) + public TestEvent(int newPrice, int oldPrice) { OldPrice = oldPrice; NewPrice = newPrice; } + + public override string EventName => "TestEvent"; + public override EventCategory Category => EventCategory.Courses; } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Controllers/NotificationSettingsController.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Controllers/NotificationSettingsController.cs index e916bb5fa..b2e93f502 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Controllers/NotificationSettingsController.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Controllers/NotificationSettingsController.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using HwProj.Models.NotificationsService; -using HwProj.NotificationsService.API.Models; using HwProj.NotificationsService.API.Repositories; using Microsoft.AspNetCore.Mvc; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs index 57ce5ff3a..db8ffa80a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs @@ -1,9 +1,8 @@ using System.Threading.Tasks; -using Hangfire; using HwProj.EventBus.Client.Interfaces; using HwProj.Models.Events.CourseEvents; +using HwProj.NotificationsService.API.Jobs; using HwProj.NotificationsService.API.Repositories; -using Swashbuckle.AspNetCore.SwaggerGen; namespace HwProj.NotificationsService.API.EventHandlers { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs index 9a08f994c..fadfe6144 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/InviteLecturerEventHandler.cs @@ -1,7 +1,7 @@ +using System; using System.Threading.Tasks; using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -26,7 +26,7 @@ public override async Task HandleAsync(InviteLecturerEvent @event) Sender = "AuthService", Body = "Вас добавили в список лекторов.", Category = CategoryState.Courses, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, Owner = @event.UserId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs index bd78f3e39..f2e827d26 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerAcceptToCourseEventHandler.cs @@ -1,7 +1,7 @@ +using System; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -37,7 +37,7 @@ public override async Task HandleAsync(LecturerAcceptToCourseEvent @event) Body = $"Вас приняли на курс {@event.CourseName}.", Category = CategoryState.Courses, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, Owner = @event.StudentId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs index d8ead54a8..0a181fe8e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerInvitedToCourseEventHandler.cs @@ -1,6 +1,6 @@ +using System; using System.Threading.Tasks; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -33,7 +33,7 @@ public override async Task HandleAsync(LecturerInvitedToCourseEvent @event) Body = $"Вас пригласили в качестве преподавателя на курс {@event.CourseName}.", Category = CategoryState.Courses, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, Owner = @event.MentorId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs index ba47a8143..f7098e474 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/LecturerRejectToCourseEventHandler.cs @@ -1,7 +1,7 @@ +using System; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -37,7 +37,7 @@ public override async Task HandleAsync(LecturerRejectToCourseEvent @event) Body = $"Вас не приняли на курс {@event.CourseName}.", Category = CategoryState.Courses, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, Owner = @event.StudentId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs index 8580bdbc6..ddefa9283 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewCourseMateHandler.cs @@ -1,7 +1,7 @@ +using System; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -44,7 +44,7 @@ public override async Task HandleAsync(NewCourseMateEvent @event) $"Студент {user.Name} {user.Surname}" + $" подал заявку на вступление в курс {@event.CourseName}.", Category = CategoryState.Courses, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, Owner = m }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs index cec6508f3..17351de68 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkEventHandler.cs @@ -1,10 +1,10 @@ +using System; using System.Linq; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; -using HwProj.Models; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; using HwProj.Models.Events.CourseEvents; @@ -44,9 +44,9 @@ public override async Task HandleAsync(NewHomeworkEvent @event) $"В курсе {@event.Course.Name}" + $" опубликована новая домашняя работа {@event.Homework}.", Category = CategoryState.Homeworks, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, - Owner = student!.UserId + Owner = student.UserId }; var addNotificationTask = _notificationRepository.AddAsync(notification); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs index cf5fee558..278fc526d 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs @@ -1,14 +1,12 @@ using System; using System.Linq; using System.Threading.Tasks; -using Hangfire; using HwProj.AuthService.Client; using HwProj.CoursesService.Client; -using HwProj.EventBus.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.Events.CourseEvents; using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Jobs; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; @@ -42,26 +40,30 @@ public NewHomeworkTaskEventHandler( public override async Task HandleAsync(NewTaskEvent @event) { - if (@event.Task.PublicationDate <= DateTimeUtils.GetMoscowNow()) + if (@event.Task.PublicationDate <= DateTime.Now) { await AddNotificationsAsync(@event); return; } - await EventHandlerExtensions.AddScheduleJobAsync(@event, @event.TaskId, + await EventHandlerExtensions.AddScheduleJobAsync( + @event, + @event.TaskId, @event.Task.PublicationDate, - () => AddNotificationsAsync(@event), _scheduleJobsRepository); + () => AddNotificationsAsync(@event), + _scheduleJobsRepository + ); } - + // ReSharper disable once MemberCanBePrivate.Global public async Task AddNotificationsAsync(NewTaskEvent @event) { var course = await _coursesServiceClient.GetCourseById(@event.Course.Id); if (course == null) return; - + var studentIds = course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); - + var url = _configuration["Url"]; var message = $"В курсе {course.Name}" + $" опубликована новая задача {@event.Task.Title}." + @@ -74,8 +76,8 @@ public async Task AddNotificationsAsync(NewTaskEvent @event) Sender = "CourseService", Body = message, Category = CategoryState.Homeworks, - Date = DateTimeUtils.GetMoscowNow(), - Owner = student!.UserId + Date = DateTime.UtcNow, + Owner = student.UserId }; var addNotificationTask = _notificationRepository.AddAsync(notification); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs index 7e748ba51..b32c5cf93 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/PasswordRecoveryEventHandler.cs @@ -1,8 +1,8 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using System.Web; using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -36,7 +36,7 @@ public override async Task HandleAsync(PasswordRecoveryEvent @event) $"Для изменения пароля перейдите по ссылке
Сменить пароль

" + $"Если вы не запрашивали сброс пароля, проигнорируйте это письмо.", Category = CategoryState.Profile, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, Owner = @event.UserId }; @@ -45,7 +45,7 @@ public override async Task HandleAsync(PasswordRecoveryEvent @event) Sender = "AuthService", Body = $"{@event.Name} {@event.Surname}, был запрошен сброс вашего пароля.", Category = CategoryState.Profile, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, Owner = @event.UserId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs index d9b979278..638403618 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RateEventHandler.cs @@ -1,7 +1,7 @@ +using System; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -44,7 +44,7 @@ public override async Task HandleAsync(RateEvent @event) $"{@event.Solution.Rating}/{@event.Task.MaxRating}." + $"{commentBody}", Category = CategoryState.Homeworks, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, Owner = @event.Solution.StudentId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs index fe42b0d47..80bdf138e 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/RegisterEventHandler.cs @@ -1,7 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using HwProj.Models.Events.AuthEvents; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; @@ -26,7 +26,7 @@ public override async Task HandleAsync(StudentRegisterEvent @event) Sender = "AuthService", Body = $"{@event.Name} {@event.Surname}, Добро Пожаловать в HwProj2.", Category = CategoryState.Profile, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, Owner = @event.UserId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs index f62cb8ddc..2f1496225 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateHomeworkEventHandler.cs @@ -1,9 +1,9 @@ +using System; using System.Threading.Tasks; using HwProj.AuthService.Client; using HwProj.EventBus.Client.Interfaces; using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; -using HwProj.Models; using HwProj.Models.Events.CourseEvents; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; @@ -40,7 +40,7 @@ public override async Task HandleAsync(UpdateHomeworkEvent @event) Body = $"В курсе {@event.Course.Name} домашнее задание {@event.Homework.Title} обновлено.", Category = CategoryState.Homeworks, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, HasSeen = false, Owner = student.StudentId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs index d3ba5cc6a..c8e2bed1f 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs @@ -2,14 +2,12 @@ using System.Linq; using System.Threading.Tasks; using AutoMapper; -using Hangfire; using HwProj.AuthService.Client; using HwProj.CoursesService.Client; using HwProj.EventBus.Client.Interfaces; -using HwProj.Models; -using HwProj.Models.AuthService.DTO; using HwProj.Models.Events.CourseEvents; using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Jobs; using HwProj.NotificationsService.API.Repositories; using HwProj.NotificationsService.API.Services; using Microsoft.Extensions.Configuration; @@ -46,7 +44,7 @@ public UpdateTaskEventHandler( public override async Task HandleAsync(UpdateTaskEvent @event) { - if (@event.PreviousEvent.PublicationDate <= DateTimeUtils.GetMoscowNow()) + if (@event.PreviousEvent.PublicationDate <= DateTime.UtcNow) { await AddNotificationsAsync(@event); return; @@ -66,7 +64,7 @@ public async Task AddNotificationsAsync(UpdateTaskEvent @event) var accountsData = await _authServiceClient.GetAccountsData(studentIds); var url = _configuration["Url"]; - var isTaskPublished = @event.PreviousEvent.PublicationDate < DateTimeUtils.GetMoscowNow(); + var isTaskPublished = @event.PreviousEvent.PublicationDate < DateTime.UtcNow; var message = isTaskPublished ? $"Задача {@event.PreviousEvent.Title}" + $" из курса {course.Name} обновлена." @@ -81,7 +79,7 @@ public async Task AddNotificationsAsync(UpdateTaskEvent @event) Sender = "CourseService", Body = message, Category = CategoryState.Homeworks, - Date = DateTimeUtils.GetMoscowNow(), + Date = DateTime.UtcNow, Owner = student!.UserId }; diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs similarity index 54% rename from HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs rename to HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs index 562a10ae6..c0b02d109 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/EventHandlerExtensions.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs @@ -3,10 +3,9 @@ using System.Threading.Tasks; using Hangfire; using HwProj.EventBus.Client; -using HwProj.Models.NotificationsService; using HwProj.NotificationsService.API.Repositories; -namespace HwProj.NotificationsService.API.EventHandlers +namespace HwProj.NotificationsService.API.Jobs { public static class EventHandlerExtensions where TEvent : Event { @@ -14,35 +13,40 @@ public static async Task AddScheduleJobAsync(TEvent @event, long itemId, DateTim Expression> jobFunc, IScheduleJobsRepository jobsRepository) { var jobId = BackgroundJob.Schedule(jobFunc, publicationDate); - var scheduleJob = new ScheduleJob(@event, itemId, jobId); - BackgroundJob.ContinueJobWith(jobId, () => jobsRepository.DeleteAsync(@event, itemId), JobContinuationOptions.OnAnyFinishedState); + if (jobId == null) + throw new InvalidOperationException($"Невозможно создать отложенное событие для {@event.EventName}"); + var scheduleJob = new ScheduleJob(@event, itemId, jobId); await jobsRepository.AddAsync(scheduleJob); - } + BackgroundJob.ContinueJobWith( + jobId, + () => jobsRepository.DeleteAsync(new[] { scheduleJob }), + JobContinuationOptions.OnAnyFinishedState + ); + } public static async Task UpdateScheduleJobAsync(TEvent @event, long itemId, DateTime publicationDate, Expression> jobFunc, IScheduleJobsRepository jobsRepository) { - await DeleteScheduleJobsAsync(@event, itemId, jobsRepository); + var scheduleJob = await jobsRepository.GetAsync(@event.Category, @event.EventName, itemId); + if (scheduleJob == null) return; + + BackgroundJob.Delete(scheduleJob.JobId); + await jobsRepository.DeleteAsync(new[] { scheduleJob }); + await AddScheduleJobAsync(@event, itemId, publicationDate, jobFunc, jobsRepository); } - public static async Task DeleteScheduleJobsAsync(TEvent @event, long itemId, IScheduleJobsRepository jobsRepository) { - var category = ScheduleJobIdHelper.GetCategory(@event); - var scheduleJobs = jobsRepository.FindAll(scheduleJob => - scheduleJob.Category.Equals(category) && scheduleJob.ItemId == itemId); - - foreach (var scheduleJob in scheduleJobs) - { - BackgroundJob.Delete(scheduleJob.JobId); - } - - await jobsRepository.DeleteAllInCategoryByItemIdAsync(@event, itemId); + var scheduleJobs = await jobsRepository.FindAllInCategoryAsync(@event.Category, itemId); + + foreach (var scheduleJob in scheduleJobs) BackgroundJob.Delete(scheduleJob.JobId); + + await jobsRepository.DeleteAsync(scheduleJobs); } } -} \ No newline at end of file +} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/ScheduleJob.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/ScheduleJob.cs new file mode 100644 index 000000000..13926fbf3 --- /dev/null +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/ScheduleJob.cs @@ -0,0 +1,24 @@ +using HwProj.EventBus.Client; + +namespace HwProj.NotificationsService.API.Jobs +{ + public class ScheduleJob + { + public EventCategory Category { get; set; } + public string EventName { get; set; } + public long ItemId { get; set; } + public string JobId { get; set; } + + public ScheduleJob(Event @event, long itemId, string jobId) + { + Category = @event.Category; + EventName = @event.EventName; + ItemId = itemId; + JobId = jobId; + } + + public ScheduleJob() + { + } + } +} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs index 68170c549..bf34110d8 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/NotificationsContext.cs @@ -1,5 +1,5 @@ -using HwProj.EventBus.Client; -using HwProj.Models.NotificationsService; +using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Jobs; using Microsoft.EntityFrameworkCore; namespace HwProj.NotificationsService.API.Models @@ -19,7 +19,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasIndex(n => n.UserId); modelBuilder.Entity().HasKey(n => new { n.UserId, n.Category }); - modelBuilder.Entity().HasKey(s => new {s.Category, s.EventName, s.ItemId}); + modelBuilder.Entity().HasKey(s => new { s.Category, s.EventName, s.ItemId }); } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs deleted file mode 100644 index 9629b9080..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Models/ScheduleJob.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System; -using HwProj.EventBus.Client; -using HwProj.Models.Events.CourseEvents; - -namespace HwProj.Models.NotificationsService -{ - public class ScheduleJob - { - public string Category { get; set; } - - public string EventName { get; set; } - - public long ItemId { get; set; } - - public string JobId { get; set; } - - public ScheduleJob(Event @event, long itemId, string jobId) - { - Category = ScheduleJobIdHelper.GetCategory(@event); - EventName = ScheduleJobIdHelper.GetEventName(@event); - ItemId = itemId; - JobId = jobId; - } - - public ScheduleJob(){} - } - - - public static class ScheduleJobIdHelper - { - public static string GetCategory(Event @event) - { - var eventType = @event.GetType(); - return @event.GetType() switch - { - _ when eventType == typeof(NewTaskEvent) || eventType == typeof(UpdateTaskEvent) || - eventType == typeof(DeleteTaskEvent) - => "Task", - _ when eventType == typeof(NewHomeworkEvent) - => "Homework", - _ => "Unknown" - }; - } - - public static string GetEventName(Event @event) - => @event.ToString(); - } -} \ No newline at end of file diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs index 17a935c9e..f1cbe9e43 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleJobsRepository.cs @@ -1,10 +1,7 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Linq.Expressions; +using System.Linq; using System.Threading.Tasks; using HwProj.EventBus.Client; -using HwProj.Models.NotificationsService; +using HwProj.NotificationsService.API.Jobs; using HwProj.NotificationsService.API.Models; using Microsoft.EntityFrameworkCore; using Z.EntityFramework.Plus; @@ -14,12 +11,9 @@ namespace HwProj.NotificationsService.API.Repositories public interface IScheduleJobsRepository { public Task AddAsync(ScheduleJob scheduleJob); - - public Task DeleteAsync(Event @event, long itemId); - - public Task DeleteAllInCategoryByItemIdAsync(Event @event, long itemId); - - public IEnumerable FindAll(Expression> predicate); + public Task GetAsync(EventCategory category, string eventName, long itemId); + public Task DeleteAsync(ScheduleJob[] jobs); + public Task FindAllInCategoryAsync(EventCategory category, long itemId); } @@ -34,36 +28,44 @@ public ScheduleJobsRepository(NotificationsContext context) public async Task AddAsync(ScheduleJob scheduleJob) { - await _context.AddAsync(scheduleJob).ConfigureAwait(false); - await _context.SaveChangesAsync().ConfigureAwait(false); + await _context.AddAsync(scheduleJob); + await _context.SaveChangesAsync(); + } + + public async Task GetAsync(EventCategory category, string eventName, long itemId) + { + return await _context.Set().FindAsync(category, eventName, itemId); } public async Task DeleteAsync(Event @event, long itemId) { - var category = ScheduleJobIdHelper.GetCategory(@event); - var eventName = ScheduleJobIdHelper.GetEventName(@event); - await _context.Set() .Where(scheduleJob => - scheduleJob.Category.Equals(category) && scheduleJob.EventName.Equals(eventName) && + scheduleJob.Category == @event.Category && + scheduleJob.EventName == @event.EventName && scheduleJob.ItemId == itemId) - .DeleteAsync() - .ConfigureAwait(false); + .DeleteAsync(); + } + + public async Task DeleteAsync(ScheduleJob[] jobs) + { + _context.Set().RemoveRange(jobs); + await _context.SaveChangesAsync(); } public async Task DeleteAllInCategoryByItemIdAsync(Event @event, long itemId) { - var category = ScheduleJobIdHelper.GetCategory(@event); - await _context.Set() - .Where(scheduleJob => scheduleJob.Category.Equals(category) && scheduleJob.ItemId == itemId) - .DeleteAsync() - .ConfigureAwait(true); + .Where(scheduleJob => scheduleJob.Category == @event.Category && scheduleJob.ItemId == itemId) + .DeleteAsync(); } - public IEnumerable FindAll(Expression> predicate) + public async Task FindAllInCategoryAsync(EventCategory category, long itemId) { - return _context.Set().AsNoTracking().Where(predicate); + return await _context.Set() + .Where(scheduleJob => scheduleJob.Category == category && scheduleJob.ItemId == itemId) + .AsNoTracking() + .ToArrayAsync(); } } -} \ No newline at end of file +} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index 9d0921d98..707a94461 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -43,8 +43,8 @@ public void ConfigureServices(IServiceCollection services) services.AddDbContext(options => options.UseSqlServer(connectionString)); services.AddScoped(); services.AddScoped(); - services.AddScoped(); services.AddEventBus(Configuration); + services.AddTransient(); services.AddTransient, RegisterEventHandler>(); services.AddTransient, RateEventHandler>(); services.AddTransient, StudentPassTaskEventHandler>(); From dac458252819108105b229611300a201acfce7c3 Mon Sep 17 00:00:00 2001 From: BakhorikovEgor Date: Wed, 6 Mar 2024 23:29:12 +0300 Subject: [PATCH 22/22] feat: change update extension to rescheduling + add lazy addorupdateEvent --- .../Events/CourseEvents/AddOrUpdateEvent.cs | 19 ++++ .../Events/CourseEvents/NewTaskEvent.cs | 22 ----- .../Events/CourseEvents/UpdateTaskEvent.cs | 27 ------ .../Services/TasksService.cs | 4 +- ...dler.cs => AddOrUpdateTaskEventHandler.cs} | 45 ++++++---- .../NewHomeworkTaskEventHandler.cs | 90 ------------------- .../Jobs/EventHandlerExtensions.cs | 18 ++-- .../Startup.cs | 6 +- 8 files changed, 58 insertions(+), 173 deletions(-) create mode 100644 HwProj.Common/HwProj.Models/Events/CourseEvents/AddOrUpdateEvent.cs delete mode 100644 HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs delete mode 100644 HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs rename HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/{UpdateTaskEventHandler.cs => AddOrUpdateTaskEventHandler.cs} (64%) delete mode 100644 HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/AddOrUpdateEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/AddOrUpdateEvent.cs new file mode 100644 index 000000000..71dc62940 --- /dev/null +++ b/HwProj.Common/HwProj.Models/Events/CourseEvents/AddOrUpdateEvent.cs @@ -0,0 +1,19 @@ +using HwProj.EventBus.Client; +using HwProj.Models.CoursesService.ViewModels; + +namespace HwProj.Models.Events.CourseEvents +{ + public class AddOrUpdateTaskEvent : Event + { + public bool IsUpdate { get; set; } + public long TaskId { get; set; } + public override string EventName => "UpdateTaskEvent"; + public override EventCategory Category => EventCategory.Tasks; + + public AddOrUpdateTaskEvent(long taskId, bool isUpdate) + { + TaskId = taskId; + IsUpdate = isUpdate; + } + } +} \ No newline at end of file diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs deleted file mode 100644 index d907b4e9e..000000000 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/NewTaskEvent.cs +++ /dev/null @@ -1,22 +0,0 @@ -using HwProj.EventBus.Client; -using HwProj.Models.CoursesService.ViewModels; - -namespace HwProj.Models.Events.CourseEvents -{ - public class NewTaskEvent : Event - { - public long TaskId { get; set; } - public HomeworkTaskDTO Task { get; set; } - public CourseDTO Course { get; set; } - - public override string EventName => "NewTaskEvent"; - public override EventCategory Category => EventCategory.Tasks; - - public NewTaskEvent(long taskId, HomeworkTaskDTO task, CourseDTO course) - { - TaskId = taskId; - Task = task; - Course = course; - } - } -} diff --git a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs b/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs deleted file mode 100644 index f3731d15e..000000000 --- a/HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs +++ /dev/null @@ -1,27 +0,0 @@ -using HwProj.EventBus.Client; -using HwProj.Models.CoursesService.ViewModels; - -namespace HwProj.Models.Events.CourseEvents -{ - public class UpdateTaskEvent : Event - { - public long TaskId { get; set; } - - public HomeworkTaskDTO PreviousEvent { get; set; } - - public HomeworkTaskDTO NewEvent { get; set; } - - public CourseDTO Course { get; set; } - - public override string EventName => "UpdateTaskEvent"; - public override EventCategory Category => EventCategory.Tasks; - - public UpdateTaskEvent(long taskId, HomeworkTaskDTO previousEvent, HomeworkTaskDTO newEvent, CourseDTO course) - { - TaskId = taskId; - PreviousEvent = previousEvent; - NewEvent = newEvent; - Course = course; - } - } -} diff --git a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs index 6a02c3274..20071deaa 100644 --- a/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs +++ b/HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs @@ -44,7 +44,7 @@ public async Task AddTaskAsync(long homeworkId, HomeworkTask task) var taskModel = _mapper.Map(task); var taskId = await _tasksRepository.AddAsync(task); - _eventBus.Publish(new NewTaskEvent(taskId, taskModel, courseModel)); + _eventBus.Publish(new AddOrUpdateTaskEvent(taskId, false)); return taskId; } @@ -64,7 +64,7 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update) var previousTaskModel = _mapper.Map(task); var newTaskModel = _mapper.Map(update); - _eventBus.Publish(new UpdateTaskEvent(taskId, previousTaskModel, newTaskModel, courseModel)); + _eventBus.Publish(new AddOrUpdateTaskEvent(taskId, true)); await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask() { diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/AddOrUpdateTaskEventHandler.cs similarity index 64% rename from HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs rename to HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/AddOrUpdateTaskEventHandler.cs index c8e2bed1f..1c404336a 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/AddOrUpdateTaskEventHandler.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.Tracing; using System.Linq; using System.Threading.Tasks; using AutoMapper; @@ -14,7 +15,7 @@ namespace HwProj.NotificationsService.API.EventHandlers { - public class UpdateTaskEventHandler : EventHandlerBase + public class AddOrUpdateTaskEventHandler : EventHandlerBase { private readonly ICoursesServiceClient _coursesServiceClient; private readonly INotificationsRepository _notificationRepository; @@ -24,7 +25,7 @@ public class UpdateTaskEventHandler : EventHandlerBase private readonly IEmailService _emailService; private readonly IScheduleJobsRepository _scheduleJobsRepository; - public UpdateTaskEventHandler( + public AddOrUpdateTaskEventHandler( ICoursesServiceClient coursesServiceClient, INotificationsRepository notificationRepository, IMapper mapper, @@ -42,35 +43,43 @@ public UpdateTaskEventHandler( _scheduleJobsRepository = scheduleJobsRepository; } - public override async Task HandleAsync(UpdateTaskEvent @event) + public override async Task HandleAsync(AddOrUpdateTaskEvent @event) { - if (@event.PreviousEvent.PublicationDate <= DateTime.UtcNow) + //TODO : ленивость не работает + var task = await _coursesServiceClient.GetTask(@event.TaskId); + + if (task.PublicationDate <= DateTime.UtcNow) { await AddNotificationsAsync(@event); - return; } - - await EventHandlerExtensions.UpdateScheduleJobAsync(@event, @event.TaskId, - @event.NewEvent.PublicationDate, - () => AddNotificationsAsync(@event), _scheduleJobsRepository); + else if (@event.IsUpdate) + { + await EventHandlerExtensions.UpdateScheduleJobAsync(@event, @event.TaskId, + task.PublicationDate, () => AddNotificationsAsync(@event), _scheduleJobsRepository); + } + else + { + await EventHandlerExtensions.AddScheduleJobAsync(@event, @event.TaskId, + task.PublicationDate, () => AddNotificationsAsync(@event), _scheduleJobsRepository); + } } - public async Task AddNotificationsAsync(UpdateTaskEvent @event) + public async Task AddNotificationsAsync(AddOrUpdateTaskEvent @event) { - var course = await _coursesServiceClient.GetCourseById(@event.Course.Id); + var course = await _coursesServiceClient.GetCourseByTask(@event.TaskId); + var task = await _coursesServiceClient.GetTask(@event.TaskId); if (course == null) return; var studentIds = course.CourseMates.Select(t => t.StudentId).ToArray(); var accountsData = await _authServiceClient.GetAccountsData(studentIds); var url = _configuration["Url"]; - var isTaskPublished = @event.PreviousEvent.PublicationDate < DateTime.UtcNow; - var message = isTaskPublished - ? $"Задача {@event.PreviousEvent.Title}" + - $" из курса {course.Name} обновлена." + var message = task.PublicationDate < DateTime.UtcNow + ? $"Задача {task.Title}" + + $" из курса {course.Name} обновлена." : $"В курсе {course.Name}" + - $" опубликована новая задача {@event.NewEvent.Title}." + - (@event.NewEvent.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); + $" опубликована новая задача {task.Title}." + + (task.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); foreach (var student in accountsData) { @@ -80,7 +89,7 @@ public async Task AddNotificationsAsync(UpdateTaskEvent @event) Body = message, Category = CategoryState.Homeworks, Date = DateTime.UtcNow, - Owner = student!.UserId + Owner = student.UserId }; var addNotificationTask = _notificationRepository.AddAsync(notification); diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs deleted file mode 100644 index 278fc526d..000000000 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Linq; -using System.Threading.Tasks; -using HwProj.AuthService.Client; -using HwProj.CoursesService.Client; -using HwProj.EventBus.Client.Interfaces; -using HwProj.Models.Events.CourseEvents; -using HwProj.Models.NotificationsService; -using HwProj.NotificationsService.API.Jobs; -using HwProj.NotificationsService.API.Repositories; -using HwProj.NotificationsService.API.Services; -using Microsoft.Extensions.Configuration; - -namespace HwProj.NotificationsService.API.EventHandlers -{ - public class NewHomeworkTaskEventHandler : EventHandlerBase - { - private readonly ICoursesServiceClient _coursesServiceClient; - private readonly INotificationsRepository _notificationRepository; - private readonly IScheduleJobsRepository _scheduleJobsRepository; - private readonly IAuthServiceClient _authServiceClient; - private readonly IConfigurationSection _configuration; - private readonly IEmailService _emailService; - - public NewHomeworkTaskEventHandler( - ICoursesServiceClient coursesServiceClient, - INotificationsRepository notificationRepository, - IScheduleJobsRepository scheduleJobsRepository, - IAuthServiceClient authServiceClient, - IConfiguration configuration, - IEmailService emailService) - { - _coursesServiceClient = coursesServiceClient; - _notificationRepository = notificationRepository; - _scheduleJobsRepository = scheduleJobsRepository; - _authServiceClient = authServiceClient; - _emailService = emailService; - _configuration = configuration.GetSection("Notification"); - } - - public override async Task HandleAsync(NewTaskEvent @event) - { - if (@event.Task.PublicationDate <= DateTime.Now) - { - await AddNotificationsAsync(@event); - return; - } - - await EventHandlerExtensions.AddScheduleJobAsync( - @event, - @event.TaskId, - @event.Task.PublicationDate, - () => AddNotificationsAsync(@event), - _scheduleJobsRepository - ); - } - - // ReSharper disable once MemberCanBePrivate.Global - public async Task AddNotificationsAsync(NewTaskEvent @event) - { - var course = await _coursesServiceClient.GetCourseById(@event.Course.Id); - if (course == null) return; - - var studentIds = course.CourseMates.Select(t => t.StudentId).ToArray(); - var accountsData = await _authServiceClient.GetAccountsData(studentIds); - - var url = _configuration["Url"]; - var message = $"В курсе {course.Name}" + - $" опубликована новая задача {@event.Task.Title}." + - (@event.Task.DeadlineDate is { } deadline ? $"\n\nДедлайн: {deadline:U}" : ""); - - foreach (var student in accountsData) - { - var notification = new Notification - { - Sender = "CourseService", - Body = message, - Category = CategoryState.Homeworks, - Date = DateTime.UtcNow, - Owner = student.UserId - }; - - var addNotificationTask = _notificationRepository.AddAsync(notification); - var sendEmailTask = _emailService.SendEmailAsync(notification, student.Email, "Новая задача"); - - await Task.WhenAll(addNotificationTask, sendEmailTask); - } - } - } -} diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs index c0b02d109..d17b471ab 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Jobs/EventHandlerExtensions.cs @@ -4,6 +4,7 @@ using Hangfire; using HwProj.EventBus.Client; using HwProj.NotificationsService.API.Repositories; +using Microsoft.Azure.KeyVault.Models; namespace HwProj.NotificationsService.API.Jobs { @@ -16,10 +17,9 @@ public static async Task AddScheduleJobAsync(TEvent @event, long itemId, DateTim if (jobId == null) throw new InvalidOperationException($"Невозможно создать отложенное событие для {@event.EventName}"); - + var scheduleJob = new ScheduleJob(@event, itemId, jobId); await jobsRepository.AddAsync(scheduleJob); - BackgroundJob.ContinueJobWith( jobId, () => jobsRepository.DeleteAsync(new[] { scheduleJob }), @@ -32,11 +32,8 @@ public static async Task UpdateScheduleJobAsync(TEvent @event, long itemId, Date { var scheduleJob = await jobsRepository.GetAsync(@event.Category, @event.EventName, itemId); if (scheduleJob == null) return; - - BackgroundJob.Delete(scheduleJob.JobId); - await jobsRepository.DeleteAsync(new[] { scheduleJob }); - - await AddScheduleJobAsync(@event, itemId, publicationDate, jobFunc, jobsRepository); + + BackgroundJob.Reschedule(scheduleJob.JobId, publicationDate); } public static async Task DeleteScheduleJobsAsync(TEvent @event, long itemId, @@ -44,9 +41,10 @@ public static async Task DeleteScheduleJobsAsync(TEvent @event, long itemId, { var scheduleJobs = await jobsRepository.FindAllInCategoryAsync(@event.Category, itemId); - foreach (var scheduleJob in scheduleJobs) BackgroundJob.Delete(scheduleJob.JobId); - - await jobsRepository.DeleteAsync(scheduleJobs); + foreach (var scheduleJob in scheduleJobs) + { + BackgroundJob.Delete(scheduleJob.JobId); + } } } } diff --git a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs index 707a94461..5db040209 100644 --- a/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs +++ b/HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs @@ -49,13 +49,12 @@ public void ConfigureServices(IServiceCollection services) services.AddTransient, RateEventHandler>(); services.AddTransient, StudentPassTaskEventHandler>(); services.AddTransient, UpdateHomeworkEventHandler>(); - services.AddTransient, UpdateTaskEventHandler>(); + services.AddTransient, AddOrUpdateTaskEventHandler>(); services.AddTransient, DeleteTaskEventHandler>(); services.AddTransient, LecturerAcceptToCourseEventHandler>(); services.AddTransient, LecturerRejectToCourseEventHandler>(); services.AddTransient, LecturerInvitedToCourseEventHandler>(); services.AddTransient, NewHomeworkEventHandler>(); - services.AddTransient, NewHomeworkTaskEventHandler>(); services.AddTransient, InviteLecturerEventHandler>(); services.AddTransient, NewCourseMateHandler>(); services.AddTransient, PasswordRecoveryEventHandler>(); @@ -77,13 +76,12 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); - eventBustSubscriber.Subscribe(); + eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); - eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe(); eventBustSubscriber.Subscribe();