-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e06b69
commit 19d4567
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
MBS_COMMAND.Application/UserCases/Commands/Groups/AddMentorToGroupCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters