using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.Data; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace AsbCloudWebApi.Controllers { [Route("api/admin/user/role")] [ApiController] [Authorize] public class AdminUserRoleController : ControllerBase { private readonly IUserRoleService userRoleService; public AdminUserRoleController(IUserRoleService userRoleService) { this.userRoleService = userRoleService; } /// /// Получает список всех доступных ролей /// /// Токен отмены задачи /// Список всех доступных ролей [HttpGet] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetAllAsync(CancellationToken token = default) { // TODO: Как будем делать проверку ролей пользователя? Админ, не админ. var result = await userRoleService.GetAllAsync(token) .ConfigureAwait(false); return Ok(result); } /// /// Получает информацию о запрашиваемой роли /// /// id запрашиваемой задачи /// Токен отмены задачи /// Информацию о запрашиваемой роли [HttpGet("{idRole}")] [ProducesResponseType(typeof(UserRoleDto), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync(int idRole, CancellationToken token = default) { var result = await userRoleService.GetAsync(idRole, token) .ConfigureAwait(false); return Ok(result); } /// /// Добавить запись /// /// Объект с параметрами добавляемой роли /// Токен отмены задачи /// 1 - добавлено, 0 - нет [HttpPost] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task Insert([FromBody] UserRoleDto dto, CancellationToken token = default) { var result = await userRoleService.InsertAsync(dto, token) .ConfigureAwait(false); return Ok(result); } /// /// Редактировать запись по id /// /// Объект с параметрами добавляемой роли /// Id добавляемых к роли разрешений /// /// 1 - успешно отредактировано, 0 - нет [HttpPut("{id}")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task Update([FromBody] UserRoleDto dto, CancellationToken token = default) { await userRoleService.UpdateAsync(dto, token).ConfigureAwait(false); return Ok(); } /// /// Удаляет роли по указанным id /// /// Список id ролей для удаления /// /// [HttpDelete] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task Update(IEnumerable ids, CancellationToken token = default) { var result = await userRoleService.DeleteAsync(ids, token).ConfigureAwait(false); return Ok(result); } } }