161 lines
4.9 KiB
C#
161 lines
4.9 KiB
C#
using AutoMapper;
|
|
using UserManagement.Application.Dtos.Incomming;
|
|
using UserManagement.Application.Dtos.Outgoing;
|
|
using UserManagement.Application.Interfaces;
|
|
using UserManagement.Domain.Entities;
|
|
using UserManagement.Infrastructure.Interfaces;
|
|
|
|
namespace UserManagement.Application.Services
|
|
{
|
|
public class UserService : IUserService
|
|
{
|
|
// CTOR
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IRoleRepository _roleRepository;
|
|
private readonly IMapper _mapper;
|
|
public UserService(IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper)
|
|
{
|
|
_userRepository = userRepository;
|
|
_roleRepository = roleRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
// CREATE
|
|
public async Task<User?> AddUserAsync(CreatingUserDto creatingUserDto)
|
|
{
|
|
var user = _mapper.Map<User>(creatingUserDto);
|
|
|
|
if (!string.IsNullOrEmpty(creatingUserDto.Password))
|
|
{
|
|
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(creatingUserDto.Password);
|
|
}
|
|
|
|
var created = await _userRepository.AddAsync(user);
|
|
|
|
return created;
|
|
}
|
|
|
|
// READ ALL
|
|
public async Task<IEnumerable<ReadingUserDto>> GetAllUsersAsync(bool includeRoles = true)
|
|
{
|
|
var users = await _userRepository.GetAllAsync(includeRoles);
|
|
|
|
if (users == null)
|
|
{
|
|
throw new KeyNotFoundException("Keine Benutzer gefunden!");
|
|
}
|
|
|
|
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
|
|
|
|
return readDto;
|
|
}
|
|
|
|
// READ BY ID
|
|
public async Task<ReadingUserDto> GetUserByIdAsync(int id, bool includeRoles = true)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var user = await _userRepository.GetByIdAsync(id, includeRoles);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException("Benutzer nicht gefunden!");
|
|
}
|
|
|
|
var readDto = _mapper.Map<ReadingUserDto>(user);
|
|
|
|
return readDto;
|
|
}
|
|
|
|
// READ BY USERNAME
|
|
public async Task<ReadingUserDto> GetUserByUsernameAsync(string username, bool includeRoles = true)
|
|
{
|
|
if (string.IsNullOrEmpty(username))
|
|
{
|
|
throw new ArgumentException("Ungültiger Benutzername!");
|
|
}
|
|
|
|
var user = await _userRepository.GetByUsernameAsync(username, includeRoles);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException("Benutzer nicht gefunden!");
|
|
}
|
|
|
|
var readDto = _mapper.Map<ReadingUserDto>(user);
|
|
|
|
return readDto;
|
|
}
|
|
|
|
// READ BY ROLE
|
|
public async Task<IEnumerable<ReadingUserDto>> GetUsersByRoleAsync(string role)
|
|
{
|
|
if (string.IsNullOrEmpty(role))
|
|
{
|
|
throw new ArgumentException("Ungültige Rolle!");
|
|
}
|
|
|
|
var users = await _userRepository.GetByRoleAsync(role);
|
|
|
|
if (users == null)
|
|
{
|
|
throw new KeyNotFoundException("Keine Benutzer in dieser Rolle gefunden!");
|
|
}
|
|
|
|
var readDto = _mapper.Map<IEnumerable<ReadingUserDto>>(users);
|
|
|
|
return readDto;
|
|
}
|
|
|
|
// UPDATE
|
|
public async Task<bool> UpdateUserAsync(UpdatingUserDto updatingUserDto)
|
|
{
|
|
if (updatingUserDto.Id <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var user = await _userRepository.GetByIdAsync(updatingUserDto.Id);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException("Benutzer nicht gefunden!");
|
|
}
|
|
|
|
_mapper.Map(updatingUserDto, user);
|
|
|
|
if (!string.IsNullOrEmpty(updatingUserDto.Password))
|
|
{
|
|
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(updatingUserDto.Password);
|
|
}
|
|
|
|
bool isUpdated = await _userRepository.UpdateAsync(user);
|
|
|
|
return isUpdated;
|
|
}
|
|
|
|
// DELETE
|
|
public async Task<bool> DeleteUserAsync(int id)
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var user = await _userRepository.GetByIdAsync(id);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException("Benutzer nicht gefunden!");
|
|
}
|
|
|
|
bool isDeleted = await _userRepository.DeleteAsync(user);
|
|
|
|
return isDeleted;
|
|
}
|
|
}
|
|
}
|