Рефакторинг

Сделан рефакторинг: сервиса, репозитория, контроллера
This commit is contained in:
parent 4dddb3af28
commit ed16b7e374
3 changed files with 25 additions and 17 deletions

View File

@ -38,6 +38,7 @@ public class ProcessMapWellboreDevelopmentRepository :
var entities = await query var entities = await query
.OrderBy(p => p.DepthStart) .OrderBy(p => p.DepthStart)
.ThenBy(p => p.Id) .ThenBy(p => p.Id)
.AsNoTracking()
.ToArrayAsync(cancellationToken); .ToArrayAsync(cancellationToken);
return entities.Select(Convert); return entities.Select(Convert);

View File

@ -33,7 +33,7 @@ public class ProcessMapWellboreDevelopmentService : IProcessMapWellboreDevelopme
nameof(processMapWellboreDevelopment.IdWell)); nameof(processMapWellboreDevelopment.IdWell));
if (processMapWellboreDevelopment.DepthStart > processMapWellboreDevelopment.DepthEnd) if (processMapWellboreDevelopment.DepthStart > processMapWellboreDevelopment.DepthEnd)
throw new ArgumentInvalidException("Значение стартовой глубины не может превышать значение конечной глубину", throw new ArgumentInvalidException("Значение стартовой глубины должно быть не больше значения конечной глубины",
nameof(processMapWellboreDevelopment.DepthStart)); nameof(processMapWellboreDevelopment.DepthStart));
processMapWellboreDevelopment.LastUpdate = DateTimeOffset.UtcNow; processMapWellboreDevelopment.LastUpdate = DateTimeOffset.UtcNow;
@ -50,7 +50,7 @@ public class ProcessMapWellboreDevelopmentService : IProcessMapWellboreDevelopme
nameof(processMapWellboreDevelopment.IdWell)); nameof(processMapWellboreDevelopment.IdWell));
if (processMapWellboreDevelopment.DepthStart > processMapWellboreDevelopment.DepthEnd) if (processMapWellboreDevelopment.DepthStart > processMapWellboreDevelopment.DepthEnd)
throw new ArgumentInvalidException("Значение стартовой глубины не может превышать значение конечной глубину", throw new ArgumentInvalidException("Значение стартовой глубины должно быть не больше значения конечной глубины",
nameof(processMapWellboreDevelopment.DepthStart)); nameof(processMapWellboreDevelopment.DepthStart));
processMapWellboreDevelopment.LastUpdate = DateTimeOffset.UtcNow; processMapWellboreDevelopment.LastUpdate = DateTimeOffset.UtcNow;

View File

@ -7,6 +7,7 @@ using AsbCloudApp.Repositories;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System; using System;
using Microsoft.AspNetCore.Authorization;
namespace AsbCloudWebApi.Controllers; namespace AsbCloudWebApi.Controllers;
@ -38,7 +39,10 @@ public class ProcessMapWellboreDevelopmentController : CrudWellRelatedController
/// <exception cref="ForbidException"></exception> /// <exception cref="ForbidException"></exception>
public override async Task<ActionResult<int>> InsertAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token) public override async Task<ActionResult<int>> InsertAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token)
{ {
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, token); value.IdUser = User.GetUserId()
?? throw new ForbidException("Неизвестный пользователь");
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, value.IdUser, token);
return await processMapWellboreDevelopmentService.InsertAsync(value, token); return await processMapWellboreDevelopmentService.InsertAsync(value, token);
} }
@ -51,7 +55,10 @@ public class ProcessMapWellboreDevelopmentController : CrudWellRelatedController
/// <returns></returns> /// <returns></returns>
public override async Task<ActionResult<int>> UpdateAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token) public override async Task<ActionResult<int>> UpdateAsync(ProcessMapWellboreDevelopmentDto value, CancellationToken token)
{ {
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, token); value.IdUser = User.GetUserId()
?? throw new ForbidException("Неизвестный пользователь");
await AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(value.IdWell, value.IdUser, token);
return await processMapWellboreDevelopmentService.UpdateAsync(value, token); return await processMapWellboreDevelopmentService.UpdateAsync(value, token);
} }
@ -64,26 +71,26 @@ public class ProcessMapWellboreDevelopmentController : CrudWellRelatedController
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet("telemetry/{uid}")] [HttpGet("telemetry/{uid}")]
[AllowAnonymous]
[ProducesResponseType(typeof(IEnumerable<ProcessMapWellboreDevelopmentDto>), (int)System.Net.HttpStatusCode.OK)] [ProducesResponseType(typeof(IEnumerable<ProcessMapWellboreDevelopmentDto>), (int)System.Net.HttpStatusCode.OK)]
public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken) public async Task<IActionResult> GetByUidAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken)
{ {
var dto = await processMapWellboreDevelopmentService.GetByTelemetryAsync(uid, var dto = await processMapWellboreDevelopmentService.GetByTelemetryAsync(uid, updateFrom,
updateFrom, cancellationToken); cancellationToken);
return Ok(dto); return Ok(dto);
} }
private async Task AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(int idWell, CancellationToken cancellationToken) private async Task AssertUserHasAccessToProcessMapWellboreDevelopmentAsync(int idUser, int idWell, CancellationToken cancellationToken)
{ {
var idUser = User.GetUserId(); var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken)
?? throw new ForbidException($"Скважины с {idWell} не существует");
if (!idUser.HasValue) var idCompany = User.GetCompanyId();
throw new ForbidException("Неизвестный пользователь"); if (idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
throw new ForbidException("Нет доступа к скважине");
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken) ?? if (well.IdState == 2 && !userRepository.HasPermission(idUser, "ProcessMap.editCompletedWell"))
throw new ForbidException($"Скважины с {idWell} не существует"); throw new ForbidException("Недостаточно прав для редактирования РТК завершённой скважины");
if (well.IdState == 2 && !userRepository.HasPermission(idUser.Value, "ProcessMap.editCompletedWell"))
throw new ForbidException("Недостаточно прав для редактирования проработки");
} }
} }