Рефакторинг контроллеров

This commit is contained in:
Степанов Дмитрий 2023-10-12 16:03:34 +05:00
parent 6556a03bb5
commit 53e1e07ca0
5 changed files with 102 additions and 99 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Exceptions;
using AsbCloudApp.Repositories;
@ -17,8 +18,8 @@ namespace AsbCloudWebApi.Controllers.ProcessMaps;
[ApiController]
[Route("api/well/{idWell}/[controller]")]
[Authorize]
public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
where TProcessMap : ProcessMapBaseDto
public abstract class ProcessMapBaseController<T> : ControllerBase
where T : ProcessMapPlanBaseDto
{
protected int IdUser
{
@ -33,17 +34,21 @@ public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
}
}
protected readonly IWellService wellService;
protected readonly IRepositoryWellRelated<TProcessMap> repository;
protected readonly IUserRepository userRepository;
private readonly IWellService wellService;
private readonly IUserRepository userRepository;
private readonly ICrudRepository<WellSectionTypeDto> wellSectionRepository;
protected readonly IRepositoryWellRelated<T> repository;
protected ProcessMapBaseController(IWellService wellService,
IRepositoryWellRelated<TProcessMap> repository,
IUserRepository userRepository)
IProcessMapPlanRepository<T> repository,
IUserRepository userRepository,
ICrudRepository<WellSectionTypeDto> wellSectionRepository)
{
this.wellService = wellService;
this.repository = repository;
this.userRepository = userRepository;
this.wellSectionRepository = wellSectionRepository;
}
/// <summary>
@ -56,13 +61,15 @@ public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
[HttpPost]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> InsertAsync(TProcessMap processMap, int idWell, CancellationToken cancellationToken)
public virtual async Task<IActionResult> InsertAsync(T processMap, int idWell, CancellationToken cancellationToken)
{
processMap.IdWell = idWell;
processMap.IdWell = idWell;
processMap.IdUser = IdUser;
processMap.LastUpdate = DateTime.UtcNow;
await AssertUserHasAccessToEditProcessMapAsync(processMap.IdWell, cancellationToken);
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
await AssertUserHasAccessToEditProcessMapAsync(processMap.IdWell, cancellationToken);
var result = await repository.InsertAsync(processMap, cancellationToken);
@ -79,17 +86,19 @@ public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
[HttpPut]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> UpdateAsync(TProcessMap processMap, int idWell, CancellationToken cancellationToken)
public virtual async Task<IActionResult> UpdateAsync(T processMap, int idWell, CancellationToken cancellationToken)
{
processMap.IdWell = idWell;
processMap.IdUser = IdUser;
processMap.LastUpdate = DateTime.UtcNow;
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
var result = await repository.UpdateAsync(processMap, cancellationToken);
if (result == ICrudRepository<TProcessMap>.ErrorIdNotFound)
if (result == ICrudRepository<T>.ErrorIdNotFound)
return this.ValidationBadRequest(nameof(processMap.Id), $"РТК с Id: {processMap.Id} не существует");
return Ok(result);
@ -98,17 +107,18 @@ public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
/// <summary>
/// Удаление РТК
/// </summary>
/// <param name="id">Id удаляемой РТК</param>
/// <param name="idWell">Id скважины</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpDelete]
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public virtual async Task<IActionResult> DeleteAsync(int idWell, CancellationToken cancellationToken)
public virtual async Task<IActionResult> DeleteAsync(int id, int idWell, CancellationToken cancellationToken)
{
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
await AssertUserHasAccessToEditProcessMapAsync(idWell, cancellationToken);
var result = await repository.DeleteAsync(idWell, cancellationToken);
var result = await repository.DeleteAsync(id, cancellationToken);
return Ok(result);
}
@ -128,8 +138,7 @@ public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
return Ok(processMaps);
}
protected virtual async Task AssertUserHasAccessToEditProcessMapAsync(int idWell,
CancellationToken cancellationToken)
protected async Task AssertUserHasAccessToEditProcessMapAsync(int idWell, CancellationToken cancellationToken)
{
var well = await wellService.GetOrDefaultAsync(idWell, cancellationToken)
?? throw new ArgumentInvalidException(nameof(idWell),$"Скважины с {idWell} не существует");
@ -140,7 +149,15 @@ public abstract class ProcessMapBaseController<TProcessMap> : ControllerBase
!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, cancellationToken))
throw new ForbidException("Нет доступа к скважине");
if (well.IdState == 2 && !userRepository.HasPermission(IdUser, "WellDrillingProcessMap.editCompletedWell"))
if (well.IdState == 2 && !userRepository.HasPermission(IdUser, "ProcessMap.editCompletedWell"))
throw new ForbidException("Недостаточно прав для редактирования РТК завершенной скважины");
}
private async Task CheckIsExistsWellSectionTypeAsync(int idWellSectionType, CancellationToken cancellationToken)
{
var wellSection = await wellSectionRepository.GetOrDefaultAsync(idWellSectionType, cancellationToken);
if (wellSection is null)
throw new ArgumentInvalidException(nameof(ProcessMapPlanWellDrillingDto.IdWellSectionType), $"Тип секции с Id: {idWellSectionType} не найден");
}
}

