Skip to content

Commit

Permalink
Merge pull request #24 from bentran1vn/Features/Group/SendMailAndAccept
Browse files Browse the repository at this point in the history
Add slot generation for semester command and API endpoint
  • Loading branch information
PhucNghi176 authored Oct 16, 2024
2 parents a21317a + 78bc6d9 commit 09f5ded
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using MBS_COMMAND.Contract.Abstractions.Messages;
using MBS_COMMAND.Contract.Abstractions.Shared;
using MBS_COMMAND.Contract.Services.Slots;
using MBS_COMMAND.Domain.Abstractions;
using MBS_COMMAND.Domain.Abstractions.Repositories;
using MBS_COMMAND.Domain.Entities;
using static MBS_COMMAND.Contract.Abstractions.Shared.Result;

namespace MBS_COMMAND.Application.UserCases.Commands.Slots;

public class GenerateSlotForSemesterCommandHandler(
IRepositoryBase<Slot, Guid> slotRepository,
IRepositoryBase<Semester, Guid> semesterRepository,
IRepositoryBase<User, Guid> userRepository,
IUnitOfWork unitOfWork)
: ICommandHandler<Command.GenerateSlotForSemester>
{
public async Task<Result> Handle(Command.GenerateSlotForSemester request, CancellationToken cancellationToken)
{
var currentSemester = await semesterRepository.FindSingleAsync(x => x.IsActive, cancellationToken);
if (currentSemester == null)
return Result.Failure(new Error("404", "No active semester found"));

var mentors = userRepository.FindAll(x => x.Role == 2);
var newSlots = new List<Slot>();

foreach (var mentor in mentors)
{
var firstWeekSlots = slotRepository.FindAll(x =>
x.MentorId == mentor.Id && x.Date >= currentSemester.From &&
x.Date <= currentSemester.From.AddDays(6));

newSlots.AddRange(GenerateNewSlots(firstWeekSlots, 10));
}

slotRepository.AddRange(newSlots);
await unitOfWork.SaveChangesAsync(cancellationToken);
return Success();
}

private static IEnumerable<Slot> GenerateNewSlots(IEnumerable<Slot> firstWeekSlots, int weeks)
{
return Enumerable.Range(1, weeks)
.SelectMany(i => firstWeekSlots.Select(x => new Slot
{
Id = Guid.NewGuid(),
MentorId = x.MentorId,
StartTime = x.StartTime,
EndTime = x.EndTime,
Date = x.Date.AddDays(7 * i),
Note = x.Note,
IsOnline = x.IsOnline,
Month = (short)x.Date.AddDays(7 * i).Month
}));
}
}
4 changes: 3 additions & 1 deletion MBS_COMMAND.Contract/Services/Slots/Command.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using MBS_COMMAND.Contract.Abstractions.Messages;
namespace MBS_COMMAND.Contract.Services.Slots;
public class Command
public static class Command
{
public record CreateSlot(Guid MentorId, List<SlotModel> SlotModels) : ICommand;

public record GenerateSlotForSemester(): ICommand;
}
14 changes: 9 additions & 5 deletions MBS_COMMAND.Presentation/APIs/Slots/SlotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ public void AddRoutes(IEndpointRouteBuilder app)
{
var gr1 = app.NewVersionedApi("Slots").MapGroup(BaseUrl).HasApiVersion(1);
gr1.MapPost(string.Empty, CreateSlot).WithSummary("mm/dd/yyyy");
gr1.MapGet("generate", GenerateSlotForSemester);
}
public static async Task<IResult> CreateSlot(ISender sender, Command.CreateSlot command)

private static async Task<IResult> CreateSlot(ISender sender,[FromBody] Command.CreateSlot command)
{
var result = await sender.Send(command);
if (result.IsFailure)
return HandlerFailure(result);

return Results.Ok(result);
return result.IsFailure ? HandlerFailure(result) : Results.Ok(result);
}
private static async Task<IResult> GenerateSlotForSemester(ISender sender)
{
var result = await sender.Send(new Command.GenerateSlotForSemester());
return result.IsFailure ? HandlerFailure(result) : Results.Ok(result);
}
}

0 comments on commit 09f5ded

Please sign in to comment.