Skip to content

Commit

Permalink
Merge pull request #22 from bentran1vn/Features/Group/SendMailAndAccept
Browse files Browse the repository at this point in the history
Refactor settings, handlers, and add new command
  • Loading branch information
Pham-Phuc-Nghi authored Oct 16, 2024
2 parents 1b219af + 9900547 commit ed29e68
Show file tree
Hide file tree
Showing 10 changed files with 866 additions and 37 deletions.
3 changes: 2 additions & 1 deletion MBS-COMMAND.API/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@
"WithThreadId"
]
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"Domain" : "localhost:3000"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using MBS_COMMAND.Contract.Abstractions.Messages;
using MBS_COMMAND.Contract.Abstractions.Shared;
using MBS_COMMAND.Domain.Abstractions;
using MBS_COMMAND.Domain.Abstractions.Repositories;
using MBS_COMMAND.Domain.Entities;
using MBS_CONTRACT.SHARE.Services.Groups;
using Command = MBS_COMMAND.Contract.Services.Groups.Command;

namespace MBS_COMMAND.Application.UserCases.Commands.Groups;

public class AcceptGroupInvitationCommandHandler(
IRepositoryBase<User, Guid> userRepository,
IRepositoryBase<Group, Guid> groupRepository,
IUnitOfWork unitOfWork)
: ICommandHandler<Command.AcceptGroupInvitation>
{
public async Task<Result> Handle(Command.AcceptGroupInvitation request, CancellationToken cancellationToken)
{
var u = await userRepository.FindByIdAsync(request.MemberId, cancellationToken);
if (u == null)
return Result.Failure(new Error("404", "User Not Found"));
if (u.Status == 2)
{
return Result.Failure(new Error("403", "User is blocked"));
}

var g = await groupRepository.FindSingleAsync(x => x.Id == request.GroupId, cancellationToken);
if (g == null)
return Result.Failure(new Error("404", "Group Not Found"));
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);
return Result.Success("Member added to group");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
using MBS_COMMAND.Domain.Abstractions;
using MBS_COMMAND.Domain.Abstractions.Repositories;
using MBS_COMMAND.Domain.Entities;
using Microsoft.Extensions.Configuration;

namespace MBS_COMMAND.Application.UserCases.Commands.Groups;

public sealed class AddMemberToGroupCommandHandler(IRepositoryBase<Group, Guid> groupRepository, IRepositoryBase<User, Guid> userRepository, IUnitOfWork unitOfWork, IMailService mailService) : ICommandHandler<Command.AddMemberToGroup>
public sealed class AddMemberToGroupCommandHandler(
IRepositoryBase<Group, Guid> groupRepository,
IRepositoryBase<User, Guid> userRepository,
IConfiguration configuration,
IMailService mailService) : ICommandHandler<Command.AddMemberToGroup>
{
public async Task<Result> Handle(Command.AddMemberToGroup request, CancellationToken cancellationToken)
{
Expand All @@ -20,17 +25,24 @@ public async Task<Result> Handle(Command.AddMemberToGroup request, CancellationT
return Result.Failure(new Error("404", "Group Not Found"));
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 });
groupRepository.Update(g);
//g.Members!.Add(new Group_Student_Mapping { StudentId = u.Id, GroupId = g.Id });
var domain = configuration["Domain"];
await mailService.SendMail(new MailContent
{
To = u.Email,
Subject = "Join Group",
Body = $"You have been invited to group {g.Name}"
Subject = $"Invitation to group {g.Name}",
Body = $@"
<p>You have been invited to group {g.Name}</p>
<a href='{domain}/api/v1/groups/accept-invitation/{g.Id}/{u.Id}'
style='display:inline-block; padding:10px 20px; background-color:#4CAF50; color:white; text-decoration:none; border-radius:5px;'>
Accept Invitation
</a>
"
});
await unitOfWork.SaveChangesAsync(cancellationToken);


//send mail to user

return Result.Success();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using MBS_COMMAND.Domain.Entities;
using static System.DateOnly;
namespace MBS_COMMAND.Application.UserCases.Commands.Slots;
//Test v2
public sealed class CreateSlotCommandHandler(
IRepositoryBase<Slot, Guid> slotRepository,
IRepositoryBase<User, Guid> userRepository,
IRepositoryBase<Semester,Guid> semesterRepository,
IUnitOfWork unitOfWork)
: ICommandHandler<Command.CreateSlot>
{
Expand All @@ -18,7 +18,9 @@ public async Task<Result> Handle(Command.CreateSlot request, CancellationToken c
var mentor = await userRepository.FindByIdAsync(request.MentorId, cancellationToken);
if (mentor == null)
return Result.Failure(new Error("404", "User Not Found"));

var currentSemester = await semesterRepository.FindSingleAsync(x => x.IsActive, cancellationToken);
if(request.SlotModels.Any(x=>Parse(x.Date) < currentSemester!.From || Parse(x.Date) > currentSemester.From.AddDays(6)))
return Result.Failure(new Error("400", "Slot date must within the current semester and in the 1st week of the semester"));
var newSlots = request.SlotModels.Select(slotModel => new Slot
{
Id = Guid.NewGuid(),
Expand All @@ -40,12 +42,9 @@ public async Task<Result> Handle(Command.CreateSlot request, CancellationToken c
var existingSlots = slotRepository.FindAll(x => x.MentorId == mentor.Id);

// Check for overlaps with existing slots
foreach (var newSlot in newSlots)
foreach (var newSlot in newSlots.Where(newSlot => HasOverlap(newSlot, existingSlots)))
{
if (HasOverlap(newSlot, existingSlots))
{
return Result.Failure(new Error("409", $"Slot overlaps with existing slot: {newSlot.Date} {newSlot.StartTime}-{newSlot.EndTime}"));
}
return Result.Failure(new Error("409", $"Slot overlaps with existing slot: {newSlot.Date} {newSlot.StartTime}-{newSlot.EndTime}"));
}

slotRepository.AddRange(newSlots);
Expand Down
1 change: 1 addition & 0 deletions MBS_COMMAND.Contract/Services/Groups/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public record AddMemberToGroup(Guid GroupId, Guid MemberId) : ICommand;
public record RemoveMemberFromGroup(Guid GroupId, Guid MemberId) : ICommand;
public record ChangeLeader(Guid GroupId, Guid NewLeaderId) : ICommand;
public record UpdateGroup(Guid GroupId, string Name, string Stacks) : ICommand;
public record AcceptGroupInvitation(Guid GroupId, Guid MemberId) : ICommand;
}
1 change: 1 addition & 0 deletions MBS_COMMAND.Domain/Entities/Semester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class Semester : Entity<Guid>, IAuditableEntity
public string Name { get; set; }
public DateOnly From { get; set; }
public DateOnly To { get; set; }
public bool IsActive { get; set; }
public DateTimeOffset CreatedOnUtc { get; set; }
public DateTimeOffset? ModifiedOnUtc { get; set; }
}
Loading

0 comments on commit ed29e68

Please sign in to comment.