PlotDirector/PlotLine/Controllers/ToolbarController.cs

21 lines
819 B
C#

using System.Text.Json;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PlotLine.Data;
using PlotLine.Services;
namespace PlotLine.Controllers;
[Authorize]
public sealed class ToolbarController(ICurrentUserService user, IToolbarPreferenceRepository preferences) : Controller
{
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Save(string[] commandKeys, bool useDefaults = false)
{
if (user.UserId is not int userId) return Unauthorized();
if (!ModelState.IsValid || !ToolbarCommands.IsValid(commandKeys)) return BadRequest();
await preferences.SaveAsync(userId, useDefaults ? null : JsonSerializer.Serialize(commandKeys));
return Json(new { commandKeys = useDefaults ? ToolbarCommands.Defaults : commandKeys });
}
}