-
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
Showing
21 changed files
with
272 additions
and
53 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
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(); | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
MBS_COMMAND.Application/UserCases/Commands/MentorSkills/CreateMentorSkillsCommandHandler.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,22 @@ | ||
using MBS_AUTHORIZATION.Domain.Entities; | ||
using MBS_COMMAND.Domain.Abstractions.Aggregates; | ||
using MBS_COMMAND.Domain.Abstractions.Entities; | ||
using MBS_COMMAND.Domain.Abstractions.Entities; | ||
|
||
namespace MBS_COMMAND.Domain.Entities; | ||
|
||
public class Group : AggregateRoot<Guid>, IAuditableEntity | ||
public class Group : Entity<Guid>, IAuditableEntity | ||
{ | ||
public string Name { get; private set; } | ||
public Guid? MentorId { get; private set; } | ||
public string Name { get; set; } | ||
public Guid? MentorId { get; set; } | ||
public virtual User? Mentor { get; set; } | ||
|
||
public Guid? LeaderId { get; set; } | ||
public virtual User? Leader { get; set; } | ||
public string Stack { get; private set; } | ||
public Guid? ProjectId { get; private set; } | ||
public string Stack { get; set; } | ||
public Guid? ProjectId { get; set; } | ||
public virtual Project? Project { get; set; } | ||
public DateTimeOffset CreatedOnUtc { get; set; } | ||
public DateTimeOffset? ModifiedOnUtc { get; set; } | ||
public virtual ICollection<Group_Student_Mapping>? Members { get; set; } = []; | ||
|
||
|
||
|
||
public Group(string name, string stack, Guid? mentorId) | ||
{ | ||
Name = name; | ||
Stack = stack; | ||
MentorId = mentorId; | ||
} | ||
|
||
public static Group Create(string name, string stack, Guid? mentorId) | ||
{ | ||
var G = new Group(name, stack, mentorId); | ||
//G.RaiseDomainEvent(new Contract.Services.Groups.DomainEvents.GroupCreated(Guid.NewGuid(), G.Id, G.Name, G.MentorId, G.Stack)); | ||
return G; | ||
} | ||
|
||
} | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
using MBS_COMMAND.Domain.Abstractions.Entities; | ||
using MBS_COMMAND.Domain.Abstractions.Aggregates; | ||
using MBS_COMMAND.Domain.Abstractions.Entities; | ||
using MBS_COMMAND.Domain.Entities; | ||
|
||
namespace MBS_AUTHORIZATION.Domain.Entities; | ||
namespace MBS_COMMAND.Domain.Entities; | ||
|
||
public class User : Entity<Guid>, IAuditableEntity | ||
public class User : AggregateRoot<Guid>, IAuditableEntity | ||
{ | ||
public string Email { get; set; } | ||
public string? FullName { get; set; } | ||
public string Password { get; set; } | ||
public int Role { get; set; } | ||
public int Points { get; set; } | ||
public int Status { get; set; } | ||
public int Status { get; set; } //0 Not Active, 1 Active, 2 Blocked | ||
public Guid? MentorId { get; set; } | ||
public bool IsFirstLogin { get; set; } = true; | ||
public virtual User? Mentor { get; set; } | ||
public DateTimeOffset CreatedOnUtc { get; set; } | ||
public DateTimeOffset? ModifiedOnUtc { get; set; } | ||
|
||
public virtual ICollection<Group_Student_Mapping>? Groups { get; set; } = []; | ||
|
||
|
||
|
||
public virtual IReadOnlyCollection<MentorSkills> MentorSkillsList { get; set; } = default!; | ||
} |
Oops, something went wrong.