DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/UserSettingsController.cs

109 lines
4.0 KiB
C#
Raw Normal View History

using AsbCloudApp.Exceptions;
2022-07-27 18:14:07 +05:00
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
{
/// <summary>
/// Контроллер настроек для пользователя
/// </summary>
2022-07-27 18:14:07 +05:00
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class UserSettingsController : ControllerBase
{
private readonly IUserSettingsRepository service;
public UserSettingsController(IUserSettingsRepository service)
{
this.service = service;
}
/// <summary>
/// Получить настройки
/// </summary>
/// <param name="key"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-07-27 18:14:07 +05:00
[HttpGet("{key}")]
[ProducesResponseType(typeof(object), (int)System.Net.HttpStatusCode.OK)]
[Produces("application/json")]
2022-07-27 18:14:07 +05:00
public virtual async Task<IActionResult> 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;
2022-07-27 18:14:07 +05:00
}
/// <summary>
/// записать новые или обновить старые
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-07-27 18:14:07 +05:00
[HttpPut("{key}")]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
public virtual async Task<ActionResult<int>> UpsertAsync(string key, [FromBody] System.Text.Json.JsonDocument value, CancellationToken token)
2022-07-27 18:14:07 +05:00
{
var userId = User.GetUserId();
if (userId is null)
return Forbid();
var result = await service.UpsertAsync((int)userId, key, value, token).ConfigureAwait(false);
2022-07-27 18:14:07 +05:00
if (result < 0)
return this.ValidationBadRequest(nameof(key), "not found");
2022-07-27 18:14:07 +05:00
return Ok(result);
}
/// <summary>
/// Удалить настройки пользователя по ключу
/// </summary>
/// <param name="key"></param>
/// <param name="token"></param>
/// <returns></returns>
2022-07-27 18:14:07 +05:00
[HttpDelete("{key}")]
[ProducesResponseType(typeof(ValidationProblemDetails), (int)System.Net.HttpStatusCode.BadRequest)]
2022-07-27 18:14:07 +05:00
public virtual async Task<ActionResult<int>> 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");
2022-07-27 18:14:07 +05:00
return Ok(result);
}
/// <summary>
/// Удалить ВСЕ настройки пользователя. Для админки.
/// </summary>
/// <param name="idUser"></param>
/// <param name="token"></param>
/// <returns></returns>
[HttpDelete("/api/admin/user/{idUser}/settings")]
[Permission]
public virtual async Task<ActionResult<int>> DeleteAllAsync(int idUser, CancellationToken token)
{
var result = await service.DeleteAsync(idUser, token).ConfigureAwait(false);
return Ok(result);
}
2022-07-27 18:14:07 +05:00
}
}