feat(cqrs): migrate Sites, Auth, Users, Cameras to CQRS
- Sites: CreateSite, UpdateSite, DeleteSite, GenerateToken commands + handlers - Auth: Login, Register, RefreshToken, 2FA, ChangePassword, Logout commands - Users: CRUD, roles, sites, lock/unlock, reset-password commands + queries - Cameras: CRUD, SetCredentials commands + queries - Add IApplicationDbContext abstraction for Clean Architecture - Add ICredentialProtector, IJwtService interfaces to Application layer - 0 build errors, 0 warnings
This commit is contained in:
1 parent
649af93f56
commit
11f7fd8d20
83 files changed
+1991
-21
No files matched your search
@@ -0,0 +1,39 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.Common.Persistence;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
using MediatR;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Heimdall.Application.Users.Commands.UpdateUser;
|
||||
|
||||
public class UpdateUserHandler : IRequestHandler<UpdateUserCommand, ServiceResult<UserResponse>>
|
||||
{
|
||||
private readonly UserManager<AppUser> _userManager;
|
||||
private readonly IUserRepository _repository;
|
||||
|
||||
public UpdateUserHandler(UserManager<AppUser> userManager, IUserRepository repository)
|
||||
{
|
||||
_userManager = userManager;
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult<UserResponse>> Handle(UpdateUserCommand command, CancellationToken ct)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(command.Id.ToString());
|
||||
if (user is null)
|
||||
return ServiceResult<UserResponse>.NotFound("User not found");
|
||||
|
||||
user.DisplayName = command.DisplayName ?? user.DisplayName;
|
||||
user.Theme = command.Theme ?? user.Theme;
|
||||
user.Language = command.Language ?? user.Language;
|
||||
|
||||
var result = await _userManager.UpdateAsync(user);
|
||||
if (!result.Succeeded)
|
||||
return ServiceResult<UserResponse>.Failure(
|
||||
string.Join(", ", result.Errors.Select(e => e.Description)));
|
||||
|
||||
var response = await _repository.GetByIdAsync(user.Id, ct);
|
||||
return ServiceResult<UserResponse>.Success(response!);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user