Skip to content

Commit 76fd0e2

Browse files
feat: add delete update task events and eventhandlers are written
1 parent fa4b2ff commit 76fd0e2

File tree

9 files changed

+65
-37
lines changed

9 files changed

+65
-37
lines changed

HwProj.Common/HwProj.Models/Events/CourseEvents/UpdateTaskEvent.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ public class UpdateTaskEvent : Event
1212

1313
public long TaskId { get; set; }
1414

15+
//закладываюсь, что дату публикации не будут менять после публикации
16+
public DateTime PreviousPublicationDate { get; set; }
17+
1518
public DateTime PublicationDate { get; set; }
1619

17-
public UpdateTaskEvent(CourseDTO course, string taskTitle, long taskId, DateTime publicationDate)
20+
public DateTime? Deadline { get; set; }
21+
22+
public UpdateTaskEvent(CourseDTO course, string taskTitle, long taskId, DateTime previousPublicationDate, DateTime publicationDate, DateTime? deadline)
1823
{
1924
Course = course;
2025
TaskTitle = taskTitle;
2126
TaskId = taskId;
27+
PreviousPublicationDate = previousPublicationDate;
2228
PublicationDate = publicationDate;
29+
Deadline = deadline;
2330
}
2431
}
2532
}

HwProj.Common/HwProj.Models/NotificationsService/ScheduleWork.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static string Build(Event @event, long id)
2323
_ when eventType == typeof(NewTaskEvent) || eventType == typeof(UpdateTaskEvent) ||
2424
eventType == typeof(DeleteTaskEvent)
2525
=> "Task",
26-
_ when eventType == typeof(NewHomeworkEvent)
26+
_ when eventType == typeof(NewHomeworkEvent)
2727
=> "Homework",
2828
_ => "Unknown"
2929
};

HwProj.CoursesService/HwProj.CoursesService.API/Services/TasksService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ public async Task UpdateTaskAsync(long taskId, HomeworkTask update)
7070
var course = await _coursesRepository.GetWithCourseMatesAsync(homework.CourseId);
7171
var courseModel = _mapper.Map<CourseDTO>(course);
7272

73-
74-
_eventBus.Publish(new UpdateTaskEvent(courseModel, update.Title, taskId, update.PublicationDate));
73+
_eventBus.Publish(new UpdateTaskEvent(courseModel, update.Title, taskId, task.PublicationDate,
74+
update.PublicationDate, update.DeadlineDate));
7575

7676
await _tasksRepository.UpdateAsync(taskId, t => new HomeworkTask()
7777
{

HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/DeleteTaskEventHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public override async Task HandleAsync(DeleteTaskEvent @event)
2222
var scheduleWork = await _scheduleWorksRepository.GetAsync(id);
2323

2424
BackgroundJob.Delete(scheduleWork.JobId);
25-
2625
await _scheduleWorksRepository.DeleteAsync(id);
2726
}
2827
}

HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/NewHomeworkTaskEventHandler.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
22
using System.Linq;
33
using System.Threading.Tasks;
4+
using Hangfire;
45
using HwProj.AuthService.Client;
56
using HwProj.EventBus.Client.Interfaces;
7+
using HwProj.Events.CourseEvents;
8+
using HwProj.Models;
69
using HwProj.Models.NotificationsService;
710
using HwProj.NotificationsService.API.Repositories;
8-
using HwProj.CoursesService.API.Events;
9-
using HwProj.Models;
10-
using Hangfire;
11-
using HwProj.Events.CourseEvents;
1211
using HwProj.NotificationsService.API.Services;
1312
using Microsoft.Extensions.Configuration;
1413

@@ -39,10 +38,15 @@ public NewHomeworkTaskEventHandler(
3938
public override async Task HandleAsync(NewTaskEvent @event)
4039
{
4140
var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId);
42-
var jobId = BackgroundJob.Schedule( () => ScheduleWorkAsync(@event, id),
43-
@event.PublicationDate >= DateTimeUtils.GetMoscowNow()
44-
? @event.PublicationDate.Subtract(TimeSpan.FromHours(3))
45-
: DateTimeUtils.GetMoscowNow().Subtract(TimeSpan.FromHours(3)));
41+
42+
if (@event.PublicationDate <= DateTimeUtils.GetMoscowNow())
43+
{
44+
await AddNotificationsAsync(@event);
45+
return;
46+
}
47+
48+
var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event),
49+
@event.PublicationDate.Subtract(TimeSpan.FromHours(3)));
4650

