ref: project structure

This commit is contained in:
2026-08-19 23:25:08 +07:00
parent 05a93cfc09
commit a1fe31cf70
155 changed files with 3339 additions and 1007 deletions
+58
View File
@@ -0,0 +1,58 @@
using AutoMapper;
using MediatR;
using mws.backend.dotnet.application.Auth;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Permissions;
using mws.backend.dotnet.domain.Roles;
using mws.backend.dotnet.domain.Users;
namespace mws.backend.dotnet.application.Users;
public record CreateUserCommand(Guid ActorUserId, CreateUserRequest Request) : IRequest<UserDto>;
public class CreateUserHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions, IMapper mapper)
: IRequestHandler<CreateUserCommand, UserDto>
{
private const string Screen = "users";
public async Task<UserDto> Handle(CreateUserCommand command, CancellationToken ct)
{
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Create, ct);
var request = command.Request;
var username = request.Username.Trim();
if (string.IsNullOrWhiteSpace(username) || string.IsNullOrWhiteSpace(request.Password))
{
throw new BadRequestException("Username and password are required");
}
if (await uow.Users.ExistsByUsernameAsync(username, ct))
{
throw new BadRequestException("Username already exists");
}
Role? role = null;
if (request.RoleId.HasValue)
{
role = await uow.Roles.GetByIdAsync(request.RoleId.Value, ct)
?? throw new BadRequestException("Role not found");
}
var now = DateTime.UtcNow;
var user = new User
{
Id = Guid.NewGuid(),
Username = username,
PasswordHash = passwordHasher.Hash(request.Password),
DisplayName = request.DisplayName.Trim(),
IsActive = true,
CreatedAt = now,
UpdatedAt = now,
UserRoles = role != null ? new List<UserRole> { new UserRole { RoleId = role.Id } } : new List<UserRole>()
};
uow.Users.Add(user);
await uow.SaveChangesAsync(ct);
return mapper.Map<UserDto>(user);
}
}
+34
View File
@@ -0,0 +1,34 @@
using MediatR;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Permissions;
namespace mws.backend.dotnet.application.Users;
public record DeleteUserCommand(Guid ActorUserId, Guid Id) : IRequest;
public class DeleteUserHandler(IUnitOfWork uow, IPermissionService permissions) : IRequestHandler<DeleteUserCommand>
{
private const string Screen = "users";
public async Task Handle(DeleteUserCommand command, CancellationToken ct)
{
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Delete, ct);
var user = await uow.Users.GetByIdAsync(command.Id, ct)
?? throw new NotFoundException("User not found");
var soleOwnerProjectIds = await uow.Projects.GetOwnedProjectIdsAsync(command.Id, ct);
foreach (var projectId in soleOwnerProjectIds)
{
var ownerCount = await uow.Projects.CountOwnersAsync(projectId, ct);
if (ownerCount <= 1)
{
throw new BadRequestException("Cannot delete a user that is the sole owner of a project");
}
}
uow.Users.Remove(user);
await uow.SaveChangesAsync(ct);
}
}
@@ -0,0 +1,31 @@
using MediatR;
using mws.backend.dotnet.application.Auth;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Permissions;
namespace mws.backend.dotnet.application.Users;
public record ResetPasswordCommand(Guid ActorUserId, Guid Id, ResetPasswordRequest Request) : IRequest;
public class ResetPasswordHandler(IUnitOfWork uow, IPasswordHasher passwordHasher, IPermissionService permissions)
: IRequestHandler<ResetPasswordCommand>
{
private const string Screen = "users";
public async Task Handle(ResetPasswordCommand command, CancellationToken ct)
{
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
if (string.IsNullOrWhiteSpace(command.Request.NewPassword))
{
throw new BadRequestException("New password is required");
}
var user = await uow.Users.GetByIdAsync(command.Id, ct)
?? throw new NotFoundException("User not found");
user.PasswordHash = passwordHasher.Hash(command.Request.NewPassword);
user.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
}
}
+36
View File
@@ -0,0 +1,36 @@
using AutoMapper;
using MediatR;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Permissions;
namespace mws.backend.dotnet.application.Users;
public record UpdateUserCommand(Guid ActorUserId, Guid Id, UpdateUserRequest Request) : IRequest<UserDto>;
public class UpdateUserHandler(IUnitOfWork uow, IPermissionService permissions, IMapper mapper)
: IRequestHandler<UpdateUserCommand, UserDto>
{
private const string Screen = "users";
public async Task<UserDto> Handle(UpdateUserCommand command, CancellationToken ct)
{
await permissions.EnsureAsync(command.ActorUserId, Screen, PermissionAction.Edit, ct);
var user = await uow.Users.GetByIdWithRoleAsync(command.Id, ct)
?? throw new NotFoundException("User not found");
var request = command.Request;
if (request.RoleId.HasValue)
{
var role = await uow.Roles.GetByIdAsync(request.RoleId.Value, ct)
?? throw new BadRequestException("Role not found");
}
user.DisplayName = request.DisplayName.Trim();
user.IsActive = request.IsActive;
user.UpdatedAt = DateTime.UtcNow;
await uow.SaveChangesAsync(ct);
return mapper.Map<UserDto>(user);
}
}
+48
View File
@@ -0,0 +1,48 @@
using mws.backend.dotnet.application.Permissions;
namespace mws.backend.dotnet.application.Users;
public class CreateUserRequest
{
public string Username { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public Guid? RoleId { get; set; }
}
public class UpdateUserRequest
{
public string DisplayName { get; set; } = string.Empty;
public Guid? RoleId { get; set; }
public bool IsActive { get; set; } = true;
}
public class ResetPasswordRequest
{
public string NewPassword { get; set; } = string.Empty;
}
public class UserDto
{
public Guid Id { get; set; }
public string Username { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public Guid RoleId { get; set; }
public string RoleName { get; set; } = string.Empty;
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
}
public class UserRoleDetailDto
{
public Guid UserId { get; set; }
public string Username { get; set; } = string.Empty;
public string DisplayName { get; set; } = string.Empty;
public List<RoleDto> AssignedRoles { get; set; } = [];
public List<RoleDto> UnassignedRoles { get; set; } = [];
}
public class AssignRoleRequest
{
public Guid RoleId { get; set; }
}
+28
View File
@@ -0,0 +1,28 @@
using AutoMapper;
using MediatR;
using mws.backend.dotnet.application.Common;
using mws.backend.dotnet.application.Permissions;
namespace mws.backend.dotnet.application.Users;
public record GetUsersQuery(Guid ActorUserId, string? Term, int Page, int PageSize) : IRequest<PagedResult<UserDto>>;
public class GetUsersHandler(IUnitOfWork uow, IPermissionService permissions, IMapper mapper)
: IRequestHandler<GetUsersQuery, PagedResult<UserDto>>
{
private const string Screen = "users";
public async Task<PagedResult<UserDto>> Handle(GetUsersQuery query, CancellationToken ct)
{
await permissions.EnsureAsync(query.ActorUserId, Screen, PermissionAction.View, ct);
var users = await uow.Users.SearchWithRolePagedAsync(query.Term, query.Page, query.PageSize, ct);
return new PagedResult<UserDto>
{
Items = mapper.Map<List<UserDto>>(users.Items),
TotalCount = users.TotalCount,
Page = users.Page,
PageSize = users.PageSize,
};
}
}