Add application services for profiles and neutrino

This commit is contained in:
Holden
2026-06-19 12:57:31 -05:00
parent 3eebfc9ba2
commit 0b81cb046a
25 changed files with 274 additions and 125 deletions

View File

@@ -0,0 +1,8 @@
namespace RecNet.Application.Profiles;
public interface IProfileService
{
Task<ProfileDTO?> GetProfileAsync(Guid profileId, CancellationToken ct = default);
Task<IReadOnlyList<ProfileDTO>> GetProfilesAsync(List<Guid> profileIds, CancellationToken ct = default);
Task<LoginProfileResult> LoginAsync(LoginProfileCommand command, CancellationToken ct = default);
}

View File

@@ -0,0 +1,9 @@
using RecNet.Domain.Common;
namespace RecNet.Application.Profiles;
public sealed record LoginProfileCommand(
string DeviceId,
PlatformType PlatformType,
string PlatformId,
string PlatformAuthentication);

View File

@@ -0,0 +1,33 @@
namespace RecNet.Application.Profiles;
public class LoginProfileResult
{
public bool Succeeded { get; set; }
public string? Message { get; set; }
public ProfileDTO? Profile { get; set; }
public string? AccessToken { get; set; }
public int ExpiresIn { get; set; }
public static LoginProfileResult Success(ProfileDTO profile, string accessToken, int expiresIn)
{
return new LoginProfileResult
{
Succeeded = true,
Profile = profile,
AccessToken = accessToken,
ExpiresIn = expiresIn
};
}
public static LoginProfileResult Fail(string? message = null)
{
return new LoginProfileResult
{
Succeeded = false,
Message = message
};
}
}

View File

@@ -1,12 +1,8 @@
using System.Text.Json.Serialization;
namespace RecNet.Application.Profiles;
public class ProfileDTO
{
[JsonPropertyName("ProfileId")]
public Guid ProfileId { get; init; }
[JsonPropertyName("Name")]
public required string Name { get; init; }
}
}

View File

@@ -0,0 +1,69 @@
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));
}