Метод получения списка контактов по ключу скважины

This commit is contained in:
Olga Nemt 2024-08-08 14:29:59 +05:00
parent 8dfe0246af
commit 873b6d99a0
4 changed files with 69 additions and 11 deletions

View File

@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace AsbCloudApp.Requests
{
/// <summary>
/// Запрос на контактов
/// </summary>
public class WellContactRequest
{
/// <summary>
/// Идентификаторы скважин
/// </summary>
public IEnumerable<int> IdsWells { get; set; } = null!;
/// <summary>
/// Ключ типа контактов
/// </summary>
public int? ContactTypeId { get; set; }
}
}

View File

@ -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
{
/// <summary>
/// Получение контактов по ключу скважины и типу контакта
/// Получение контактов по параметрам запроса
/// </summary>
/// <param name="idWell">ключ скважины</param>
/// <param name="contactTypeId">тип контакта</param>
/// <param name="request">ключ скважины</param>
/// <param name="token"></param>
/// <returns></returns>
Task<IEnumerable<ContactDto>> GetAllAsync(int idWell, int contactTypeId, CancellationToken token);
Task<IEnumerable<ContactDto>> GetAllAsync(WellContactRequest request, CancellationToken token);
/// <summary>
/// Получение контакта по ключу

View File

@ -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<IEnumerable<ContactDto>> GetAllAsync(int wellId, int contactTypeId, CancellationToken token)
public async Task<IEnumerable<ContactDto>> GetAllAsync(WellContactRequest request, CancellationToken token)
{
var query = db.Contacts
.Where(c => c.IdCompanyType == contactTypeId)
.Where(c => c.IdWell == wellId)
.Select(c => c.Adapt<ContactDto>());
.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<ContactDto>());
var entities = await dtos.AsNoTracking()
.ToArrayAsync(token);
return entities;

View File

@ -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
/// <returns></returns>
[HttpGet("type/{contactTypeId}")]
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync(int idWell, int contactTypeId, CancellationToken token)
public async Task<IActionResult> 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);
}
/// <summary>
/// Получение контактов по массиву ключей скважины
/// </summary>
/// <param name="idsWells">ключи скважин</param>
/// <param name="token"></param>
/// <returns></returns>
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ContactDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetAllAsync([FromQuery] IEnumerable<int> 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);
}