users/src/Nocr.Users.AppServices/Users/Services/UsersService.cs
2024-03-23 09:59:37 +04:00

47 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Nocr.Users.Api.Contracts.Users.Dto;
using Nocr.Users.AppServices.Users.Repositories;
namespace Nocr.Users.AppServices.Users.Services;
public sealed class UsersService : IUsersService
{
private readonly IUsersRepository _repository;
public UsersService(IUsersRepository repository)
{
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
}
public async Task<long> Create(string username, string? telegramUsername, string? email, long? telegramId,
CancellationToken cancellationToken = default)
{
var identities = new List<UserIdentity>();
if (!string.IsNullOrWhiteSpace(email))
identities.Add(UserIdentity.Email(email));
if (!string.IsNullOrWhiteSpace(telegramUsername))
identities.Add(UserIdentity.TelegramUsername(telegramUsername));
if (telegramId.HasValue)
identities.Add(UserIdentity.TelegramId(telegramId.Value.ToString()));
// TODO: ToArray = грубо
var user = User.Initialize(username, identities.ToArray());
return await _repository.Create(user, cancellationToken);
}
public async Task<UserData?> GetById(long id, CancellationToken cancellationToken = default)
{
var user = await _repository.GetUserById(id, cancellationToken);
if (user == null)
return null;
return new UserData
{
Id = user.Id,
Username = user.Username,
};
}
}