1
This commit is contained in:
25
Yes.Application/Admins/Users/CreateUserCommandHandler.cs
Normal file
25
Yes.Application/Admins/Users/CreateUserCommandHandler.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
public record CreateUserCommand(
|
||||
string Name,
|
||||
string Email,
|
||||
string NickName,
|
||||
string Password
|
||||
) : IRequest<CreateUserCommandResponse>;
|
||||
|
||||
public record CreateUserCommandResponse(int Id);
|
||||
|
||||
public class CreateUserCommandHandler(BlogDbContext db) : IRequestHandler<CreateUserCommand, CreateUserCommandResponse>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
|
||||
public async Task<CreateUserCommandResponse> Handle(CreateUserCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = UserEntity.Create(request.Name, request.Email, request.NickName, request.Password);
|
||||
await _db.Users.AddAsync(user);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new CreateUserCommandResponse(user.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Yes.Application/Admins/Users/DeleteUserCommandHandler.cs
Normal file
27
Yes.Application/Admins/Users/DeleteUserCommandHandler.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
public record DeleteUserCommand(int Id) : IRequest<Unit>;
|
||||
|
||||
public class DeleteUserCommandHandler(BlogDbContext db) : IRequestHandler<DeleteUserCommand, Unit>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
|
||||
public async Task<Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = await _db.Users.FindAsync(request.Id);
|
||||
if (user == null)
|
||||
{
|
||||
throw new UserNotExistsException(request.Id);
|
||||
}
|
||||
if (user.IsSystemUser())
|
||||
{
|
||||
throw new RemoveSystemUserException();
|
||||
}
|
||||
|
||||
_db.Users.Remove(user);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new Unit();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Yes.Application/Admins/Users/GetProfileQueryHandler.cs
Normal file
36
Yes.Application/Admins/Users/GetProfileQueryHandler.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
|
||||
public record GetProfileQuery() : IRequest<GetProfileQueryResponse>;
|
||||
|
||||
public record GetProfileQueryResponse(
|
||||
int Id,
|
||||
string Name,
|
||||
string Email,
|
||||
string NickName,
|
||||
string Url
|
||||
);
|
||||
|
||||
public class GetProfileQueryHandler(BlogDbContext db, IMapper mapper, IIdentityContext identityContext) : IRequestHandler<GetProfileQuery, GetProfileQueryResponse>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IMapper _mapper = mapper;
|
||||
private readonly IIdentityContext _identityContext = identityContext;
|
||||
public async Task<GetProfileQueryResponse> Handle(GetProfileQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = _identityContext.Id;
|
||||
var user = await _db.Users.FindAsync(userId);
|
||||
var response = _mapper.Map<GetProfileQueryResponse>(user);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
internal class GetMyProfileMappingProfile : IRegister
|
||||
{
|
||||
public void Register(TypeAdapterConfig config)
|
||||
{
|
||||
config.ForType<UserEntity, GetProfileQueryResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Yes.Application/Admins/Users/GetUserQueryHandler.cs
Normal file
35
Yes.Application/Admins/Users/GetUserQueryHandler.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
|
||||
public record GetUserQuery(int Id) : IRequest<GetUserQueryResponse>;
|
||||
|
||||
public record GetUserQueryResponse(
|
||||
int Id,
|
||||
string Name,
|
||||
string Email,
|
||||
string NickName,
|
||||
bool IsSystem
|
||||
);
|
||||
|
||||
public class GetUserQueryHandler(BlogDbContext db, IMapper mapper) : IRequestHandler<GetUserQuery, GetUserQueryResponse>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IMapper _mapper = mapper;
|
||||
public async Task<GetUserQueryResponse> Handle(GetUserQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var user = await _db.Users.FindAsync(request.Id);
|
||||
var response = _mapper.Map<GetUserQueryResponse>(user);
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
internal class GetUserMappingProfile : IRegister
|
||||
{
|
||||
public void Register(TypeAdapterConfig config)
|
||||
{
|
||||
config.ForType<UserEntity, GetUserQueryResponse>();
|
||||
}
|
||||
}
|
||||
}
|
||||
36
Yes.Application/Admins/Users/GetUsersQueryHandler.cs
Normal file
36
Yes.Application/Admins/Users/GetUsersQueryHandler.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
|
||||
public record GetUsersQuery() : IRequest<List<GetUsersQueryResponse>>;
|
||||
|
||||
public record GetUsersQueryResponse(
|
||||
int Id,
|
||||
string Name,
|
||||
string Email,
|
||||
string NickName,
|
||||
DateTime? RegDate,
|
||||
bool IsSystem
|
||||
);
|
||||
|
||||
public class GetUsersQueryHandler(BlogDbContext db) : IRequestHandler<GetUsersQuery, List<GetUsersQueryResponse>>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
public async Task<List<GetUsersQueryResponse>> Handle(GetUsersQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
var articles = await _db.Users.Select(x =>
|
||||
new GetUsersQueryResponse
|
||||
(
|
||||
x.Id,
|
||||
x.Name,
|
||||
x.Email,
|
||||
x.NickName,
|
||||
x.RegDate,
|
||||
x.IsSystem
|
||||
)
|
||||
).ToListAsync(cancellationToken);
|
||||
|
||||
return articles;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
Yes.Application/Admins/Users/UpdatePasswordCommandHandler.cs
Normal file
32
Yes.Application/Admins/Users/UpdatePasswordCommandHandler.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
public record UpdatePasswordCommand(
|
||||
string OriginalPassword,
|
||||
string NewPassword
|
||||
) : IRequest<Unit>;
|
||||
|
||||
|
||||
public class UpdatePasswordCommandHandler(BlogDbContext db, IIdentityContext identityContext) : IRequestHandler<UpdatePasswordCommand, Unit>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IIdentityContext _identityContext = identityContext;
|
||||
|
||||
public async Task<Unit> Handle(UpdatePasswordCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = _identityContext.Id;
|
||||
var user = await _db.Users.FindAsync(userId);
|
||||
if (user == null)
|
||||
{
|
||||
throw new InvalidPasswordException();
|
||||
}
|
||||
|
||||
user.CheckPassword(request.OriginalPassword);
|
||||
user.UpdatePassword(request.NewPassword);
|
||||
|
||||
_db.Users.Update(user);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new Unit();
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Yes.Application/Admins/Users/UpdateProfileCommandHandler.cs
Normal file
30
Yes.Application/Admins/Users/UpdateProfileCommandHandler.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
public record UpdateProfileCommand(
|
||||
string Email,
|
||||
string NickName,
|
||||
string Url
|
||||
) : IRequest<Unit>;
|
||||
|
||||
|
||||
public class UpdateProfileCommandHandler(BlogDbContext db, IIdentityContext identityContext) : IRequestHandler<UpdateProfileCommand, Unit>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
private readonly IIdentityContext _identityContext = identityContext;
|
||||
|
||||
public async Task<Unit> Handle(UpdateProfileCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var userId = _identityContext.Id;
|
||||
var user = await _db.Users.FindAsync(userId);
|
||||
if (user == null)
|
||||
{
|
||||
throw new UserNotExistsException();
|
||||
}
|
||||
user.UpdateProfile(request.Email, request.NickName, request.Url);
|
||||
_db.Users.Update(user);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new Unit();
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Yes.Application/Admins/Users/UpdateUserCommandHandler.cs
Normal file
30
Yes.Application/Admins/Users/UpdateUserCommandHandler.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
namespace Yes.Application.Admins.Users
|
||||
{
|
||||
public record UpdateUserCommand(
|
||||
int Id,
|
||||
string Name,
|
||||
string Email,
|
||||
string NickName,
|
||||
string Password
|
||||
) : IRequest<Unit>;
|
||||
|
||||
|
||||
public class UpdateUserCommandHandler(BlogDbContext db) : IRequestHandler<UpdateUserCommand, Unit>
|
||||
{
|
||||
private readonly BlogDbContext _db = db;
|
||||
|
||||
public async Task<Unit> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = await _db.Users.FindAsync(request.Id);
|
||||
if (user == null)
|
||||
{
|
||||
throw new UserNotExistsException(request.Id);
|
||||
}
|
||||
user.Update(request.Name, request.Email, request.NickName, request.Password);
|
||||
_db.Users.Update(user);
|
||||
await _db.SaveChangesAsync();
|
||||
|
||||
return new Unit();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user