72 lines
3.1 KiB
C#
72 lines
3.1 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
|
|
{
|
|
private readonly IStringLocalizer<Resource> _localizer;
|
|
public UserService(IUserRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper) : base(repository, mapper)
|
|
{
|
|
_localizer = localizer;
|
|
}
|
|
|
|
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.Id))
|
|
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);
|
|
}
|
|
}
|
|
} |