TekH e9527ca61e Refactor namespaces and update DTO structures
Updated namespaces for DTOs and services to improve project organization. Marked several interfaces as obsolete in favor of MediatR for better request handling. Simplified `BaseUpdateDto` and other DTOs by removing `IUnique<int>` implementation. Changed return types of `CreateAsync` methods to return corresponding read DTOs. Removed reference to `DigitalData.Core.DTO` in the project file, reflecting a shift in architecture for maintainability.
2025-06-25 17:14:24 +02:00

76 lines
3.2 KiB
C#

using AutoMapper;
using DigitalData.UserManager.Application.Contracts;
using DigitalData.UserManager.Application.DTOs.User;
using DigitalData.UserManager.Domain.Entities;
using DigitalData.UserManager.Application.Contracts.Repositories;
using Microsoft.Extensions.Localization;
using DigitalData.Core.Abstraction.Application.DTO;
namespace DigitalData.UserManager.Application.Services
{
[Obsolete("Use MediatR")]
public class UserService : BaseService<IUserRepository, UserCreateDto, UserReadDto, User>, IUserService
{
private readonly IStringLocalizer<Resource>? _localizer;
public UserService(IUserRepository repository, IMapper mapper, IStringLocalizer<Resource>? localizer = null) : base(repository, mapper)
{
_localizer = localizer;
}
public async Task<DataResult<IEnumerable<UserReadDto>>> ReadByModuleIdAsync(int moduleId)
{
var users = await _repository.ReadByModuleIdAsync(moduleId);
IEnumerable<UserReadDto> readDTOs = _mapper.Map<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.Map<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.Map<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.Map<IEnumerable<UserReadDto>>(users);
return Result.Success(readDTOs);
}
public async Task<DataResult<int>> CreateAsync(UserPrincipalDto upDto)
{
var user = _mapper.Map<User>(upDto);
if (await HasEntity(user.Id))
return Result.Fail<int>().Message(_localizer?[Key.UserAlreadyExists].Value);
//set the user
var current_user = await GetUserAsync();
user.AddedWho = current_user?.Username ?? "UNAUTHORIZED";
var createdUser = await _repository.CreateAsync(user);
if (createdUser is null)
return Result.Fail<int>();
else
return Result.Success(createdUser.Id);
}
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].Value);
var userDto = _mapper.Map<UserReadDto>(user);
return Result.Success(userDto);
}
}
}