diff --git a/AsbCloudApp/Requests/WellContactRequest.cs b/AsbCloudApp/Requests/WellContactRequest.cs new file mode 100644 index 00000000..26fd4f1b --- /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..770375f3 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,17 +22,22 @@ 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)); + + if (request.ContactTypeId.HasValue) + { + query = query.Where(c => c.IdCompanyType == request.ContactTypeId); + }; var entities = await query.AsNoTracking() .ToArrayAsync(token); - return entities; + var dtos = entities.Select(c => c.Adapt()); + + return dtos; } public async Task GetAsync(int idWell, int id, CancellationToken token) diff --git a/AsbCloudWebApi/Controllers/WellContactController.cs b/AsbCloudWebApi/Controllers/WellContactController.cs index bdb6a7ae..1bd43564 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("/api/well/[controller]")] + [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); }