DD.WellWorkover.Cloud/AsbCloudWebApi/Controllers/CrudWellRelatedController.cs
2022-06-16 12:33:05 +05:00

134 lines
4.8 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 контроллер для админки.
/// </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 : ICrudWellRelatedService<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.GetWellsByCompanyAsync((int)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);
}
/// <inheritdoc/>
[HttpGet("{id}")]
public override async Task<ActionResult<T>> GetAsync(int id, CancellationToken token)
{
var actionResult = await base.GetAsync(id, token);
var result = actionResult.Value;
if (!await UserHasAccesToWellAsync(result.IdWell, token))
return Forbid();
return Ok(result);
}
/// <inheritdoc/>
[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);
}
/// <inheritdoc/>
[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);
}
/// <inheritdoc/>
[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);
}
/// <inheritdoc/>
[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;
}
}
}