From aa492f15aa453677e5850bb74afacd9881a1941e Mon Sep 17 00:00:00 2001 From: Eric <36512385+PhucNghi176@users.noreply.github.com> Date: Fri, 25 Oct 2024 11:23:27 +0700 Subject: [PATCH] Update --- .../MBS_COMMAND.Application.csproj | 2 +- .../Schedules/CreateScheduleCommandHandler.cs | 19 ++++++++++--------- MBS_COMMAND.Domain/Entities/Slot.cs | 16 ++-------------- MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj | 2 +- .../ProcessOutboxMessagesJob.cs | 18 ++++++++++-------- .../ApplicationDbContextModelSnapshot.cs | 3 +++ .../APIs/Schedules/SchedulesApi.cs | 2 +- 7 files changed, 28 insertions(+), 34 deletions(-) diff --git a/MBS_COMMAND.Application/MBS_COMMAND.Application.csproj b/MBS_COMMAND.Application/MBS_COMMAND.Application.csproj index cf2dd1e..8105b09 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/Schedules/CreateScheduleCommandHandler.cs b/MBS_COMMAND.Application/UserCases/Commands/Schedules/CreateScheduleCommandHandler.cs index 7aa8d99..e6fbb60 100644 --- a/MBS_COMMAND.Application/UserCases/Commands/Schedules/CreateScheduleCommandHandler.cs +++ b/MBS_COMMAND.Application/UserCases/Commands/Schedules/CreateScheduleCommandHandler.cs @@ -5,7 +5,6 @@ using MBS_COMMAND.Domain.Entities; namespace MBS_COMMAND.Application.UserCases.Commands.Schedules; - public class CreateScheduleCommandHandler : ICommandHandler { private readonly IRepositoryBase _userRepository; @@ -14,7 +13,9 @@ public class CreateScheduleCommandHandler : ICommandHandler _subjectRepository; private readonly IRepositoryBase _scheduleRepository; - public CreateScheduleCommandHandler(IRepositoryBase userRepository, IRepositoryBase groupRepository, IRepositoryBase slotRepository, IRepositoryBase subjectRepository, IRepositoryBase scheduleRepository) + public CreateScheduleCommandHandler(IRepositoryBase userRepository, + IRepositoryBase groupRepository, IRepositoryBase slotRepository, + IRepositoryBase subjectRepository, IRepositoryBase scheduleRepository) { _userRepository = userRepository; _groupRepository = groupRepository; @@ -32,15 +33,15 @@ public async Task Handle(Command.CreateScheduleCommand request, Cancella return Result.Failure(new Error("400", "User is not exist !")); } - var group = await _groupRepository.FindSingleAsync(x => x.LeaderId.Equals(user.Id) && x.ProjectId.Equals(request.ProjectId), cancellationToken); - + var group = await _groupRepository.FindSingleAsync(x => x.LeaderId.Equals(user.Id), cancellationToken); + if (group == null || group.IsDeleted) { return Result.Failure(new Error("403", "Must own a group !")); } var slot = await _slotRepository.FindByIdAsync(request.SlotId, cancellationToken); - + if (slot == null || group.IsDeleted) { return Result.Failure(new Error("400", "Slot is not exist !")); @@ -52,7 +53,7 @@ public async Task Handle(Command.CreateScheduleCommand request, Cancella } var subject = await _subjectRepository.FindByIdAsync(request.SubjectId, cancellationToken); - + if (subject == null || subject.IsDeleted) { return Result.Failure(new Error("400", "Subject is not exist !")); @@ -60,7 +61,7 @@ public async Task Handle(Command.CreateScheduleCommand request, Cancella var start = TimeOnly.Parse(request.StartTime); var end = TimeOnly.Parse(request.EndTime); - + if (start.CompareTo(slot.StartTime) < 0 || end.CompareTo(slot.EndTime) > 0) { @@ -80,9 +81,9 @@ public async Task Handle(Command.CreateScheduleCommand request, Cancella }; slot.IsBook = true; - + slot.ChangeSlotStatusInToBooked(slot.Id); _scheduleRepository.Add(schedule); - + return Result.Success("Booking Schedule Successfully !"); } } \ No newline at end of file diff --git a/MBS_COMMAND.Domain/Entities/Slot.cs b/MBS_COMMAND.Domain/Entities/Slot.cs index 3687f0b..83e6f44 100644 --- a/MBS_COMMAND.Domain/Entities/Slot.cs +++ b/MBS_COMMAND.Domain/Entities/Slot.cs @@ -1,6 +1,5 @@ using MBS_COMMAND.Domain.Abstractions.Aggregates; using MBS_COMMAND.Domain.Abstractions.Entities; -using MBS_CONTRACT.SHARE.Services.Groups; using MBS_CONTRACT.SHARE.Services.Slots; namespace MBS_COMMAND.Domain.Entities; @@ -19,20 +18,9 @@ public class Slot : AggregateRoot, IAuditableEntity public DateTimeOffset CreatedOnUtc { get; set; } public DateTimeOffset? ModifiedOnUtc { get; set; } - public void CreateSlot(IEnumerable slots) + public void ChangeSlotStatusInToBooked(Guid SlotId) { - 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)); + RaiseDomainEvent(new DomainEvent.ChangeSlotStatusInToBooked(Guid.NewGuid(), SlotId));; } } diff --git a/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj b/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj index 52ff600..f1d81e7 100644 --- a/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj +++ b/MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj @@ -11,7 +11,7 @@ - + diff --git a/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs b/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs index 43ee150..5a4722f 100644 --- a/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs +++ b/MBS_COMMAND.Infrastucture/BackgroundJobs/ProcessOutboxMessagesJob.cs @@ -79,15 +79,17 @@ public async Task Execute(IJobExecutionContext context) }); await _publishEndpoint.Publish(MentorSlotCreated, context.CancellationToken); break; - case nameof(ServicesShared.Slots.DomainEvent.SlotsCreated): - var slotsCreated = JsonConvert.DeserializeObject( - outboxMessage.Content, - new JsonSerializerSettings - { - TypeNameHandling = TypeNameHandling.All - }); + case nameof(ServicesShared.Slots.DomainEvent.ChangeSlotStatusInToBooked): + var ChangeSlotStatusInToBooked = + JsonConvert.DeserializeObject( + outboxMessage.Content, + new JsonSerializerSettings + { + TypeNameHandling = TypeNameHandling.All + }); + - await _publishEndpoint.Publish(slotsCreated, context.CancellationToken); + await _publishEndpoint.Publish(ChangeSlotStatusInToBooked, context.CancellationToken); break; } diff --git a/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs b/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs index d418419..3b869de 100644 --- a/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/MBS_COMMAND.Persistence/Migrations/ApplicationDbContextModelSnapshot.cs @@ -267,6 +267,9 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.Property("Date") .HasColumnType("date"); + b.Property("Description") + .HasColumnType("nvarchar(max)"); + b.Property("EndTime") .HasColumnType("time"); diff --git a/MBS_COMMAND.Presentation/APIs/Schedules/SchedulesApi.cs b/MBS_COMMAND.Presentation/APIs/Schedules/SchedulesApi.cs index 229558b..81d5b99 100644 --- a/MBS_COMMAND.Presentation/APIs/Schedules/SchedulesApi.cs +++ b/MBS_COMMAND.Presentation/APIs/Schedules/SchedulesApi.cs @@ -15,7 +15,7 @@ public void AddRoutes(IEndpointRouteBuilder app) var gr1 = app.NewVersionedApi("Schedules") .MapGroup(BaseUrl).HasApiVersion(1); - gr1.MapPost("", CreateSchedules).RequireAuthorization(RoleNames.Student); + gr1.MapPost("", CreateSchedules); } public static async Task CreateSchedules(ISender sender, HttpContext context, IJwtTokenService jwtTokenService,