using AsbCloudApp.Exceptions; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers; /// /// Контроллер настроек для пользователя /// [Route("api/[controller]")] [ApiController] [Authorize] public class UserSettingsController : ControllerBase { private readonly IUserSettingsRepository service; public UserSettingsController(IUserSettingsRepository service) { this.service = service; } /// /// Получить настройки /// /// /// /// [HttpGet("{key}")] [ProducesResponseType(typeof(object), (int)System.Net.HttpStatusCode.OK)] [Produces("application/json")] public virtual async Task GetAsync( [StringLength(255, MinimumLength = 1, ErrorMessage = "The key value cannot less then 1 character and greater then 255. ")] string key, CancellationToken token) { var userId = User.GetUserId(); if (userId is null) return Forbid(); var result = await service.GetOrDefaultAsync((int)userId, key, token).ConfigureAwait(false); var actionResult = new JsonResult(result); actionResult.ContentType = "application/json"; return actionResult; } /// /// записать новые или обновить старые /// /// /// /// /// [HttpPut("{key}")] [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] public virtual async Task> UpsertAsync(string key, [FromBody] System.Text.Json.JsonDocument value, CancellationToken token) { var userId = User.GetUserId(); if (userId is null) return Forbid(); var result = await service.UpsertAsync((int)userId, key, value, token).ConfigureAwait(false); if (result < 0) return this.ValidationBadRequest(nameof(key), "not found"); return Ok(result); } /// /// Удалить настройки пользователя по ключу /// /// /// /// [HttpDelete("{key}")] [ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)] public virtual async Task> DeleteAsync(string key, CancellationToken token) { var userId = User.GetUserId(); if (userId is null) return Forbid(); var result = await service.DeleteAsync((int)userId, key, token).ConfigureAwait(false); if (result < 0) return this.ValidationBadRequest(nameof(key), "not found"); return Ok(result); } /// /// Удалить ВСЕ настройки пользователя. Для админки. /// /// /// /// [HttpDelete("/api/admin/user/{idUser}/settings")] [Permission] public virtual async Task> DeleteAllAsync(int idUser, CancellationToken token) { var result = await service.DeleteAsync(idUser, token).ConfigureAwait(false); return Ok(result); } }