64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using API.Contracts.Profiles.Responses;
|
|
using AutoMapper;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using RecNet.Application.Profiles;
|
|
using RecNet.Domain.Repositories;
|
|
using LoginRequest = API.Contracts.Profiles.Requests.LoginRequest;
|
|
using Profile = RecNet.Domain.Entities.Profiles.Profile;
|
|
|
|
namespace API.Controllers.Profiles.V1;
|
|
|
|
[Route("api/[controller]/v1")]
|
|
[ApiController]
|
|
public class ProfilesController(
|
|
IProfileRepository profileRepository,
|
|
IMapper mapper) : ControllerBase
|
|
{
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<ActionResult<ProfileDTO>> GetProfile(
|
|
Guid id,
|
|
CancellationToken ct)
|
|
{
|
|
var profile = await profileRepository.GetByIdAsync(id, ct);
|
|
if (profile == null)
|
|
return NotFound();
|
|
|
|
return mapper.Map<ProfileDTO>(profile);
|
|
}
|
|
|
|
[HttpGet("bulk")]
|
|
public async Task<IReadOnlyList<ProfileDTO>> GetBulkProfiles(
|
|
[FromQuery(Name = "id")] List<Guid> ids,
|
|
CancellationToken ct)
|
|
{
|
|
var profiles = await profileRepository.GetByIdsAsync(ids, ct);
|
|
|
|
return mapper.Map<List<ProfileDTO>>(profiles);
|
|
}
|
|
|
|
// TODO: Implement
|
|
[HttpPost("login")]
|
|
public async Task<ActionResult<LoginResponse>> Login(
|
|
[FromBody] LoginRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
var profile = await profileRepository.GetByPlatform(request.PlatformType, request.PlatformId, ct);
|
|
if (profile is null)
|
|
{
|
|
profile = Profile.Create(request.Username, request.PlatformType, request.PlatformId);
|
|
await profileRepository.AddAsync(profile, ct);
|
|
}
|
|
|
|
profile.AddDeviceId(request.DeviceId);
|
|
|
|
if (request.Username != profile.Name)
|
|
profile.SetName(request.Username);
|
|
|
|
await profileRepository.SaveChangesAsync(ct);
|
|
|
|
return new LoginResponse
|
|
{
|
|
Profile = mapper.Map<ProfileDTO>(profile)
|
|
};
|
|
}
|
|
} |