From 873b6d99a0c552aed3b845901b69180ad803299c Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 8 Aug 2024 14:29:59 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9C=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BF=D0=B8?= =?UTF-8?q?=D1=81=D0=BA=D0=B0=20=D0=BA=D0=BE=D0=BD=D1=82=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D0=B2=20=D0=BF=D0=BE=20=D0=BA=D0=BB=D1=8E=D1=87=D1=83=20?= =?UTF-8?q?=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Requests/WellContactRequest.cs | 20 +++++++++++ AsbCloudApp/Services/IWellContactService.cs | 8 ++--- .../Services/WellContactService.cs | 16 ++++++--- .../Controllers/WellContactController.cs | 36 +++++++++++++++++-- 4 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 AsbCloudApp/Requests/WellContactRequest.cs diff --git a/AsbCloudApp/Requests/WellContactRequest.cs b/AsbCloudApp/Requests/WellContactRequest.cs new file mode 100644 index 00000000..b2b18fa6 --- /dev/null +++ b/AsbCloudApp/Requests/WellContactRequest.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; + +namespace AsbCloudApp.Requests +{ + /// + /// Запрос на контактов + /// + public class WellContactRequest + { + /// + /// Идентификаторы скважин + /// + public IEnumerable IdsWells { get; set; } = null!; + + /// + /// Ключ типа контактов + /// + public int? ContactTypeId { get; set; } + } +} diff --git a/AsbCloudApp/Services/IWellContactService.cs b/AsbCloudApp/Services/IWellContactService.cs index 266da573..cf7124db 100644 --- a/AsbCloudApp/Services/IWellContactService.cs +++ b/AsbCloudApp/Services/IWellContactService.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.User; +using AsbCloudApp.Requests; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -12,13 +13,12 @@ namespace AsbCloudApp.Services public interface IWellContactService { /// - /// Получение контактов по ключу скважины и типу контакта + /// Получение контактов по параметрам запроса /// - /// ключ скважины - /// тип контакта + /// ключ скважины /// /// - Task> GetAllAsync(int idWell, int contactTypeId, CancellationToken token); + Task> GetAllAsync(WellContactRequest request, CancellationToken token); /// /// Получение контакта по ключу diff --git a/AsbCloudInfrastructure/Services/WellContactService.cs b/AsbCloudInfrastructure/Services/WellContactService.cs index 1bb45b77..8fb7b6e6 100644 --- a/AsbCloudInfrastructure/Services/WellContactService.cs +++ b/AsbCloudInfrastructure/Services/WellContactService.cs @@ -1,6 +1,7 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.User; using AsbCloudApp.Exceptions; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; using Mapster; @@ -21,14 +22,19 @@ namespace AsbCloudInfrastructure.Services this.db = db; } - public async Task> GetAllAsync(int wellId, int contactTypeId, CancellationToken token) + public async Task> GetAllAsync(WellContactRequest request, CancellationToken token) { var query = db.Contacts - .Where(c => c.IdCompanyType == contactTypeId) - .Where(c => c.IdWell == wellId) - .Select(c => c.Adapt()); + .Where(c => request.IdsWells.Contains(c.IdWell)); - var entities = await query.AsNoTracking() + if (request.ContactTypeId.HasValue) + { + query = query.Where(c => c.IdCompanyType == request.ContactTypeId); + }; + + var dtos = query.Select(c => c.Adapt()); + + var entities = await dtos.AsNoTracking() .ToArrayAsync(token); return entities; diff --git a/AsbCloudWebApi/Controllers/WellContactController.cs b/AsbCloudWebApi/Controllers/WellContactController.cs index bdb6a7ae..6efe55b1 100644 --- a/AsbCloudWebApi/Controllers/WellContactController.cs +++ b/AsbCloudWebApi/Controllers/WellContactController.cs @@ -1,5 +1,6 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.User; +using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudDb.Model; using Microsoft.AspNetCore.Authorization; @@ -49,12 +50,43 @@ namespace AsbCloudWebApi.Controllers /// [HttpGet("type/{contactTypeId}")] [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] - public async Task GetAllAsync(int idWell, int contactTypeId, CancellationToken token) + public async Task GetByTypeAsync(int idWell, int contactTypeId, CancellationToken token) { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); - var result = await wellContactsRepository.GetAllAsync(idWell, contactTypeId, token); + var request = new WellContactRequest() + { + IdsWells = new int[] { idWell }, + ContactTypeId = contactTypeId + }; + + var result = await wellContactsRepository.GetAllAsync(request, token); + return Ok(result); + } + + /// + /// Получение контактов по массиву ключей скважины + /// + /// ключи скважин + /// + /// + [HttpGet] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + public async Task GetAllAsync([FromQuery] IEnumerable idsWells, CancellationToken token) + { + foreach(var idWell in idsWells) + { + if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) + return Forbid(); + } + + var request = new WellContactRequest() + { + IdsWells = idsWells + }; + + var result = await wellContactsRepository.GetAllAsync(request, token); return Ok(result); }