4751
var scheduleWork = new ScheduleWork
4852
{
@@ -53,7 +57,7 @@ public override async Task HandleAsync(NewTaskEvent @event)
5357
}
5458

5559

56-
public async Task ScheduleWorkAsync(NewTaskEvent @event, string id)
60+
public async Task AddNotificationsAsync(NewTaskEvent @event)
5761
{
5862
var studentIds = @event.Course.CourseMates.Select(t => t.StudentId).ToArray();
5963
var accountsData = await _authServiceClient.GetAccountsData(studentIds);
@@ -79,8 +83,6 @@ public async Task ScheduleWorkAsync(NewTaskEvent @event, string id)
7983

8084
await Task.WhenAll(addNotificationTask, sendEmailTask);
8185
}
82-
83-
await _scheduleWorksRepository.DeleteAsync(id);
8486
}
8587
}
8688
}

HwProj.NotificationsService/HwProj.NotificationsService.API/EventHandlers/UpdateTaskEventHandler.cs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
using System;
12
using System.Threading.Tasks;
23
using AutoMapper;
4+
using Hangfire;
35
using HwProj.AuthService.Client;
46
using HwProj.EventBus.Client.Interfaces;
57
using HwProj.Events.CourseEvents;
@@ -39,8 +41,37 @@ public UpdateTaskEventHandler(
3941

4042
public override async Task HandleAsync(UpdateTaskEvent @event)
4143
{
42-
//TODO: event types
4344
var id = ScheduleWorkIdBuilder.Build(@event, @event.TaskId);
45+
var isTaskPublished = @event.PreviousPublicationDate <= DateTimeUtils.GetMoscowNow();
46+
if (isTaskPublished)
47+
{
48+
await AddNotificationsAsync(@event);
49+
return;
50+
}
51+
52+
var previousWork = await _scheduleWorksRepository.GetAsync(id);
53+
BackgroundJob.Delete(previousWork.JobId);
54+
55+
var jobId = BackgroundJob.Schedule(() => AddNotificationsAsync(@event),
56+
@event.PublicationDate.Subtract(TimeSpan.FromHours(3)));
57+
58+
await _scheduleWorksRepository.UpdateAsync(id, work => new ScheduleWork()
59+
{
60+
Id = id,
61+
JobId = jobId
62+
});
63+
}
64+
65+
public async Task AddNotificationsAsync(UpdateTaskEvent @event)
66+
{
67+
var url = _configuration["Url"];
68+
var isTaskPublished = @event.PreviousPublicationDate < DateTimeUtils.GetMoscowNow();
69+
var message = isTaskPublished
70+
? $"Задача <a href='{_configuration["Url"]}/task/{@event.TaskId}'>{@event.TaskTitle}</a>" +
71+
$" из курса <a href='{_configuration["Url"]}/courses/{@event.Course.Id}'>{@event.Course.Name}</a> обновлена."
72+
: $"В курсе <a href='{url}/courses/{@event.Course.Id}'>{@event.Course.Name}</a>" +
73+
$" опубликована новая задача <a href='{url}/task/{@event.TaskId}'>{@event.TaskTitle}</a>." +
74+
(@event.Deadline is { } deadline ? $"\n\nДедлайн: {deadline:U}" : "");
4475

4576
foreach (var student in @event.Course.CourseMates)
4677
{
@@ -49,8 +80,7 @@ public override async Task HandleAsync(UpdateTaskEvent @event)
4980
var notification = new Notification
5081
{
5182
Sender = "CourseService",
52-
Body = $"Задача <a href='{_configuration["Url"]}/task/{@event.TaskId}'>{@event.TaskTitle}</a>" +
53-
$" из курса <a href='{_configuration["Url"]}/courses/{@event.Course.Id}'>{@event.Course.Name}</a> обновлена.",
83+
Body = message,
5484
Category = CategoryState.Courses,
5585
Date = DateTimeUtils.GetMoscowNow(),
5686
HasSeen = false,
@@ -63,10 +93,5 @@ public override async Task HandleAsync(UpdateTaskEvent @event)
6393
await Task.WhenAll(addNotificationTask, sendEmailTask);
6494
}
6595
}
66-
67-
public async Task ScheduleWorkAsync()
68-
{
69-
70-
}
7196
}
72-
}
97+
}

HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/IScheduleWorksRepository.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ namespace HwProj.NotificationsService.API.Repositories;
77

88
public interface IScheduleWorksRepository : ICrudRepository<ScheduleWork, string>
99
{
10-
bool Contains(string id);
1110
}

HwProj.NotificationsService/HwProj.NotificationsService.API/Repositories/ScheduleWorksRepository.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,5 @@ public ScheduleWorksRepository(NotificationsContext context) : base(context)
1313
{
1414
}
1515

16-
public bool Contains(string id)
17-
{
18-
return Context.Set<ScheduleWork>().AsNoTracking().Any(work => work.Id.Equals(id));
19-
}
2016
}
2117
}

HwProj.NotificationsService/HwProj.NotificationsService.API/Startup.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
using HwProj.AuthService.API.Events;
1+
using Hangfire;
2+
using HwProj.AuthService.API.Events;
23
using HwProj.AuthService.Client;
4+
using HwProj.CoursesService.API.Events;
35
using HwProj.EventBus.Client.Interfaces;
6+
using HwProj.Events.CourseEvents;
47
using HwProj.NotificationsService.API.EventHandlers;
58
using HwProj.NotificationsService.API.Models;
69
using HwProj.NotificationsService.API.Repositories;
710
using HwProj.NotificationsService.API.Services;
11+
using HwProj.SolutionsService.API.Events;
812
using HwProj.Utils.Configuration;
9-
using Hangfire;
10-
using Hangfire.SqlServer;
1113
using Microsoft.AspNetCore.Builder;
1214
using Microsoft.AspNetCore.Hosting;
1315
using Microsoft.EntityFrameworkCore;
1416
using Microsoft.Extensions.Configuration;
1517
using Microsoft.Extensions.DependencyInjection;
16-
using HwProj.CoursesService.API.Events;
17-
using HwProj.EventBus.Client;
18-
using HwProj.SolutionsService.API.Events;
19-
using HwProj.Events.CourseEvents;
2018

2119
namespace HwProj.NotificationsService.API
2220
{
@@ -51,6 +49,7 @@ public void ConfigureServices(IServiceCollection services)
5149
services.AddTransient<IEventHandler<StudentPassTaskEvent>, StudentPassTaskEventHandler>();
5250
services.AddTransient<IEventHandler<UpdateHomeworkEvent>, UpdateHomeworkEventHandler>();
5351
services.AddTransient<IEventHandler<UpdateTaskEvent>, UpdateTaskEventHandler>();
52+
services.AddTransient<IEventHandler<DeleteTaskEvent>, DeleteTaskEventHandler>();
5453
services.AddTransient<IEventHandler<LecturerAcceptToCourseEvent>, LecturerAcceptToCourseEventHandler>();
5554
services.AddTransient<IEventHandler<LecturerRejectToCourseEvent>, LecturerRejectToCourseEventHandler>();
5655
services.AddTransient<IEventHandler<LecturerInvitedToCourseEvent>, LecturerInvitedToCourseEventHandler>();
@@ -76,6 +75,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, IEventBu
7675
eventBustSubscriber.Subscribe<StudentPassTaskEvent, StudentPassTaskEventHandler>();
7776
eventBustSubscriber.Subscribe<UpdateHomeworkEvent, UpdateHomeworkEventHandler>();
7877
eventBustSubscriber.Subscribe<UpdateTaskEvent, UpdateTaskEventHandler>();
78+
eventBustSubscriber.Subscribe<DeleteTaskEvent, DeleteTaskEventHandler>();
7979
eventBustSubscriber.Subscribe<LecturerAcceptToCourseEvent, LecturerAcceptToCourseEventHandler>();
8080
eventBustSubscriber.Subscribe<LecturerRejectToCourseEvent, LecturerRejectToCourseEventHandler>();
8181
eventBustSubscriber.Subscribe<LecturerInvitedToCourseEvent, LecturerInvitedToCourseEventHandler>();

0 commit comments

Comments
 (0)