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 GetProfileAsync(Guid profileId, CancellationToken ct = default) { var profile = await profileRepository.GetByIdAsync(profileId, ct); return profile is null ? null : mapper.Map(profile); } public async Task> GetProfilesAsync( List profileIds, CancellationToken ct = default) { var profiles = await profileRepository.GetByIdsAsync(profileIds, ct); return mapper.Map>(profiles); } public async Task 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(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)); }