feat: V2 microservices stack — backend services, gateway, JWT auth

Add full V2 architecture: identity, content, studio (.NET 10) and file,
render, notification, gateway (Go) services with vendored deps, plus DB
migrations, event/API contracts, and an init-db script.

Wire the Next.js frontend to the gateway: server-side JWT auth routes
(login/register/refresh/logout/me), gateway fetch helper, and session/
cookie/jwt helpers under src/lib.

Containerize the stack via docker-compose.v2.yml and per-service
Dockerfiles. Base images resolve through a Nexus mirror (Docker Hub) and
MCR directly; npm/NuGet pull from Nexus groups. Self-host fonts via
next/font/local to avoid Google Fonts (geo-blocked).

Add CI workflow and ignore .env.v2, *.stackdump, and .NET bin/obj.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
soroush.asadi
2026-05-29 23:29:31 +03:30
parent 53ea78a00d
commit 90ac0b81d1
7636 changed files with 3707504 additions and 240 deletions
@@ -0,0 +1,176 @@
using FlatRender.IdentitySvc.Application.Services;
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Requests;
using FlatRender.IdentitySvc.Models.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Route("v1/auth")]
public class AuthController(IAuthService authService) : ControllerBase
{
[HttpPost("register")]
[AllowAnonymous]
[ProducesResponseType(typeof(RegisterResponse), 201)]
public async Task<IActionResult> Register([FromBody] RegisterRequest request)
{
var result = await authService.RegisterAsync(request, GetClientIp());
return StatusCode(201, result);
}
[HttpPost("login")]
[AllowAnonymous]
[ProducesResponseType(typeof(AuthTokensResponse), 200)]
[ProducesResponseType(401)]
[ProducesResponseType(403)]
public async Task<IActionResult> Login([FromBody] LoginRequest request)
{
try
{
var result = await authService.LoginAsync(request, GetClientIp());
return Ok(result);
}
catch (MfaRequiredException mfaEx)
{
return StatusCode(403, new { mfa_required = true, mfa_token = mfaEx.MfaToken });
}
}
[HttpPost("refresh")]
[AllowAnonymous]
[ProducesResponseType(typeof(AuthTokensResponse), 200)]
public async Task<IActionResult> Refresh([FromBody] RefreshTokenRequest request)
{
var result = await authService.RefreshAsync(request.RefreshToken);
return Ok(result);
}
[HttpPost("logout")]
[Authorize]
[ProducesResponseType(204)]
public async Task<IActionResult> Logout()
{
await authService.LogoutAsync(GetSessionId(), GetUserId());
return NoContent();
}
[HttpGet("sessions")]
[Authorize]
[ProducesResponseType(typeof(object), 200)]
public async Task<IActionResult> GetSessions()
{
var sessions = await authService.GetSessionsAsync(GetUserId());
return Ok(new { sessions });
}
[HttpDelete("sessions/{sessionId:guid}")]
[Authorize]
[ProducesResponseType(204)]
public async Task<IActionResult> RevokeSession(Guid sessionId)
{
await authService.RevokeSessionAsync(sessionId, GetUserId());
return NoContent();
}
[HttpPost("verify/email")]
[AllowAnonymous]
public async Task<IActionResult> VerifyEmail([FromBody] VerifyOtpRequest request)
{
var ok = await authService.VerifyEmailAsync(request.Token, request.Code);
return ok ? Ok() : BadRequest(new { error = "Invalid code" });
}
[HttpPost("verify/phone")]
[AllowAnonymous]
public async Task<IActionResult> VerifyPhone([FromBody] VerifyOtpRequest request)
{
var ok = await authService.VerifyPhoneAsync(request.Token, request.Code);
return ok ? Ok() : BadRequest(new { error = "Invalid code" });
}
[HttpPost("password/reset/request")]
[AllowAnonymous]
[ProducesResponseType(202)]
public async Task<IActionResult> PasswordResetRequest([FromBody] PasswordResetRequestDto request)
{
await authService.RequestPasswordResetAsync(request.TenantSlug, request.Email, request.PhoneNumber);
return Accepted();
}
[HttpPost("password/reset/confirm")]
[AllowAnonymous]
public async Task<IActionResult> PasswordResetConfirm([FromBody] PasswordResetConfirmRequest request)
{
await authService.ConfirmPasswordResetAsync(request.Token, request.NewPassword);
return Ok();
}
[HttpPost("password/change")]
[Authorize]
[ProducesResponseType(204)]
public async Task<IActionResult> PasswordChange([FromBody] PasswordChangeRequest request)
{
await authService.ChangePasswordAsync(GetUserId(), request.CurrentPassword, request.NewPassword);
return NoContent();
}
[HttpPost("mfa/setup")]
[Authorize]
[ProducesResponseType(typeof(MfaSetupResponse), 200)]
public async Task<IActionResult> MfaSetup([FromBody] MfaSetupRequest request)
{
var result = await authService.SetupMfaAsync(GetUserId(), request.FactorType, request.Label);
return Ok(result);
}
[HttpPost("mfa/verify")]
[Authorize]
public async Task<IActionResult> MfaVerify([FromBody] MfaVerifyRequest request)
{
var ok = await authService.VerifyMfaAsync(GetUserId(), request.FactorId, request.Code);
return ok ? Ok() : BadRequest(new { error = "Invalid code" });
}
[HttpPost("mfa/challenge")]
[AllowAnonymous]
[ProducesResponseType(typeof(AuthTokensResponse), 200)]
public async Task<IActionResult> MfaChallenge([FromBody] MfaChallengeRequest request)
{
var result = await authService.ChallengeMfaAsync(request.MfaToken, request.Code);
return Ok(result);
}
[HttpPost("push/subscribe")]
[Authorize]
[ProducesResponseType(201)]
public async Task<IActionResult> PushSubscribe([FromBody] PushSubscribeRequest request)
{
await authService.SubscribePushAsync(
GetUserId(), GetTenantId(),
request.Endpoint, request.Keys.P256dh, request.Keys.Auth, request.UserAgent);
return StatusCode(201);
}
[HttpPost("push/unsubscribe")]
[Authorize]
[ProducesResponseType(204)]
public async Task<IActionResult> PushUnsubscribe([FromBody] PushUnsubscribeRequest request)
{
await authService.UnsubscribePushAsync(GetUserId(), request.Endpoint);
return NoContent();
}
// ── Helpers ───────────────────────────────────────────────────────────
private Guid GetUserId() => Guid.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException());
private Guid GetTenantId() => Guid.Parse(User.FindFirst("tenant_id")?.Value
?? throw new UnauthorizedAccessException());
private Guid GetSessionId() => Guid.Parse(User.FindFirst("jti")?.Value ?? Guid.Empty.ToString());
private string? GetClientIp() => HttpContext.Connection.RemoteIpAddress?.ToString();
}
@@ -0,0 +1,48 @@
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Requests;
using FlatRender.IdentitySvc.Models.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Route("v1/discounts")]
[Authorize]
public class DiscountsController(IDiscountService discountService) : ControllerBase
{
[HttpPost("validate")]
[AllowAnonymous]
[ProducesResponseType(typeof(DiscountValidateResponse), 200)]
public async Task<IActionResult> Validate([FromBody] ValidateDiscountRequest request)
{
var tenantId = GetTenantIdOrDefault();
var result = await discountService.ValidateAsync(tenantId, request.Code, request.PlanId);
return Ok(result);
}
[HttpGet]
[ProducesResponseType(typeof(PagedResponse<DiscountResponse>), 200)]
public async Task<IActionResult> List([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
{
var result = await discountService.ListAsync(GetTenantId(), page, pageSize);
return Ok(result);
}
[HttpPost]
[ProducesResponseType(typeof(DiscountResponse), 201)]
public async Task<IActionResult> Create([FromBody] CreateDiscountRequest request)
{
var result = await discountService.CreateAsync(GetTenantId(), request);
return StatusCode(201, result);
}
private Guid GetTenantId() => Guid.Parse(User.FindFirst("tenant_id")?.Value
?? throw new UnauthorizedAccessException());
private Guid GetTenantIdOrDefault()
{
var claim = User.FindFirst("tenant_id")?.Value;
return claim != null ? Guid.Parse(claim) : Guid.Empty;
}
}
@@ -0,0 +1,49 @@
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Authorize]
public class GamificationController(IGamificationService gamificationService) : ControllerBase
{
[HttpGet("v1/quests")]
[ProducesResponseType(typeof(object), 200)]
public async Task<IActionResult> GetQuests()
{
var quests = await gamificationService.GetActiveQuestsAsync(GetUserId(), GetTenantId());
return Ok(new { data = quests });
}
[HttpPost("v1/quests/{questId:guid}/claim")]
[ProducesResponseType(200)]
public async Task<IActionResult> ClaimQuest(Guid questId)
{
await gamificationService.ClaimQuestPrizeAsync(GetUserId(), questId);
return Ok();
}
[HttpGet("v1/gifts/earned")]
[ProducesResponseType(typeof(object), 200)]
public async Task<IActionResult> GetEarnedGifts()
{
var gifts = await gamificationService.GetEarnedGiftsAsync(GetUserId());
return Ok(new { data = gifts });
}
[HttpPost("v1/gifts/earned/{earnedGiftId:guid}/use")]
[ProducesResponseType(200)]
public async Task<IActionResult> UseGift(Guid earnedGiftId)
{
await gamificationService.UseEarnedGiftAsync(GetUserId(), earnedGiftId);
return Ok();
}
private Guid GetUserId() => Guid.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException());
private Guid GetTenantId() => Guid.Parse(User.FindFirst("tenant_id")?.Value
?? throw new UnauthorizedAccessException());
}
@@ -0,0 +1,159 @@
using FlatRender.IdentitySvc.Application.Services;
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Route("v1")]
public class PaymentsController(IPaymentService paymentService) : ControllerBase
{
// ── Helpers ───────────────────────────────────────────────────────────────────
private Guid GetUserId() => Guid.Parse(
User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value
?? throw new UnauthorizedAccessException());
private bool IsAdmin => User.FindFirst("is_admin")?.Value == "true";
// ── Listing ───────────────────────────────────────────────────────────────────
/// <summary>GET /v1/payments — list the caller's payment history</summary>
[Authorize]
[HttpGet("payments")]
public async Task<IActionResult> List(
[FromQuery] int page = 1,
[FromQuery] int page_size = 20)
=> Ok(await paymentService.GetUserPaymentsAsync(GetUserId(), page, page_size));
/// <summary>GET /v1/payments/{id} — get a single payment</summary>
[Authorize]
[HttpGet("payments/{id:guid}")]
public async Task<IActionResult> GetById(Guid id)
=> Ok(await paymentService.GetByIdAsync(id, GetUserId()));
// ── ZarinPal flow ─────────────────────────────────────────────────────────────
/// <summary>
/// GET /v1/payments/gateway/zarinpal?payment_id={id}
/// Initiates a ZarinPal payment and redirects the browser to zarinpal.com.
/// Called from the frontend after PurchasePlan returns the redirect URL.
/// </summary>
[Authorize]
[HttpGet("payments/gateway/zarinpal")]
public async Task<IActionResult> InitiateZarinPal([FromQuery] Guid payment_id)
{
var redirectUrl = await paymentService.InitiateZarinPalAsync(payment_id, GetUserId());
return Redirect(redirectUrl);
}
/// <summary>
/// GET /v1/payments/callback/zarinpal?Authority={a}&amp;Status={s}
/// ZarinPal calls this URL after the user completes (or cancels) payment.
/// Verifies with ZarinPal, activates the plan, then redirects to the frontend.
/// </summary>
[AllowAnonymous]
[HttpGet("payments/callback/zarinpal")]
public async Task<IActionResult> ZarinPalCallback(
[FromQuery] string Authority,
[FromQuery] string Status)
{
var frontendUrl = await paymentService.HandleZarinPalCallbackAsync(Authority, Status);
return Redirect(frontendUrl);
}
// ── SnapPay flow ──────────────────────────────────────────────────────────────
/// <summary>
/// GET /v1/payments/gateway/snappay?payment_id={id}
/// Initiates a SnapPay payment and redirects the browser to snappay.ir.
/// </summary>
[Authorize]
[HttpGet("payments/gateway/snappay")]
public async Task<IActionResult> InitiateSnapPay([FromQuery] Guid payment_id)
{
var redirectUrl = await paymentService.InitiateSnapPayAsync(payment_id, GetUserId());
return Redirect(redirectUrl);
}
/// <summary>
/// GET /v1/payments/callback/snappay?paymentToken={t}&amp;shapSnapStatus={s}
/// SnapPay calls this URL after the user completes (or cancels) payment.
/// Verifies with SnapPay, activates the plan, then redirects to the frontend.
/// </summary>
[AllowAnonymous]
[HttpGet("payments/callback/snappay")]
public async Task<IActionResult> SnapPayCallback(
[FromQuery] string paymentToken,
[FromQuery] string shapSnapStatus)
{
var frontendUrl = await paymentService.HandleSnapPayCallbackAsync(paymentToken, shapSnapStatus);
return Redirect(frontendUrl);
}
// ── Tara flow ─────────────────────────────────────────────────────────────────
/// <summary>
/// GET /v1/payments/gateway/tara?payment_id={id}
/// Initiates a Tara payment and redirects the browser to tara.ir.
/// </summary>
[Authorize]
[HttpGet("payments/gateway/tara")]
public async Task<IActionResult> InitiateTara([FromQuery] Guid payment_id)
{
var redirectUrl = await paymentService.InitiateTaraAsync(payment_id, GetUserId());
return Redirect(redirectUrl);
}
/// <summary>
/// GET /v1/payments/callback/tara?token={t}&amp;status={s}
/// Tara calls this URL after the user completes (or cancels) payment.
/// Verifies with Tara, activates the plan, then redirects to the frontend.
/// </summary>
[AllowAnonymous]
[HttpGet("payments/callback/tara")]
public async Task<IActionResult> TaraCallback(
[FromQuery] string token,
[FromQuery] string status)
{
var frontendUrl = await paymentService.HandleTaraCallbackAsync(token, status);
return Redirect(frontendUrl);
}
// ── Stripe webhook ────────────────────────────────────────────────────────────
/// <summary>
/// POST /v1/payments/webhook/stripe
/// Receives Stripe webhook events. Must be reachable from the public internet.
/// Register this URL in your Stripe dashboard under Developers → Webhooks.
/// </summary>
[AllowAnonymous]
[HttpPost("payments/webhook/stripe")]
public async Task<IActionResult> StripeWebhook()
{
using var reader = new StreamReader(Request.Body);
var payload = await reader.ReadToEndAsync();
var signature = Request.Headers["Stripe-Signature"].ToString();
await paymentService.HandleStripeWebhookAsync(payload, signature);
return Ok();
}
// ── Admin ─────────────────────────────────────────────────────────────────────
/// <summary>
/// POST /v1/admin/payments/{id}/refund
/// Issues a refund for a payment. Admin-only.
/// </summary>
[Authorize]
[HttpPost("admin/payments/{id:guid}/refund")]
public async Task<IActionResult> Refund(Guid id, [FromBody] IssueRefundRequest request)
{
if (!IsAdmin) return Forbid();
var result = await paymentService.IssueRefundAsync(
id, request.AmountMinor, request.Reason, request.RefundTo);
return Ok(result);
}
}
@@ -0,0 +1,46 @@
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Requests;
using FlatRender.IdentitySvc.Models.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Route("v1")]
[Authorize]
public class PlansController(IPlanService planService) : ControllerBase
{
[HttpGet("plans")]
[ProducesResponseType(typeof(object), 200)]
public async Task<IActionResult> List([FromQuery] string? scope)
{
var tenantId = GetTenantId();
var plans = await planService.ListAsync(tenantId, scope);
return Ok(new { data = plans });
}
[HttpGet("plans/{planId:guid}")]
[ProducesResponseType(typeof(PlanResponse), 200)]
public async Task<IActionResult> GetById(Guid planId)
=> Ok(await planService.GetByIdAsync(planId));
[HttpGet("users/me/plan")]
[ProducesResponseType(typeof(UserPlanResponse), 200)]
public async Task<IActionResult> GetCurrentPlan()
=> Ok(await planService.GetCurrentPlanAsync(GetUserId()));
[HttpPost("users/me/plan/purchase")]
[ProducesResponseType(typeof(PurchasePlanResponse), 200)]
public async Task<IActionResult> Purchase([FromBody] PurchasePlanRequest request)
{
var result = await planService.PurchasePlanAsync(GetUserId(), GetTenantId(), request);
return Ok(result);
}
private Guid GetUserId() => Guid.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException());
private Guid GetTenantId() => Guid.Parse(User.FindFirst("tenant_id")?.Value
?? throw new UnauthorizedAccessException());
}
@@ -0,0 +1,125 @@
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Requests;
using FlatRender.IdentitySvc.Models.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Route("v1/tenants")]
[Authorize]
public class TenantsController(ITenantService tenantService) : ControllerBase
{
[HttpGet]
[ProducesResponseType(typeof(PagedResponse<TenantResponse>), 200)]
public async Task<IActionResult> List([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
=> Ok(await tenantService.ListAsync(page, pageSize));
[HttpPost]
[ProducesResponseType(typeof(TenantResponse), 201)]
public async Task<IActionResult> Create([FromBody] CreateTenantRequest request)
{
var result = await tenantService.CreateAsync(request);
return StatusCode(201, result);
}
[HttpGet("by-slug/{slug}")]
[AllowAnonymous]
[ProducesResponseType(typeof(TenantResponse), 200)]
public async Task<IActionResult> GetBySlug(string slug)
=> Ok(await tenantService.GetBySlugAsync(slug));
[HttpGet("{tenantId:guid}")]
[ProducesResponseType(typeof(TenantResponse), 200)]
public async Task<IActionResult> GetById(Guid tenantId)
=> Ok(await tenantService.GetByIdAsync(tenantId));
[HttpPatch("{tenantId:guid}")]
[ProducesResponseType(typeof(TenantResponse), 200)]
public async Task<IActionResult> Update(Guid tenantId, [FromBody] UpdateTenantRequest request)
=> Ok(await tenantService.UpdateAsync(tenantId, request));
[HttpGet("{tenantId:guid}/branding")]
[ProducesResponseType(typeof(TenantBrandingResponse), 200)]
public async Task<IActionResult> GetBranding(Guid tenantId)
=> Ok(await tenantService.GetBrandingAsync(tenantId));
[HttpPut("{tenantId:guid}/branding")]
[ProducesResponseType(typeof(TenantBrandingResponse), 200)]
public async Task<IActionResult> UpsertBranding(Guid tenantId, [FromBody] TenantBrandingRequest request)
=> Ok(await tenantService.UpsertBrandingAsync(tenantId, request));
[HttpPost("{tenantId:guid}/domains/verify")]
[ProducesResponseType(typeof(DomainVerificationResponse), 200)]
public async Task<IActionResult> VerifyDomain(Guid tenantId, [FromBody] StartDomainVerificationRequest request)
=> Ok(await tenantService.StartDomainVerificationAsync(tenantId, request.Domain, request.Method));
[HttpGet("{tenantId:guid}/usage")]
[ProducesResponseType(typeof(object), 200)]
public async Task<IActionResult> GetUsage(
Guid tenantId,
[FromQuery] DateOnly from,
[FromQuery] DateOnly to)
{
var data = await tenantService.GetUsageAsync(tenantId, from, to);
return Ok(new { data });
}
// ── API Keys ──────────────────────────────────────────────────────────
[HttpGet("{tenantId:guid}/api-keys")]
public async Task<IActionResult> GetApiKeys(Guid tenantId)
=> Ok(new { data = await tenantService.GetApiKeysAsync(tenantId) });
[HttpPost("{tenantId:guid}/api-keys")]
public async Task<IActionResult> CreateApiKey(Guid tenantId, [FromBody] CreateApiKeyRequest request)
{
var userId = GetUserId();
var result = await tenantService.CreateApiKeyAsync(tenantId, userId, request);
return StatusCode(201, result);
}
[HttpDelete("{tenantId:guid}/api-keys/{apiKeyId:guid}")]
public async Task<IActionResult> RevokeApiKey(Guid tenantId, Guid apiKeyId, [FromBody] RevokeApiKeyRequest? request)
{
await tenantService.RevokeApiKeyAsync(tenantId, apiKeyId, request?.Reason);
return NoContent();
}
// ── Webhooks ──────────────────────────────────────────────────────────
[HttpGet("{tenantId:guid}/webhooks")]
public async Task<IActionResult> GetWebhooks(Guid tenantId)
=> Ok(new { data = await tenantService.GetWebhooksAsync(tenantId) });
[HttpPost("{tenantId:guid}/webhooks")]
public async Task<IActionResult> CreateWebhook(Guid tenantId, [FromBody] CreateWebhookRequest request)
{
var result = await tenantService.CreateWebhookAsync(tenantId, request);
return StatusCode(201, result);
}
[HttpDelete("{tenantId:guid}/webhooks/{webhookId:guid}")]
public async Task<IActionResult> DeleteWebhook(Guid tenantId, Guid webhookId)
{
await tenantService.DeleteWebhookAsync(tenantId, webhookId);
return NoContent();
}
[HttpGet("{tenantId:guid}/webhooks/{webhookId:guid}/deliveries")]
public async Task<IActionResult> GetWebhookDeliveries(Guid tenantId, Guid webhookId)
=> Ok(new { data = await tenantService.GetWebhookDeliveriesAsync(tenantId, webhookId) });
private Guid GetUserId() => Guid.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException());
}
[ApiController]
[Route("v1/api-keys")]
public class ApiKeyValidationController(ITenantService tenantService) : ControllerBase
{
[HttpPost("validate")]
public async Task<IActionResult> Validate([FromBody] ValidateApiKeyRequest request)
=> Ok(await tenantService.ValidateApiKeyAsync(request.KeyPrefix, request.KeyHash, request.IpAddress));
}
@@ -0,0 +1,61 @@
using FlatRender.IdentitySvc.Application.Services.Interfaces;
using FlatRender.IdentitySvc.Models.Requests;
using FlatRender.IdentitySvc.Models.Responses;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace FlatRender.IdentitySvc.Controllers;
[ApiController]
[Route("v1/users")]
[Authorize]
public class UsersController(IUserService userService) : ControllerBase
{
[HttpGet("me")]
[ProducesResponseType(typeof(UserResponse), 200)]
public async Task<IActionResult> GetMe()
=> Ok(await userService.GetMeAsync(GetUserId()));
[HttpPatch("me")]
[ProducesResponseType(typeof(UserResponse), 200)]
public async Task<IActionResult> UpdateMe([FromBody] UpdateUserRequest request)
=> Ok(await userService.UpdateMeAsync(GetUserId(), request));
[HttpGet("me/balance")]
[ProducesResponseType(typeof(BalanceResponse), 200)]
public async Task<IActionResult> GetBalance()
=> Ok(await userService.GetBalanceAsync(GetUserId()));
[HttpPost("me/avatar")]
public async Task<IActionResult> SetAvatar([FromBody] SetAvatarRequest request)
{
await userService.UpdateAvatarAsync(GetUserId(), request.AvatarId, request.AvatarUrl);
return Ok();
}
[HttpGet("{userId:guid}")]
[ProducesResponseType(typeof(UserResponse), 200)]
[ProducesResponseType(404)]
public async Task<IActionResult> GetById(Guid userId)
=> Ok(await userService.GetByIdAsync(userId));
[HttpGet]
[ProducesResponseType(typeof(PagedResponse<UserResponse>), 200)]
public async Task<IActionResult> Search(
[FromQuery] string? q,
[FromQuery] Guid? tenantId,
[FromQuery] int page = 1,
[FromQuery] int pageSize = 20)
=> Ok(await userService.SearchAsync(q, tenantId, page, pageSize));
[HttpPost("{userId:guid}/ban")]
[ProducesResponseType(204)]
public async Task<IActionResult> Ban(Guid userId, [FromBody] BanUserRequest request)
{
await userService.BanAsync(userId, request.Reason, request.UnblockDate);
return NoContent();
}
private Guid GetUserId() => Guid.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value
?? User.FindFirst("sub")?.Value ?? throw new UnauthorizedAccessException());
}