From a9c10204119222bae064274485abccf054b5ac40 Mon Sep 17 00:00:00 2001 From: Eric <36512385+PhucNghi176@users.noreply.github.com> Date: Tue, 22 Oct 2024 02:28:15 +0700 Subject: [PATCH] Update and GenerateSlotForSemesterCommandHandler.cs not finish --- .../MBS_COMMAND.Application.csproj | 2 +- .../AcceptGroupInvitationCommandHandler.cs | 2 +- .../Groups/CreateGroupCommandHandler.cs | 1 - .../Commands/Slots/CreateSlotCommandHandler.cs | 3 ++- .../GenerateSlotForSemesterCommandHandler.cs | 11 ++++++----- MBS_COMMAND.Contract/Services/Slots/Command.cs | 2 +- MBS_COMMAND.Domain/Entities/Slot.cs | 16 +++++++++++++++- MBS_COMMAND.Domain/Entities/User.cs | 6 ++---- MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj | 2 +- .../BackgroundJobs/ProcessOutboxMessagesJob.cs | 18 ++++++++++++++++++ 10 files changed, 47 insertions(+), 16 deletions(-) diff --git a/MBS_COMMAND.Application/MBS_COMMAND.Application.csproj b/MBS_COMMAND.Application/MBS_COMMAND.Application.csproj index 2ca612b..acc56a4 100644 --- a/MBS_COMMAND.Application/MBS_COMMAND.Application.csproj +++ b/MBS_COMMAND.Application/MBS_COMMAND.Application.csproj @@ -16,7 +16,7 @@ - + diff --git a/MBS_COMMAND.Application/UserCases/Commands/Groups/AcceptGroupInvitationCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Groups/AcceptGroupInvitationCommandHandler.cs index 0df641c..99a72cc 100644 --- a/MBS_COMMAND.Application/UserCases/Commands/Groups/AcceptGroupInvitationCommandHandler.cs +++ b/MBS_COMMAND.Application/UserCases/Commands/Groups/AcceptGroupInvitationCommandHandler.cs @@ -30,7 +30,7 @@ public async Task Handle(Command.AcceptGroupInvitation request, Cancella if (g.Members!.Any(x => x.StudentId == u.Id)) return Result.Failure(new Error("422", "Member already joined group")); g.Members!.Add(new Group_Student_Mapping { StudentId = u.Id, GroupId = g.Id }); - unitOfWork.SaveChangesAsync(cancellationToken); + await unitOfWork.SaveChangesAsync(cancellationToken); return Result.Success("Member added to group"); } } \ No newline at end of file diff --git a/MBS_COMMAND.Application/UserCases/Commands/Groups/CreateGroupCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Groups/CreateGroupCommandHandler.cs index 0a7dc2a..5849155 100644 --- a/MBS_COMMAND.Application/UserCases/Commands/Groups/CreateGroupCommandHandler.cs +++ b/MBS_COMMAND.Application/UserCases/Commands/Groups/CreateGroupCommandHandler.cs @@ -8,7 +8,6 @@ namespace MBS_COMMAND.Application.UserCases.Commands.Groups; public sealed class CreateGroupCommandHandler( IRepositoryBase repositoryBase, - IRepositoryBase userRepository, ICurrentUserService currentUserService) : ICommandHandler { public async Task Handle(Command.CreateGroupCommand request, CancellationToken cancellationToken) diff --git a/MBS_COMMAND.Application/UserCases/Commands/Slots/CreateSlotCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Slots/CreateSlotCommandHandler.cs index 0e7bfb1..45b247d 100644 --- a/MBS_COMMAND.Application/UserCases/Commands/Slots/CreateSlotCommandHandler.cs +++ b/MBS_COMMAND.Application/UserCases/Commands/Slots/CreateSlotCommandHandler.cs @@ -48,7 +48,8 @@ public async Task Handle(Command.CreateSlot request, CancellationToken c } slotRepository.AddRange(newSlots); - mentor.CreateSlot(newSlots, mentor.Id); + + mentor.CreateSlot(newSlots); await unitOfWork.SaveChangesAsync(cancellationToken); return Result.Success(); diff --git a/MBS_COMMAND.Application/UserCases/Commands/Slots/GenerateSlotForSemesterCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Slots/GenerateSlotForSemesterCommandHandler.cs index 62e79fc..2baf51a 100644 --- a/MBS_COMMAND.Application/UserCases/Commands/Slots/GenerateSlotForSemesterCommandHandler.cs +++ b/MBS_COMMAND.Application/UserCases/Commands/Slots/GenerateSlotForSemesterCommandHandler.cs @@ -7,8 +7,7 @@ using static MBS_COMMAND.Contract.Abstractions.Shared.Result; namespace MBS_COMMAND.Application.UserCases.Commands.Slots; - -public class GenerateSlotForSemesterCommandHandler( +public sealed class GenerateSlotForSemesterCommandHandler( IRepositoryBase slotRepository, IRepositoryBase semesterRepository, IRepositoryBase userRepository, @@ -19,7 +18,7 @@ public async Task Handle(Command.GenerateSlotForSemester request, Cancel { var currentSemester = await semesterRepository.FindSingleAsync(x => x.IsActive, cancellationToken); if (currentSemester == null) - return Result.Failure(new Error("404", "No active semester found")); + return Failure(new Error("404", "No active semester found")); var mentors = userRepository.FindAll(x => x.Role == 2); var newSlots = new List(); @@ -29,9 +28,11 @@ public async Task Handle(Command.GenerateSlotForSemester request, Cancel var firstWeekSlots = slotRepository.FindAll(x => x.MentorId == mentor.Id && x.Date >= currentSemester.From && x.Date <= currentSemester.From.AddDays(6)); - - newSlots.AddRange(GenerateNewSlots(firstWeekSlots, 10)); + var slot = GenerateNewSlots(firstWeekSlots, 10); + newSlots.AddRange(slot); + } + new Slot().CreateSlot(newSlots); slotRepository.AddRange(newSlots); await unitOfWork.SaveChangesAsync(cancellationToken); diff --git a/MBS_COMMAND.Contract/Services/Slots/Command.cs b/MBS_COMMAND.Contract/Services/Slots/Command.cs index 5fe37ac..1ab6939 100644 --- a/MBS_COMMAND.Contract/Services/Slots/Command.cs +++ b/MBS_COMMAND.Contract/Services/Slots/Command.cs @@ -4,5 +4,5 @@ public static class Command { public record CreateSlot(Guid MentorId, List SlotModels) : ICommand; - public record GenerateSlotForSemester(): ICommand; + public record GenerateSlotForSemester: ICommand; } diff --git a/MBS_COMMAND.Domain/Entities/Slot.cs b/MBS_COMMAND.Domain/Entities/Slot.cs index a9cea3d..e62dbc2 100644 --- a/MBS_COMMAND.Domain/Entities/Slot.cs +++ b/MBS_COMMAND.Domain/Entities/Slot.cs @@ -19,6 +19,20 @@ public class Slot : AggregateRoot, IAuditableEntity public DateTimeOffset CreatedOnUtc { get; set; } public DateTimeOffset? ModifiedOnUtc { get; set; } - + public void CreateSlot(IEnumerable slots) + { + var slot = slots.Select(x => new DomainEvent.Slot + { + MentorId = x.MentorId, + StartTime = x.StartTime, + EndTime = x.EndTime, + Date = x.Date, + IsOnline = x.IsOnline, + Note = x.Note, + Month = x.Month, + IsBook = x.IsBook, + }).ToList(); + RaiseDomainEvent(new DomainEvent.SlotsCreated(Guid.NewGuid(), slot,new Guid())); + } } diff --git a/MBS_COMMAND.Domain/Entities/User.cs b/MBS_COMMAND.Domain/Entities/User.cs index ef2d473..4acb59f 100644 --- a/MBS_COMMAND.Domain/Entities/User.cs +++ b/MBS_COMMAND.Domain/Entities/User.cs @@ -29,7 +29,7 @@ public void CreateMentor(User user) user.Status, user.CreatedOnUtc, user.IsDeleted)); } - public void CreateSlot(IEnumerable slots, Guid MentorID) + public void CreateSlot(IEnumerable slots) { var slot = slots.Select(x => new DomainEvent.Slot { @@ -41,9 +41,7 @@ public void CreateSlot(IEnumerable slots, Guid MentorID) Note = x.Note, Month = x.Month, IsBook = x.IsBook, - CreatedOnUtc = x.CreatedOnUtc, - ModifiedOnUtc = x.ModifiedOnUtc }).ToList(); - RaiseDomainEvent(new DomainEvent.SlotsCreated(Guid.NewGuid(), slot, MentorID)); + RaiseDomainEvent(new DomainEvent.MentorSlotCreated(Guid.NewGuid(), slot)); } } \ No newline at end of file diff --git a/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj b/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj index 2fcb034..620d841 100644 --- a/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj +++ b/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj @@ -7,7 +7,7 @@ - + diff --git a/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs b/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs index 9b86ca4..b2e5f72 100644 --- a/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs +++ b/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs @@ -68,6 +68,24 @@ public async Task Execute(IJobExecutionContext context) }); await _publishEndpoint.Publish(mentorCreated, context.CancellationToken); break; + case nameof(ServicesShared.Users.DomainEvent.MentorSlotCreated): + var MentorSlotCreated = JsonConvert.DeserializeObject( + outboxMessage.Content, + new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); + await _publishEndpoint.Publish(MentorSlotCreated, context.CancellationToken); + break; + case nameof(ServicesShared.Slots.DomainEvent.SlotsCreated): + var slotsCreated = JsonConvert.DeserializeObject( + outboxMessage.Content, + new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); + await _publishEndpoint.Publish(slotsCreated, context.CancellationToken); + break; // case nameof(DomainEvent.ProductUpdated): // var productUpdated = JsonConvert.DeserializeObject(