using AsbCloudApp.Data; using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace AsbCloudWebApi.Controllers { #nullable enable /// /// контроллер по работе с faq-вопросами /// [Route("api/well/{idWell}/faq")] [ApiController] [Authorize] public class FaqController : ControllerBase { private readonly IFaqRepository faqRepository; private readonly IWellService wellService; public FaqController(IFaqRepository faqRepository, IWellService wellService) { this.faqRepository = faqRepository; this.wellService = wellService; } /// /// получение списка faq-вопросов /// /// /// /// /// [HttpGet] [Permission] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] public async Task GetAsync( [FromRoute] int idWell, [FromQuery] FaqRequestBase request, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var requestToRepository = new FaqRequest(request, idWell); var result = await faqRepository.GetAsync( requestToRepository, token) .ConfigureAwait(false); return Ok(result); } /// /// добавление нового вопроса /// /// ключ скважины /// вопрос /// /// [HttpPost] [Permission] [ProducesResponseType(typeof(FaqDto), (int)System.Net.HttpStatusCode.OK)] public async Task InsertAsync( [Range(1, int.MaxValue, ErrorMessage = "Id скважины не может быть меньше 1")] int idWell, [FromBody] FaqDto faqDto, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); faqDto.IdWell = idWell; faqDto.IdAuthorQuestion = User.GetUserId()!.Value; var result = await faqRepository.InsertAsync(faqDto, token) .ConfigureAwait(false); return Ok(result); } [HttpPut] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task UpdateAsync(int idWell, [FromBody] FaqDto faqDto, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); faqDto.IdAuthorAnswer = User.GetUserId()!.Value; var result = await faqRepository.UpdateAsync(faqDto, token) .ConfigureAwait(false); return Ok(result); } /// /// Объединение 2 вопросов в один /// /// ключ скважины /// ключи вопросов, подлежащих объединению /// настройка, объединять ли текст 2-х вопросов в один или нет /// /// /// [HttpGet("merge")] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task MergeAsync( int idWell, [FromQuery] IEnumerable sourceIds, [FromQuery] bool mergeTexts, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); if (sourceIds.Count() < 2) throw new ArgumentInvalidException("Count of questions must be more than 1", nameof(sourceIds)); var result = await faqRepository.MergeAsync(sourceIds, mergeTexts, token) .ConfigureAwait(false); return Ok(result); } /// /// Удаляет вопрос /// /// id скважины /// id вопроса /// Токен отмены задачи /// Количество удаленных из БД строк [HttpDelete("{id}")] [Permission] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] public async Task DeleteAsync(int idWell, int id, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); var result = await faqRepository.MarkAsDeletedAsync(idWell, id, token) .ConfigureAwait(false); return Ok(result); } private async Task CanUserAccessToWellAsync(int idWell, CancellationToken token) { int? idCompany = User.GetCompanyId(); return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token).ConfigureAwait(false); } } #nullable disable }