Рефакторинг

Сделан рефакторинг: сервиса, репозитория, контроллера
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
.OrderBy(p => p.DepthStart)
.ThenBy(p => p.Id)
.AsNoTracking()
.ToArrayAsync(cancellationToken);
return entities.Select(Convert);

View File

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

View File

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