2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
2021-08-11 12:11:21 +05:00
|
|
|
|
using System.Threading.Tasks;
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudWebApi.Controllers
|
|
|
|
|
{
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Authorize]
|
2021-08-02 12:59:43 +05:00
|
|
|
|
public abstract class LastDataController<T> : ControllerBase where T : class
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly ILastDataService<T> lastDataService;
|
|
|
|
|
private readonly IWellService wellService;
|
|
|
|
|
|
2021-08-02 12:59:43 +05:00
|
|
|
|
public LastDataController(ILastDataService<T> lastDataService, IWellService wellService)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
|
|
|
|
this.lastDataService = lastDataService;
|
|
|
|
|
this.wellService = wellService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<IActionResult> GetAsync([FromRoute] int idWell,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
[FromQuery] int idCategory, CancellationToken token = default)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
idWell, token).ConfigureAwait(false))
|
2021-08-24 10:59:10 +05:00
|
|
|
|
return Forbid();
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var result = await lastDataService.GetAsync(idWell,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
idCategory, token).ConfigureAwait(false);
|
2021-08-03 17:55:28 +05:00
|
|
|
|
return Ok(result);
|
2021-08-02 11:39:39 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2021-08-24 10:59:10 +05:00
|
|
|
|
public async Task<IActionResult> PutAsync([FromRoute] int idWell,
|
2021-08-11 16:54:42 +05:00
|
|
|
|
[FromQuery] int idCategory, T data, CancellationToken token = default)
|
2021-08-02 11:39:39 +05:00
|
|
|
|
{
|
|
|
|
|
int? idCompany = User.GetCompanyId();
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
if (idCompany is null || !await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
idWell, token).ConfigureAwait(false))
|
2021-08-24 10:59:10 +05:00
|
|
|
|
return Forbid();
|
2021-08-02 11:39:39 +05:00
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
await lastDataService.UpsertAsync(idWell,
|
2021-08-11 17:26:02 +05:00
|
|
|
|
idCategory, data, token).ConfigureAwait(false);
|
2021-08-02 11:39:39 +05:00
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|