forked from ddrilling/AsbCloudServer
166 lines
6.2 KiB
C#
166 lines
6.2 KiB
C#
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
|
||
{
|
||
|
||
/// <summary>
|
||
/// CRUD контроллер dto связных со скважиной для админки.
|
||
/// </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
|
||
where TService : IRepositoryWellRelated<T>
|
||
{
|
||
protected readonly IWellService wellService;
|
||
|
||
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.GetAsync(new() { IdCompany = idCompany }, token);
|
||
if (!wells.Any())
|
||
return NoContent();
|
||
|
||
var idsWells = wells.Select(w => w.Id);
|
||
var result = await service.GetByIdWellAsync(idsWells, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получение всех записей, для скважины.
|
||
/// </summary>
|
||
/// <param name="idWell"></param>
|
||
/// <param name="token"></param>
|
||
/// <returns></returns>
|
||
[HttpGet("well/{idWell}")]
|
||
public async Task<ActionResult<IEnumerable<T>>> GetByIdWellAsync(int idWell, CancellationToken token)
|
||
{
|
||
if (!await UserHasAccesToWellAsync(idWell, token))
|
||
return Forbid();
|
||
|
||
var result = await service.GetByIdWellAsync(idWell, token);
|
||
return Ok(result);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получить одну запись по Id
|
||
/// </summary>
|
||
/// <param name="id">id записи</param>
|
||
/// <param name="token"></param>
|
||
/// <returns>запись</returns>
|
||
[HttpGet("{id}")]
|
||
public override async Task<ActionResult<T?>> GetOrDefaultAsync(int id, CancellationToken token)
|
||
{
|
||
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;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавить запись
|
||
/// </summary>
|
||
/// <param name="value">запись</param>
|
||
/// <param name="token"></param>
|
||
/// <returns>id</returns>
|
||
[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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Добавить несколько записей<br/>
|
||
/// При невозможности добавить любую из записей, все не будут добавлены.
|
||
/// </summary>
|
||
/// <param name="values">записи</param>
|
||
/// <param name="token"></param>
|
||
/// <returns>id</returns>
|
||
[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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Редактировать запись по id
|
||
/// </summary>
|
||
/// <param name="value">запись</param>
|
||
/// <param name="token"></param>
|
||
/// <returns>1 - успешно отредактировано, 0 - нет</returns>
|
||
[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);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Удалить запись по id
|
||
/// </summary>
|
||
/// <param name="id">id записи</param>
|
||
/// <param name="token"></param>
|
||
/// <returns>1 - успешно удалено, 0 - нет</returns>
|
||
[HttpDelete("{id}")]
|
||
public override async Task<ActionResult<int>> DeleteAsync(int id, CancellationToken token)
|
||
{
|
||
var item = await service.GetOrDefaultAsync(id, token);
|
||
if (item is null)
|
||
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;
|
||
}
|
||
}
|
||
|
||
}
|