Files
TenWholeYears.Server/RecNet.Application/Profiles/ProfileService.cs

69 lines
2.1 KiB
C#

using AutoMapper;
using RecNet.Application.Common.Interfaces;
using RecNet.Domain.Repositories;
using ProfileEntity = RecNet.Domain.Entities.Profiles.Profile;
namespace RecNet.Application.Profiles;
public class ProfileService(
IProfileRepository profileRepository,
ITokenService tokenService,
IMapper mapper) : IProfileService
{
public async Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default)
{
var profile = await profileRepository.GetByIdAsync(profileId, ct);
return profile is null
? null
: mapper.Map<ProfileDTO>(profile);
}
public async Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(
List<Guid> profileIds,
CancellationToken ct = default)
{
var profiles = await profileRepository.GetByIdsAsync(profileIds, ct);
return mapper.Map<List<ProfileDTO>>(profiles);
}
public async Task<LoginProfileResult> LoginAsync(
LoginProfileCommand command,
CancellationToken ct = default)
{
var profile = await profileRepository.GetByPlatform(
command.PlatformType,
command.PlatformId,
ct);
if (profile is null)
{
profile = ProfileEntity.Create(
GenerateRandomName(),
command.PlatformType,
command.PlatformId);
await profileRepository.AddAsync(profile, ct);
}
profile.AddDeviceId(command.DeviceId);
await profileRepository.SaveChangesAsync(ct);
if (profile.IsBanned)
return LoginProfileResult.Fail("Profile is banned");
var token = tokenService.GenerateProfileToken(profile);
return LoginProfileResult.Success(
profile: mapper.Map<ProfileDTO>(profile),
accessToken: token.AccessToken,
expiresIn: token.ExpiresIn
);
}
// Temporary until platform/user-name verification becomes a real use case.
private static string GenerateRandomName()
=> string.Concat("rr", Guid.NewGuid().ToString("N").AsSpan(0, 18));
}