Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucNghi176 committed Oct 25, 2024
1 parent 0236c87 commit aa492f1
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 34 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.14" />
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.15" />
<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 @@ -5,7 +5,6 @@
using MBS_COMMAND.Domain.Entities;

namespace MBS_COMMAND.Application.UserCases.Commands.Schedules;

public class CreateScheduleCommandHandler : ICommandHandler<Command.CreateScheduleCommand>
{
private readonly IRepositoryBase<User, Guid> _userRepository;
Expand All @@ -14,7 +13,9 @@ public class CreateScheduleCommandHandler : ICommandHandler<Command.CreateSchedu
private readonly IRepositoryBase<Subject, Guid> _subjectRepository;
private readonly IRepositoryBase<Schedule, Guid> _scheduleRepository;

public CreateScheduleCommandHandler(IRepositoryBase<User, Guid> userRepository, IRepositoryBase<Group, Guid> groupRepository, IRepositoryBase<Slot, Guid> slotRepository, IRepositoryBase<Subject, Guid> subjectRepository, IRepositoryBase<Schedule, Guid> scheduleRepository)
public CreateScheduleCommandHandler(IRepositoryBase<User, Guid> userRepository,
IRepositoryBase<Group, Guid> groupRepository, IRepositoryBase<Slot, Guid> slotRepository,
IRepositoryBase<Subject, Guid> subjectRepository, IRepositoryBase<Schedule, Guid> scheduleRepository)
{
_userRepository = userRepository;
_groupRepository = groupRepository;
Expand All @@ -32,15 +33,15 @@ public async Task<Result> 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 !"));
Expand All @@ -52,15 +53,15 @@ public async Task<Result> 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 !"));
}

var start = TimeOnly.Parse(request.StartTime);
var end = TimeOnly.Parse(request.EndTime);

if (start.CompareTo(slot.StartTime) < 0 ||
end.CompareTo(slot.EndTime) > 0)
{
Expand All @@ -80,9 +81,9 @@ public async Task<Result> Handle(Command.CreateScheduleCommand request, Cancella
};

slot.IsBook = true;

slot.ChangeSlotStatusInToBooked(slot.Id);
_scheduleRepository.Add(schedule);

return Result.Success("Booking Schedule Successfully !");
}
}
16 changes: 2 additions & 14 deletions MBS_COMMAND.Domain/Entities/Slot.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,20 +18,9 @@ public class Slot : AggregateRoot<Guid>, IAuditableEntity
public DateTimeOffset CreatedOnUtc { get; set; }
public DateTimeOffset? ModifiedOnUtc { get; set; }

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

}
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 @@ -11,7 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.14" />
<PackageReference Include="MBS_CONTRACT.SHARE" Version="1.1.15" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServicesShared.Slots.DomainEvent.SlotsCreated>(
outboxMessage.Content,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});
case nameof(ServicesShared.Slots.DomainEvent.ChangeSlotStatusInToBooked):
var ChangeSlotStatusInToBooked =
JsonConvert.DeserializeObject<ServicesShared.Slots.DomainEvent.ChangeSlotStatusInToBooked>(
outboxMessage.Content,
new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All
});


await _publishEndpoint.Publish(slotsCreated, context.CancellationToken);
await _publishEndpoint.Publish(ChangeSlotStatusInToBooked, context.CancellationToken);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<DateOnly>("Date")
.HasColumnType("date");

b.Property<string>("Description")
.HasColumnType("nvarchar(max)");

b.Property<TimeOnly>("EndTime")
.HasColumnType("time");

Expand Down
2 changes: 1 addition & 1 deletion MBS_COMMAND.Presentation/APIs/Schedules/SchedulesApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IResult> CreateSchedules(ISender sender, HttpContext context, IJwtTokenService jwtTokenService,
Expand Down

0 comments on commit aa492f1

Please sign in to comment.