Files
IM_NEW/User.WebApi/Applications/User/UserService.cs
T

65 lines
2.2 KiB
C#

using AutoMapper;
using IdentityService.Domain;
using IdentityService.Infrastructure;
using IdentityService.WebApi.Applications.Dtos.Common;
using IM.Commons;
namespace IdentityService.WebApi.Applications.User
{
public class UserService
{
private readonly IIdRepository repository;
private readonly IMapper mapper;
private readonly UserDbContext userDb;
public UserService(IIdRepository repository, IMapper mapper,
UserDbContext userDbContext
)
{
this.repository = repository;
this.mapper = mapper;
userDb = userDbContext;
}
public async Task<Result<UserResponse?>> GetUserInfoAsync(Guid userId)
{
var user = await repository.FindByIdAsync(userId);
if (user is null)
{
return Result<UserResponse?>.Fail(ResultCode.USER_NOT_FOUND);
}
return Result<UserResponse?>.Success(mapper.Map<UserResponse>(user));
}
public async Task<Result<UserResponse?>> GetUserInfoByUnameAsync(string username)
{
var user = await repository.FindByUserNameAsync(username);
if (user is null)
{
return Result<UserResponse?>.Fail(ResultCode.USER_NOT_FOUND);
}
return Result<UserResponse?>.Success(mapper.Map<UserResponse>(user));
}
public async Task<Result<UserResponse>> UpdateAsync(UserUpdateCommand command)
{
var user = await repository.FindByIdAsync(command.UserId);
if (user is null)
{
return Result<UserResponse>.Fail(ResultCode.USER_NOT_FOUND);
}
user.Update(command.NickName, command.Region, command.Avatar, command.Description);
return Result<UserResponse>.Success(mapper.Map<UserResponse>(user));
}
public async Task<Result<List<UserResponse>>> GetUsersByIdsAsync(IEnumerable<Guid> ids)
{
var specification = new UserResponseFindSpecification(ids, mapper);
var users = await repository.GetUsersAsync(specification);
return Result<List<UserResponse>>.Success([.. users]);
}
}
}