using AsbCloudApp.Data; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { /// /// контроллер по работе с faq-вопросами /// [Route("api/faq")] [ApiController] [Authorize] public class FaqController : ControllerBase { private readonly IFaqRepository faqRepository; public FaqController(IFaqRepository faqRepository) { this.faqRepository = faqRepository; } /// /// получение списка faq-вопросов /// /// /// /// [HttpGet] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetFilteredAsync( [FromQuery] FaqRequest request, CancellationToken token) { var result = await faqRepository.GetFilteredAsync(request, token).ConfigureAwait(false); return Ok(result); } /// /// добавление нового вопроса /// /// вопрос /// /// [HttpPost] [ProducesResponseType(typeof(FaqDto), (int)System.Net.HttpStatusCode.OK)] public async Task InsertAsync( [FromBody] FaqDto faqDto, CancellationToken token) { faqDto.IdAuthorQuestion = User.GetUserId()!.Value; var result = await faqRepository.InsertAsync(faqDto, token) .ConfigureAwait(false); return Ok(result); } [HttpPut] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateAsync([FromBody] FaqDto faqDto, CancellationToken token) { faqDto.IdAuthorAnswer = User.GetUserId()!.Value; var result = await faqRepository.UpdateAsync(faqDto, token) .ConfigureAwait(false); return Ok(result); } /// /// Объединение 2 вопросов в один /// /// /// /// настройка, объединять ли текст 2-х вопросов в один или нет /// /// /// [HttpPost("merge")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task MergeAsync( [FromQuery] int sourceId1, [FromQuery] int sourceId2, [FromQuery] bool mergeQuestions, CancellationToken token) { var result = await faqRepository.MergeAsync(sourceId1, sourceId2, mergeQuestions, token) .ConfigureAwait(false); return Ok(result); } /// /// Помечает вопрос как удаленный /// /// id вопроса /// Токен отмены задачи /// Количество удаленных из БД строк [HttpPut("{id}")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task MarkAsDeletedAsync(int id, CancellationToken token) { var result = await faqRepository.MarkAsDeletedAsync(id, token) .ConfigureAwait(false); return Ok(result); } /// /// Удаление вопроса /// /// id вопроса /// Токен отмены задачи /// Количество удаленных из БД строк [HttpDelete("{id}")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task DeleteAsync(int id, CancellationToken token) { var result = await faqRepository.DeleteAsync(id, token) .ConfigureAwait(false); return Ok(result); } } }