Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
PhucNghi176 committed Oct 18, 2024
1 parent 8e06b69 commit 19d4567
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using MBS_COMMAND.Contract.Abstractions.Messages;
using MBS_COMMAND.Contract.Abstractions.Shared;
using MBS_COMMAND.Contract.Services.Groups;
using MBS_COMMAND.Domain.Abstractions;
using MBS_COMMAND.Domain.Abstractions.Repositories;
using MBS_COMMAND.Domain.Entities;

namespace MBS_COMMAND.Application.UserCases.Commands.Groups;

public class AddMentorToGroupCommandHandler(
IRepositoryBase<User, Guid> userRepository,
IRepositoryBase<Group, Guid> groupRepository,
IUnitOfWork unitOfWork)
: ICommandHandler<Command.AddMentorToGroup>
{
public async Task<Result> Handle(Command.AddMentorToGroup request, CancellationToken cancellationToken)
{
var user = await userRepository.FindByIdAsync(request.MentorId, cancellationToken);
if (user == null)
{
return Result.Failure(new Error("404", "User not found"));
}
if(user.Role!=2)
{
return Result.Failure(new Error("403", "User is not a mentor"));
}
var group = await groupRepository.FindByIdAsync(request.GroupId, cancellationToken);
if (group == null)
{
return Result.Failure(new Error("404", "Group not found"));
}

if (group.MentorId is not null)
{
return Result.Failure(new Error("400", "Group already has a mentor"));
}
group.MentorId = request.MentorId;
groupRepository.Update(group);
await unitOfWork.SaveChangesAsync(cancellationToken);
return Result.Success();
}
}
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 @@ -10,4 +10,5 @@ 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;
public record AddMentorToGroup(Guid GroupId, Guid MentorId) : ICommand;
}
7 changes: 6 additions & 1 deletion MBS_COMMAND.Presentation/APIs/Groups/GroupApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ public void AddRoutes(IEndpointRouteBuilder app)
gr1.MapPut("change-leader", ChangeLeader).WithSummary("must login in order to use this api");
gr1.MapPut(string.Empty, UpdateGroup);
gr1.MapGet("accept-invitation/{groupId}/{memberId}", AcceptGroupInvitation);
gr1.MapPost("mentor", AddMentorToGroup);
}


private static async Task<IResult> AddMentorToGroup(ISender sender, [FromBody] Command.AddMentorToGroup request)
{
var result = await sender.Send(request);
return result.IsFailure ? HandlerFailure(result) : Results.Ok(result);
}
private static async Task<IResult> AcceptGroupInvitation(ISender sender, string groupId,string memberId)
{
var result = await sender.Send(new Command.AcceptGroupInvitation(Guid.Parse(groupId), Guid.Parse(memberId)));
Expand Down

0 comments on commit 19d4567

Please sign in to comment.