Files
VORTEX-REC-LEAK/Controllers/Link/ActionLinkController.cs
T
2026-07-23 18:21:43 -07:00

77 lines
2.2 KiB
C#

using DeluxeBackend.Models;
using DeluxeBackend.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static DeluxeBackend.Enums;
namespace DeluxeBackend.Controllers.Link
{
[Route("Link/actionlink")]
[ApiController]
public class ActionLinkController : ControllerBase
{
private readonly ILiteDbService db;
private readonly INotificationService ws;
private readonly IJwtService jwt;
public ActionLinkController(ILiteDbService _db, INotificationService _ws, IJwtService _jwt)
{
db = _db;
ws = _ws;
jwt= _jwt;
}
[HttpGet("{code}")]
[Authorize(Roles = "gameClient")]
public async Task<IActionResult> LinkFromCode(string code)
{
ActionLink? actionLink = db.ActionLinks.FindOne(x => x.Code == code);
if (actionLink == null)
{
return NotFound();
}
return Ok(actionLink.ToDictionary());
}
[HttpPost("{code}/consume")]
[Authorize(Roles = "gameClient")]
public async Task<IActionResult> ConsumeLink(string code)
{
Account? account = await jwt.GetLogin(User);
if (account == null)
{
return NotFound("User not found in database");
}
ActionLink? actionLink = db.ActionLinks.FindOne(x => x.Code == code);
if (actionLink == null)
{
return NotFound();
}
if (!actionLink.IsValid)
{
return NotFound();
}
actionLink.Uses += 1;
db.ActionLinks.Update(actionLink);
switch (actionLink.Type)
{
case ActionLinkType.DiscordLink:
account.DiscordId = actionLink.ExtraData!.DiscordUserId;
db.Accounts.Update(account);
await ws.SendToPlayer(account.Id, PushNotification.ModerationQuitGame, new Dictionary<string, object>());
break;
}
return Ok(null);
}
}
}