Files

50 lines
1.7 KiB
C#
Raw Permalink Normal View History

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());
}