-
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.
Merge pull request #12 from bentran1vn/Features/Group/Create
Features/group/create
- Loading branch information
Showing
21 changed files
with
259 additions
and
33 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
MBS-COMMAND.API/DependencyInjection/Extensions/CurrentUserService.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,21 @@ | ||
using MBS_COMMAND.Domain.Abstractions.Repositories; | ||
using Microsoft.AspNetCore.Authorization; | ||
using System.Security.Claims; | ||
|
||
namespace MBS_COMMAND.API.DependencyInjection.Extensions; | ||
|
||
public class CurrentUserService : ICurrentUserService | ||
{ | ||
private readonly ClaimsPrincipal? _claimsPrincipal; | ||
|
||
public CurrentUserService(IHttpContextAccessor httpContextAccessor) | ||
{ | ||
_claimsPrincipal = httpContextAccessor?.HttpContext?.User; | ||
|
||
} | ||
|
||
public string? UserId => _claimsPrincipal?.FindFirstValue(ClaimTypes.NameIdentifier); | ||
|
||
public string? UserName => _claimsPrincipal?.FindFirstValue(ClaimTypes.Name); | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
MBS-COMMAND.API/DependencyInjection/Extensions/ServiceCollectionExtensions.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
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
46 changes: 46 additions & 0 deletions
46
MBS_COMMAND.Application/UserCases/Commands/Groups/AddListMemberToGroupCommandHandler.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,46 @@ | ||
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 sealed class AddListMemberToGroupCommandHandler(IRepositoryBase<User, Guid> userRepository, IRepositoryBase<Group, Guid> groupRepository, IUnitOfWork unitOfWork) : ICommandHandler<Command.AddListMemberToGroup> | ||
{ | ||
public async Task<Result> Handle(Command.AddListMemberToGroup request, CancellationToken cancellationToken) | ||
{ | ||
//validate all MemberId | ||
Dictionary<Guid, string> MembersNotFound = []; | ||
var G = await groupRepository.FindSingleAsync(x => x.Id == request.GroupId, cancellationToken); | ||
if (G == null) | ||
return Result.Failure(new Error("404", "Group Not Found")); | ||
foreach (var memberId in request.MemberId) | ||
{ | ||
var user = await userRepository.FindByIdAsync(memberId); | ||
if (user == null) | ||
{ | ||
MembersNotFound.Add(memberId, "Member Not Found"); | ||
} | ||
else | ||
{ | ||
G.Members!.Add(new Group_Student_Mapping { StudentId = user!.Id, GroupId = G.Id }); | ||
groupRepository.Update(G); | ||
try | ||
{ | ||
await unitOfWork.SaveChangesAsync(cancellationToken); | ||
} | ||
catch | ||
{ | ||
MembersNotFound.Add(memberId, "Member already joined group"); | ||
} | ||
} | ||
|
||
} | ||
var returns = string.Join(", ", MembersNotFound.Select(x => $"MemberId: {x.Key}, Error: {x.Value}")); | ||
|
||
|
||
return MembersNotFound.Count > 0 ? Result.Failure(new Error("422", returns)) : Result.Success(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
MBS_COMMAND.Application/UserCases/Commands/Groups/AddMemberToGroupCommandHandler.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,27 @@ | ||
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 sealed class AddMemberToGroupCommandHandler(IRepositoryBase<Group, Guid> groupRepository, IRepositoryBase<User, Guid> userRepository, IUnitOfWork unitOfWork) : ICommandHandler<Command.AddMemberToGroup> | ||
{ | ||
public async Task<Result> Handle(Command.AddMemberToGroup request, CancellationToken cancellationToken) | ||
{ | ||
var U = await userRepository.FindByIdAsync(request.MemberId); | ||
if (U == null) | ||
return Result.Failure(new Error("404", "User Not Found")); | ||
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 }); | ||
groupRepository.Update(G); | ||
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
54 changes: 54 additions & 0 deletions
54
MBS_COMMAND.Application/UserCases/Commands/Groups/RemoveListMemberFromGroupCommandHandler.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,54 @@ | ||
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 sealed class RemoveListMemberFromGroupCommandHandler(IRepositoryBase<Group, Guid> groupRepository, IRepositoryBase<User, Guid> userRepository, IUnitOfWork unitOfWork) : ICommandHandler<Command.RemoveListMemberFromGroup> | ||
{ | ||
public async Task<Result> Handle(Command.RemoveListMemberFromGroup request, CancellationToken cancellationToken) | ||
{ | ||
Dictionary<Guid, string> MembersNotFound = []; | ||
var G = await groupRepository.FindSingleAsync(x => x.Id == request.GroupId, cancellationToken); | ||
if (G == null) | ||
return Result.Failure(new Error("404", "Group Not Found")); | ||
if (request.MemberId.Contains((Guid)G.LeaderId)) | ||
return Result.Failure(new Error("422", "Mentor cannot be removed from group")); | ||
foreach (var memberId in request.MemberId) | ||
{ | ||
var user = await userRepository.FindByIdAsync(memberId); | ||
if (user == null) | ||
{ | ||
MembersNotFound.Add(memberId, "Member Not Found"); | ||
} | ||
else | ||
{ | ||
var member = G.Members!.FirstOrDefault(x => x.StudentId == user!.Id); | ||
if (member == null) | ||
{ | ||
MembersNotFound.Add(memberId, "Member Not Found in Group"); | ||
} | ||
else | ||
{ | ||
G.Members!.Remove(member); | ||
groupRepository.Update(G); | ||
try | ||
{ | ||
await unitOfWork.SaveChangesAsync(cancellationToken); | ||
} | ||
catch | ||
{ | ||
MembersNotFound.Add(memberId, "Member already removed from group"); | ||
} | ||
} | ||
} | ||
|
||
} | ||
var returns = string.Join(", ", MembersNotFound.Select(x => $"MemberId: {x.Key}, Error: {x.Value}")); | ||
return MembersNotFound.Count > 0 ? Result.Failure(new Error("422", returns)) : Result.Success(); | ||
|
||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
MBS_COMMAND.Application/UserCases/Commands/Groups/RemoveMemberFromGroupCommandHandler.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,32 @@ | ||
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 sealed class RemoveMemberFromGroupCommandHandler(IRepositoryBase<Group, Guid> groupRepository, IRepositoryBase<User, Guid> userRepository, IUnitOfWork unitOfWork, ICurrentUserService currentUserService) : ICommandHandler<Command.RemoveMemberFromGroup> | ||
{ | ||
public async Task<Result> Handle(Command.RemoveMemberFromGroup request, CancellationToken cancellationToken) | ||
{ | ||
var CurrentUserId = currentUserService.UserId; | ||
|
||
var U = await userRepository.FindByIdAsync(request.MemberId); | ||
if (U == null) | ||
return Result.Failure(new Error("404", "User Not Found")); | ||
var G = await groupRepository.FindSingleAsync(x => x.Id == request.GroupId, cancellationToken); | ||
if (G == null) | ||
return Result.Failure(new Error("404", "Group Not Found")); | ||
if (CurrentUserId != null && G.LeaderId.ToString() == CurrentUserId) | ||
return Result.Failure(new Error("403", "Leader cannot be removed from group")); | ||
var member = G.Members!.FirstOrDefault(x => x.StudentId == U.Id); | ||
if (member == null) | ||
return Result.Failure(new Error("422", "Member are not found in group")); | ||
G.Members!.Remove(member); | ||
groupRepository.Update(G); | ||
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
7 changes: 7 additions & 0 deletions
7
MBS_COMMAND.Domain/Abstractions/Repositories/ICurrentUserService.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,7 @@ | ||
namespace MBS_COMMAND.Domain.Abstractions.Repositories; | ||
|
||
public interface ICurrentUserService | ||
{ | ||
string? UserId { get; } | ||
string? UserName { get; } | ||
} |
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
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
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
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
Oops, something went wrong.