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 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 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()); break; } return Ok(null); } } }