using DeluxeBackend.Models; using DeluxeBackend.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json.Linq; using System.ComponentModel.DataAnnotations; namespace DeluxeBackend.Controllers { [Route("PlayerSettings")] [ApiController] public class PlayerSettingsController : ControllerBase { private readonly IJwtService jwt; private readonly ILiteDbService db; public PlayerSettingsController(IJwtService _jwt, ILiteDbService _db) { jwt = _jwt; db = _db; } private Dictionary SettingRoomToDictionary(AccountSetting setting) { return new Dictionary { ["Key"] = setting.Key, ["Value"] = setting.Value }; } [HttpGet("playersettings")] [Authorize(Roles = "gameClient")] public async Task Get() { Account? account = await jwt.GetLogin(User); if (account == null) { return NotFound("User not found in database"); } return Ok(db.Settings.Find(x => x.Account.Id == account.Id).Select(p => SettingRoomToDictionary(p)).ToList()); } [HttpPut("playersettings")] [Authorize(Roles = "gameClient")] public async Task Put([FromForm] string key, [FromForm] string value) { Account? account = await jwt.GetLogin(User); if (account == null) { return NotFound("User not found in database"); } AccountSetting setting = db.Settings.FindOne(x => x.Account.Id == account.Id && x.Key == key); if (setting == null) { setting = new AccountSetting() { Account=account, Key=key, Value=value}; } else { setting.Value = value; } db.Settings.Upsert(setting); return Ok(new { Success = true }); } [HttpDelete("playersettings")] [Authorize(Roles = "gameClient")] public async Task Delete([FromForm] string key) { Account? account = await jwt.GetLogin(User); if (account == null) { return NotFound("User not found in database"); } AccountSetting setting = db.Settings.FindOne(x => x.Account.Id == account.Id && x.Key == key); if (setting == null) { return NotFound("Key not found"); } return Ok(new { Success = db.Settings.Delete(setting.Id) }); } } }