Developer 02 0abdbfa705 Refaktorisierung der Lokalisierung und DTO-Integration
- Ersetzung von ITranslateService durch IStringLocalizer<X> für verbesserte Lokalisierung.
- Aktualisierung der DTO-Klassen entsprechend der neuesten Core.DTO-Struktur.
- Integration der neuen Klassen Result und DataResult aus Core.DTO für standardisierte Serviceantworten.
2024-05-02 17:36:53 +02:00

70 lines
3.0 KiB
C#

using AutoMapper;
using DigitalData.Core.Application;
using DigitalData.Core.DTO;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.User;
using DigitalData.UserManager.Domain.Entities;
using DigitalData.UserManager.Infrastructure.Contracts;
using Microsoft.Extensions.Localization;
namespace DigitalData.UserManager.Application.Services
{
public class UserService : CRUDService<IUserRepository, UserCreateDto, UserReadDto, UserUpdateDto, User, int>, IUserService
{
public UserService(IUserRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, localizer, mapper)
{
}
public async Task<DataResult<IEnumerable<UserReadDto>>> ReadByModuleIdAsync(int moduleId)
{
var users = await _repository.ReadByModuleIdAsync(moduleId);
IEnumerable<UserReadDto> readDTOs = _mapper.MapOrThrow<IEnumerable<UserReadDto>>(users);
return Result.Success(readDTOs);
}
public async Task<DataResult<IEnumerable<UserReadDto>>> ReadByGroupIdAsync(int groupId)
{
var users = await _repository.ReadByGroupIdAsync(groupId);
IEnumerable<UserReadDto> readDTOs = _mapper.MapOrThrow<IEnumerable<UserReadDto>>(users);
return Result.Success(readDTOs);
}
public async Task<DataResult<IEnumerable<UserReadDto>>> ReadUnassignedByModuleIdAsync(int moduleId)
{
var users = await _repository.ReadUnassignedByModuleIdAsync(moduleId);
IEnumerable<UserReadDto> readDTOs = _mapper.MapOrThrow<IEnumerable<UserReadDto>>(users);
return Result.Success(readDTOs);
}
public async Task<DataResult<IEnumerable<UserReadDto>>> ReadUnassignedByGroupIdAsync(int groupId)
{
var users = await _repository.ReadUnassignedByGroupIdAsync(groupId);
IEnumerable<UserReadDto> readDTOs = _mapper.MapOrThrow<IEnumerable<UserReadDto>>(users);
return Result.Success(readDTOs);
}
public async Task<DataResult<int>> CreateAsync(UserPrincipalDto upDto)
{
var user = _mapper.MapOrThrow<User>(upDto);
if (await HasEntity(user.Guid))
return Result.Fail<int>().Message(_localizer[Key.UserAlreadyExists]);
var createdUser = await _repository.CreateAsync(user);
if (createdUser is null)
return Result.Fail<int>();
else
return Result.Success(KeyValueOf(createdUser));
}
public async Task<DataResult<UserReadDto>> ReadByUsernameAsync(string username)
{
var user = await _repository.ReadByUsernameAsync(username);
if (user is null)
return Result.Fail<UserReadDto>().Message(_localizer[Key.UserNotFoundInLocalDB]);
var userDto = _mapper.MapOrThrow<UserReadDto>(user);
return Result.Success(userDto);
}
}
}