Skip to content

Commit

Permalink
Update and GenerateSlotForSemesterCommandHandler.cs not finish
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucNghi176 committed Oct 21, 2024
1 parent daefc3b commit a9c1020
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion MBS_COMMAND.Application/MBS_COMMAND.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.10.0" />
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.6" />
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.11" />
<PackageReference Include="MediatR" Version="12.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public async Task<Result> 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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MBS_COMMAND.Application.UserCases.Commands.Groups;

public sealed class CreateGroupCommandHandler(
IRepositoryBase<Group, Guid> repositoryBase,
IRepositoryBase<User, Guid> userRepository,
ICurrentUserService currentUserService) : ICommandHandler<Command.CreateGroupCommand>
{
public async Task<Result> Handle(Command.CreateGroupCommand request, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public async Task<Result> Handle(Command.CreateSlot request, CancellationToken c
}

slotRepository.AddRange(newSlots);
mentor.CreateSlot(newSlots, mentor.Id);

mentor.CreateSlot(newSlots);
await unitOfWork.SaveChangesAsync(cancellationToken);

return Result.Success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Slot, Guid> slotRepository,
IRepositoryBase<Semester, Guid> semesterRepository,
IRepositoryBase<User, Guid> userRepository,
Expand All @@ -19,7 +18,7 @@ public async Task<Result> 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<Slot>();
Expand All @@ -29,9 +28,11 @@ public async Task<Result> 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);
Expand Down
2 changes: 1 addition & 1 deletion MBS_COMMAND.Contract/Services/Slots/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public static class Command
{
public record CreateSlot(Guid MentorId, List<SlotModel> SlotModels) : ICommand;

public record GenerateSlotForSemester(): ICommand;
public record GenerateSlotForSemester: ICommand;
}
16 changes: 15 additions & 1 deletion MBS_COMMAND.Domain/Entities/Slot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public class Slot : AggregateRoot<Guid>, IAuditableEntity
public DateTimeOffset CreatedOnUtc { get; set; }
public DateTimeOffset? ModifiedOnUtc { get; set; }


public void CreateSlot(IEnumerable<Slot> 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()));
}

}
6 changes: 2 additions & 4 deletions MBS_COMMAND.Domain/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CreateMentor(User user)
user.Status, user.CreatedOnUtc, user.IsDeleted));
}

public void CreateSlot(IEnumerable<Slot> slots, Guid MentorID)
public void CreateSlot(IEnumerable<Slot> slots)
{
var slot = slots.Select(x => new DomainEvent.Slot
{
Expand All @@ -41,9 +41,7 @@ public void CreateSlot(IEnumerable<Slot> 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));
}
}
2 changes: 1 addition & 1 deletion MBS_COMMAND.Domain/MBS_COMMAND.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.6" />
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.11" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServicesShared.Users.DomainEvent.MentorSlotCreated>(
outboxMessage.Content,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
await _publishEndpoint.Publish(MentorSlotCreated, context.CancellationToken);
break;
case nameof(ServicesShared.Slots.DomainEvent.SlotsCreated):
var slotsCreated = JsonConvert.DeserializeObject<ServicesShared.Slots.DomainEvent.SlotsCreated>(
outboxMessage.Content,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
await _publishEndpoint.Publish(slotsCreated, context.CancellationToken);
break;

// case nameof(DomainEvent.ProductUpdated):
// var productUpdated = JsonConvert.DeserializeObject<DomainEvent.ProductUpdated>(
Expand Down

0 comments on commit a9c1020

Please sign in to comment.