2022-06-09 11:19:52 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
2022-12-07 15:04:36 +05:00
|
|
|
|
#nullable enable
|
2022-06-09 11:19:52 +05:00
|
|
|
|
/// <summary>
|
2022-06-16 17:37:10 +05:00
|
|
|
|
/// CRUD контроллер dto связных со скважиной для админки.
|
2022-06-09 11:19:52 +05:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <typeparam name="TService"></typeparam>
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[Authorize]
|
|
|
|
|
public abstract class CrudWellRelatedController<T, TService> : CrudController<T, TService>
|
|
|
|
|
where T : IId, IWellRelated
|
2022-08-11 13:55:36 +05:00
|
|
|
|
where TService : IRepositoryWellRelated<T>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
{
|
2022-06-15 14:57:37 +05:00
|
|
|
|
protected readonly IWellService wellService;
|
2022-06-09 11:19:52 +05:00
|
|
|
|
|
|
|
|
|
protected CrudWellRelatedController(IWellService wellService, TService service)
|
|
|
|
|
: base(service)
|
|
|
|
|
{
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение всех записей, доступных компании пользователя.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public override async Task<ActionResult<IEnumerable<T>>> GetAllAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
|
|
|
|
|
|
if (idCompany is null)
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
|
|
|
|
var wells = await wellService.GetWellsByCompanyAsync((int)idCompany, token);
|
|
|
|
|
if (!wells.Any())
|
|
|
|
|
return NoContent();
|
|
|
|
|
|
|
|
|
|
var idsWells = wells.Select(w => w.Id);
|
2022-06-14 15:35:31 +05:00
|
|
|
|
var result = await service.GetByIdWellAsync(idsWells, token);
|
2022-06-09 11:19:52 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение всех записей, для скважины.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="idWell"></param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpGet("well/{idWell}")]
|
2022-06-14 15:35:31 +05:00
|
|
|
|
public async Task<ActionResult<IEnumerable<T>>> GetByIdWellAsync(int idWell, CancellationToken token)
|
2022-06-09 11:19:52 +05:00
|
|
|
|
{
|
|
|
|
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
|
2022-06-14 15:35:31 +05:00
|
|
|
|
var result = await service.GetByIdWellAsync(idWell, token);
|
2022-06-09 11:19:52 +05:00
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:58:17 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получить одну запись по Id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">id записи</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>запись</returns>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
[HttpGet("{id}")]
|
2022-12-07 15:04:36 +05:00
|
|
|
|
public override async Task<ActionResult<T?>> GetOrDefaultAsync(int id, CancellationToken token)
|
2022-06-09 11:19:52 +05:00
|
|
|
|
{
|
2022-12-07 15:04:36 +05:00
|
|
|
|
var actionResult = await base.GetOrDefaultAsync(id, token);
|
|
|
|
|
if(actionResult.Result is OkObjectResult okResult)
|
|
|
|
|
{
|
|
|
|
|
if (okResult.Value is IWellRelated wellRelated)
|
|
|
|
|
if (!await UserHasAccesToWellAsync(wellRelated.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return actionResult;
|
2022-06-09 11:19:52 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:58:17 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить запись
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">запись</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>id</returns>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
[HttpPost]
|
|
|
|
|
public override async Task<ActionResult<int>> InsertAsync([FromBody] T value, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!await UserHasAccesToWellAsync(value.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
return await base.InsertAsync(value, token);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:58:17 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить несколько записей<br/>
|
|
|
|
|
/// При невозможности добавить любую из записей, все не будут добавлены.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="values">записи</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>id</returns>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
[HttpPost("range")]
|
|
|
|
|
public override async Task<ActionResult<int>> InsertRangeAsync([FromBody] IEnumerable<T> values, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idsWells = values.Select(v => v.IdWell).Distinct();
|
|
|
|
|
foreach (var idWell in idsWells)
|
|
|
|
|
if (!await UserHasAccesToWellAsync(idWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
return await base.InsertRangeAsync(values, token);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:58:17 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Редактировать запись по id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value">запись</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
[HttpPut]
|
|
|
|
|
public override async Task<ActionResult<int>> UpdateAsync([FromBody] T value, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
if (!await UserHasAccesToWellAsync(value.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
return await base.UpdateAsync(value, token);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 17:58:17 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удалить запись по id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">id записи</param>
|
|
|
|
|
/// <param name="token"></param>
|
|
|
|
|
/// <returns>1 - успешно удалено, 0 - нет</returns>
|
2022-06-09 11:19:52 +05:00
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
public override async Task<ActionResult<int>> DeleteAsync(int id, CancellationToken token)
|
|
|
|
|
{
|
2022-06-16 12:33:05 +05:00
|
|
|
|
var item = await service.GetOrDefaultAsync(id, token);
|
2022-06-15 14:57:37 +05:00
|
|
|
|
if (item is null)
|
2022-06-09 11:19:52 +05:00
|
|
|
|
return NoContent();
|
|
|
|
|
if (!await UserHasAccesToWellAsync(item.IdWell, token))
|
|
|
|
|
return Forbid();
|
|
|
|
|
return await base.DeleteAsync(id, token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task<bool> UserHasAccesToWellAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var idCompany = User.GetCompanyId();
|
|
|
|
|
if (idCompany is not null &&
|
|
|
|
|
await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, idWell, token)
|
|
|
|
|
.ConfigureAwait(false))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-07 15:04:36 +05:00
|
|
|
|
#nullable disable
|
2022-06-09 11:19:52 +05:00
|
|
|
|
}
|