View File

@ -16,30 +16,28 @@ using Microsoft.AspNetCore.SignalR;
namespace AsbCloudWebApi.Controllers.ProcessMaps;
public class WellDrillingProcessMapController : ProcessMapBaseController<WellDrillingProcessMapDto>
public class ProcessMapWellDrillingController : ProcessMapBaseController<ProcessMapPlanWellDrillingDto>
{
private readonly IHubContext<TelemetryHub> telemetryHubContext;
private readonly IWellDrillingProcessMapReportService wellDrillingProcessMapReportService;
private readonly IProcessMapReportWellDrillingService wellDrillingProcessMapReportService;
private readonly IProcessMapReportExportService wellDrillingProcessMapReportExportService;
private readonly IProcessMapImportService wellDrillingProcessMapImportService;
private readonly ICrudRepository<WellSectionTypeDto> wellSectionRepository;
public WellDrillingProcessMapController(IWellService wellService,
IWellDrillingProcessMapRepository repository,
public ProcessMapWellDrillingController(IWellService wellService,
IProcessMapPlanRepository<ProcessMapPlanWellDrillingDto> repository,
IUserRepository userRepository,
IHubContext<TelemetryHub> telemetryHubContext,
IProcessMapReportExportService wellDrillingProcessMapReportExportService,
IProcessMapImportService wellDrillingProcessMapImportService,
IWellDrillingProcessMapReportService wellDrillingProcessMapReportService,
IProcessMapReportWellDrillingService wellDrillingProcessMapReportService,
ICrudRepository<WellSectionTypeDto> wellSectionRepository)
: base(wellService, repository, userRepository)
: base(wellService, repository, userRepository, wellSectionRepository)
{
this.telemetryHubContext = telemetryHubContext;
this.wellDrillingProcessMapReportExportService = wellDrillingProcessMapReportExportService;
this.wellDrillingProcessMapImportService = wellDrillingProcessMapImportService;
this.wellDrillingProcessMapReportService = wellDrillingProcessMapReportService;
this.wellSectionRepository = wellSectionRepository;
}
/// <summary>
@ -49,11 +47,8 @@ public class WellDrillingProcessMapController : ProcessMapBaseController<WellDri
/// <param name="idWell">Id скважины</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<IActionResult> InsertAsync(WellDrillingProcessMapDto processMap, int idWell,
CancellationToken cancellationToken)
public override async Task<IActionResult> InsertAsync(ProcessMapPlanWellDrillingDto processMap, int idWell, CancellationToken cancellationToken)
{
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
var result = await base.InsertAsync(processMap, idWell, cancellationToken);
await NotifyUsersBySignalR(idWell, cancellationToken);
@ -68,11 +63,8 @@ public class WellDrillingProcessMapController : ProcessMapBaseController<WellDri
/// <param name="idWell">Id скважины</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<IActionResult> UpdateAsync(WellDrillingProcessMapDto processMap, int idWell,
CancellationToken cancellationToken)
public override async Task<IActionResult> UpdateAsync(ProcessMapPlanWellDrillingDto processMap, int idWell, CancellationToken cancellationToken)
{
await CheckIsExistsWellSectionTypeAsync(processMap.IdWellSectionType, cancellationToken);
var result = await base.UpdateAsync(processMap, idWell, cancellationToken);
await NotifyUsersBySignalR(idWell, cancellationToken);
@ -83,12 +75,13 @@ public class WellDrillingProcessMapController : ProcessMapBaseController<WellDri
/// <summary>
/// Удаление РТК
/// </summary>
/// <param name="id">Id удаляемой РТК</param>
/// <param name="idWell">Id скважины</param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override async Task<IActionResult> DeleteAsync(int idWell, CancellationToken cancellationToken)
public override async Task<IActionResult> DeleteAsync(int id, int idWell, CancellationToken cancellationToken)
{
var result = await base.DeleteAsync(idWell, cancellationToken);
var result = await base.DeleteAsync(id, idWell, cancellationToken);
await NotifyUsersBySignalR(idWell, cancellationToken);
@ -102,7 +95,7 @@ public class WellDrillingProcessMapController : ProcessMapBaseController<WellDri
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("report")]
[ProducesResponseType(typeof(WellDrillingProcessMapDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProcessMapPlanWellDrillingDto), StatusCodes.Status200OK)]
public async Task<IActionResult> GetReportAsync(int idWell, CancellationToken cancellationToken)
{
var report = await wellDrillingProcessMapReportService.GetAsync(idWell, cancellationToken);
@ -158,7 +151,9 @@ public class WellDrillingProcessMapController : ProcessMapBaseController<WellDri
(options & 1) > 0,
stream,
cancellationToken);
}
await NotifyUsersBySignalR(idWell, cancellationToken);
}
catch (FileFormatException ex)
{
return this.ValidationBadRequest(nameof(file), ex.Message);
@ -195,14 +190,6 @@ public class WellDrillingProcessMapController : ProcessMapBaseController<WellDri
return File(template.File, "application/octet-stream", template.Name);
}
private async Task CheckIsExistsWellSectionTypeAsync(int idWellSectionType, CancellationToken cancellationToken)
{
var wellSection = await wellSectionRepository.GetOrDefaultAsync(idWellSectionType, cancellationToken);
if (wellSection is null)
throw new ArgumentInvalidException(nameof(WellDrillingProcessMapDto.IdWellSectionType), $"Тип секции с Id: {idWellSectionType} не найден");
}
private async Task NotifyUsersBySignalR(int idWell, CancellationToken cancellationToken)
{
var wellDrillingProcessMaps = await repository.GetByIdWellAsync(idWell, cancellationToken);

View File

@ -0,0 +1,17 @@
using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
namespace AsbCloudWebApi.Controllers.ProcessMaps;
public class ProcessMapWellReamController : ProcessMapBaseController<ProcessMapPlanWellReamDto>
{
public ProcessMapWellReamController(IWellService wellService,
IProcessMapPlanRepository<ProcessMapPlanWellReamDto> repository,
IUserRepository userRepository,
ICrudRepository<WellSectionTypeDto> wellSectionRepository)
: base(wellService, repository, userRepository, wellSectionRepository)
{
}
}

View File

@ -1,15 +0,0 @@
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
namespace AsbCloudWebApi.Controllers.ProcessMaps;
public class WellReamProcessMapController : ProcessMapBaseController<WellReamProcessMapDto>
{
public WellReamProcessMapController(IWellService wellService,
IWellReamProcessMapRepository repository,
IUserRepository userRepository)
: base(wellService, repository, userRepository)
{
}
}

View File

@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.ProcessMaps;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using AsbCloudApp.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace AsbCloudWebApi.Controllers.SAUB;
@ -18,20 +18,17 @@ namespace AsbCloudWebApi.Controllers.SAUB;
[Route("api/[controller]")]
public class TelemetryProcessMapController : ControllerBase
{
private readonly IWellService wellService;
private readonly ITelemetryService telemetryService;
private readonly IWellDrillingProcessMapRepository wellDrillingProcessMapRepository;
private readonly IWellReamProcessMapRepository wellReamProcessMapRepository;
private readonly IProcessMapPlanRepository<ProcessMapPlanWellDrillingDto> processMapPlanWellDrillingRepository;
private readonly IProcessMapPlanRepository<ProcessMapPlanWellReamDto> processMapPlanWellReamRepository;
public TelemetryProcessMapController(IWellService wellService,
ITelemetryService telemetryService,
IWellDrillingProcessMapRepository wellDrillingProcessMapRepository,
IWellReamProcessMapRepository wellReamProcessMapRepository)
public TelemetryProcessMapController(ITelemetryService telemetryService,
IProcessMapPlanRepository<ProcessMapPlanWellDrillingDto> processMapPlanWellDrillingRepository,
IProcessMapPlanRepository<ProcessMapPlanWellReamDto> processMapPlanWellReamRepository)
{
this.wellService = wellService;
this.telemetryService = telemetryService;
this.wellDrillingProcessMapRepository = wellDrillingProcessMapRepository;
this.wellReamProcessMapRepository = wellReamProcessMapRepository;
this.telemetryService = telemetryService;
this.processMapPlanWellDrillingRepository = processMapPlanWellDrillingRepository;
this.processMapPlanWellReamRepository = processMapPlanWellReamRepository;
}
/// <summary>
@ -42,22 +39,22 @@ public class TelemetryProcessMapController : ControllerBase
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("wellDrillingProcessMap")]
[ProducesResponseType(typeof(IEnumerable<WellDrillingProcessMapDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanWellDrillingDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetWellDrillingProcessMapByUidAsync(string uid, DateTime updateFrom, CancellationToken cancellationToken)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (!idWell.HasValue)
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
if (!idWell.HasValue)
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
var wellDrillingProcessMaps = await wellDrillingProcessMapRepository.GetAsync(new WellDrillingProcessMapRequest
{
IdWell = idWell.Value,
UpdateFrom = updateFrom
}, cancellationToken);
var wellDrillingProcessMaps = await processMapPlanWellDrillingRepository.GetAsync(new[] { new ProcessMapPlanRequest
{
IdWell = idWell.Value,
UpdateFrom = updateFrom
}}, cancellationToken);
return Ok(wellDrillingProcessMaps);
return Ok(wellDrillingProcessMaps);
}
/// <summary>
@ -68,23 +65,23 @@ public class TelemetryProcessMapController : ControllerBase
/// <param name="cancellationToken"></param>
/// <returns></returns>
[HttpGet("wellReamProcessMap")]
[ProducesResponseType(typeof(IEnumerable<WellReamProcessMapDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(IEnumerable<ProcessMapPlanWellReamDto>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> GetWellReamProcessMapByUidAsync(string uid, DateTime updateFrom,
CancellationToken cancellationToken)
CancellationToken cancellationToken)
{
var idWell = telemetryService.GetIdWellByTelemetryUid(uid);
if (!idWell.HasValue)
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
if (!idWell.HasValue)
return this.ValidationBadRequest(nameof(uid), $"Wrong uid {uid}");
var wellReamProcessMaps = await wellReamProcessMapRepository.GetAsync(new WellReamProcessMapRequest
{
IdWell = idWell.Value,
UpdateFrom = updateFrom
}, cancellationToken);
var wellReamProcessMaps = await processMapPlanWellReamRepository.GetAsync(new[] { new ProcessMapPlanRequest
{
IdWell = idWell.Value,
UpdateFrom = updateFrom
}}, cancellationToken);
return Ok(wellReamProcessMaps);
return Ok(wellReamProcessMaps);
}
}