forked from ddrilling/AsbCloudServer
199 lines
7.5 KiB
C#
199 lines
7.5 KiB
C#
using AsbCloudApp.Data.ProcessMapPlan;
|
||
using AsbCloudApp.Repositories;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using System.Threading;
|
||
using Microsoft.AspNetCore.Http;
|
||
using AsbCloudApp.Exceptions;
|
||
using AsbCloudApp.Requests;
|
||
using System;
|
||
using AsbCloudApp.Services;
|
||
using System.Linq;
|
||
|
||
namespace AsbCloudWebApi.Controllers.ProcessMapPlan;
|
||
|
||
/// <summary>
|
||
/// РТК план
|
||
/// </summary>
|
||
[ApiController]
|
||
[Route("api/well/{idWell}/[controller]")]
|
||
[Authorize]
|
||
public abstract class ProcessMapPlanBaseController<TDto> : ControllerBase
|
||
where TDto : ProcessMapPlanBaseDto
|
||
{
|
||
private readonly IProcessMapPlanBaseRepository<TDto> repository;
|
||
private readonly IWellService wellService;
|
||
|
||
public ProcessMapPlanBaseController(IProcessMapPlanBaseRepository<TDto> repository, IWellService wellService)
|
||
{
|
||
this.repository = repository;
|
||
this.wellService = wellService;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавление
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="dtos"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<IActionResult> InsertRangeAsync([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
||
{
|
||
if (idWell == 0 || dtos.Any(d => d.IdWell != idWell))
|
||
return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell");
|
||
|
||
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
|
||
var result = await repository.InsertRangeAsync(idUser, dtos, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удалить все по скважине и добавить новые
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="dtos"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpPost("replace")]
|
||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<IActionResult> ClearAndInsertRangeAsync([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
||
{
|
||
if (idWell == 0 || dtos.Any(d => d.IdWell != idWell))
|
||
return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell");
|
||
|
||
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
|
||
var result = await repository.ClearAndInsertRangeAsync(idUser, idWell, dtos, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удаление
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="ids"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpDelete]
|
||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<IActionResult> DeleteRangeAsync([FromRoute]int idWell, IEnumerable<int> ids, CancellationToken token)
|
||
{
|
||
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
|
||
var result = await repository.DeleteRangeAsync(idUser, ids, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="request"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet]
|
||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<ActionResult<IEnumerable<TDto>>> GetAsync([FromRoute] int idWell, [FromQuery]ProcessMapPlanBaseRequest request, CancellationToken token)
|
||
{
|
||
request.IdWell = idWell;
|
||
await AssertUserHasAccessToWellAsync(request.IdWell, token);
|
||
var result = await repository.GetAsync(request, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Изменения за определенную дату
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="date"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("changeLog")]
|
||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<ActionResult<IEnumerable<TDto>>> GetChangeLogAsync([FromRoute] int idWell, [FromQuery] DateOnly? date, CancellationToken token)
|
||
{
|
||
await AssertUserHasAccessToWellAsync(idWell, token);
|
||
var result = await repository.GetChangeLogAsync(idWell, date, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Даты за которые есть изменения
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("dates")]
|
||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<ActionResult<IEnumerable<DateOnly>>> GetDatesChangeAsync([FromRoute] int idWell, CancellationToken token)
|
||
{
|
||
await AssertUserHasAccessToWellAsync(idWell, token);
|
||
var result = await repository.GetDatesChangeAsync(idWell, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Редактирование
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="dtos"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpPut]
|
||
[ProducesResponseType(typeof(int), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
|
||
public async Task<IActionResult> UpdateRangeAsync([FromRoute] int idWell, IEnumerable<TDto> dtos, CancellationToken token)
|
||
{
|
||
var first = dtos.FirstOrDefault();
|
||
if(first is null)
|
||
return NoContent();
|
||
|
||
if (idWell == 0 || dtos.Any(d => d.IdWell != idWell))
|
||
return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell");
|
||
|
||
var idUser = await AssertUserHasAccessToWellAsync(idWell, token);
|
||
|
||
var result = await repository.UpdateRangeAsync(idUser, dtos, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// returns user id, if he has access to well
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
/// <exception cref="ForbidException"></exception>
|
||
private async Task<int> AssertUserHasAccessToWellAsync(int idWell, CancellationToken token)
|
||
{
|
||
var idUser = GetUserId();
|
||
var idCompany = User.GetCompanyId();
|
||
|
||
if (!idCompany.HasValue)
|
||
throw new ForbidException("Нет доступа к скважине");
|
||
|
||
if (!await wellService.IsCompanyInvolvedInWellAsync(idCompany.Value, idWell, token))
|
||
throw new ForbidException("Нет доступа к скважине");
|
||
return idUser;
|
||
}
|
||
|
||
/// <summary>
|
||
/// returns user id or throw
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
/// <exception cref="ForbidException"></exception>
|
||
private int GetUserId()
|
||
{
|
||
var idUser = User.GetUserId() ?? throw new ForbidException("Неизвестный пользователь");
|
||
return idUser;
|
||
}
|
||
}
|