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; /// /// CRUD контроллер dto связных со скважиной для админки. /// /// /// [ApiController] [Route("api/[controller]")] [Authorize] public abstract class CrudWellRelatedController : CrudController where T : IId, IWellRelated where TService : IRepositoryWellRelated { protected readonly IWellService wellService; protected CrudWellRelatedController(IWellService wellService, TService service) : base(service) { this.wellService = wellService; } /// /// Получение всех записей, доступных компании пользователя. /// /// /// [HttpGet] public override async Task>> 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); } /// /// Получение всех записей, для скважины. /// /// /// /// [HttpGet("well/{idWell}")] public async Task>> GetByIdWellAsync(int idWell, CancellationToken token) { if (!await UserHasAccesToWellAsync(idWell, token)) return Forbid(); var result = await service.GetByIdWellAsync(idWell, token); return Ok(result); } /// /// Получить одну запись по Id /// /// id записи /// /// запись [HttpGet("{id}")] public override async Task> 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; } /// /// Добавить запись /// /// запись /// /// id [HttpPost] public override async Task> InsertAsync([FromBody] T value, CancellationToken token) { if (!await UserHasAccesToWellAsync(value.IdWell, token)) return Forbid(); return await base.InsertAsync(value, token); } /// /// Добавить несколько записей
/// При невозможности добавить любую из записей, все не будут добавлены. ///
/// записи /// /// id [HttpPost("range")] public override async Task> InsertRangeAsync([FromBody] IEnumerable 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); } /// /// Редактировать запись по id /// /// запись /// /// 1 - успешно отредактировано, 0 - нет [HttpPut] public override async Task> UpdateAsync([FromBody] T value, CancellationToken token) { if (!await UserHasAccesToWellAsync(value.IdWell, token)) return Forbid(); return await base.UpdateAsync(value, token); } /// /// Удалить запись по id /// /// id записи /// /// 1 - успешно удалено, 0 - нет [HttpDelete("{id}")] public override async Task> 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 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; } }