Files
tiku-backend.net/Tiku.Api/Controllers/MeController.cs
2026-07-26 12:55:31 +08:00

69 lines
1.9 KiB
C#

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Tiku.Application.Security;
using Tiku.Domain.Tenancy;
using Tiku.Infrastructure.Persistence;
namespace Tiku.Api.Controllers;
[ApiController]
[Authorize(Policy = TikuPolicies.AuthenticatedUser)]
[Route("api/me")]
public sealed class MeController(
ICurrentUser currentUser,
TikuDbContext dbContext) : ControllerBase
{
[HttpGet]
public async Task<ActionResult<MeResponse>> Get(CancellationToken cancellationToken)
{
if (currentUser.UserId is null)
{
return Unauthorized();
}
var user = await dbContext.Users.FindAsync([currentUser.UserId.Value], cancellationToken);
if (user is null)
{
return Unauthorized();
}
var memberships = await dbContext.TenantMemberships
.Where(membership =>
membership.UserId == user.Id &&
membership.Status == MembershipStatus.Active)
.Join(
dbContext.Tenants,
membership => membership.TenantId,
tenant => tenant.Id,
(membership, tenant) => new TenantMembershipResponse(
tenant.Id,
tenant.Name,
tenant.Slug,
membership.Role,
membership.Status))
.ToArrayAsync(cancellationToken);
return Ok(new MeResponse(
user.Id,
user.Phone,
user.Email,
user.Name,
memberships));
}
}
public sealed record MeResponse(
Guid UserId,
string? Phone,
string? Email,
string? Name,
IReadOnlyCollection<TenantMembershipResponse> Tenants);
public sealed record TenantMembershipResponse(
Guid TenantId,
string TenantName,
string TenantSlug,
TenantRole Role,
MembershipStatus Status);