forked from ddrilling/AsbCloudServer
Add WellCompositeController
This commit is contained in:
parent
bf7c0895b3
commit
3bff1d0daf
16
AsbCloudApp/Services/IWellCompositeService.cs
Normal file
16
AsbCloudApp/Services/IWellCompositeService.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using AsbCloudApp.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudApp.Services
|
||||
{
|
||||
public interface IWellCompositeService
|
||||
{
|
||||
Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken cancellationToken);
|
||||
Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token);
|
||||
}
|
||||
}
|
@ -14,7 +14,6 @@ namespace AsbCloudDb.Model
|
||||
{
|
||||
public virtual DbSet<Cluster> Clusters { get; set; }
|
||||
public virtual DbSet<Company> Companies { get; set; }
|
||||
public virtual DbSet<WellComposite> WellComposites { get; set; }
|
||||
public virtual DbSet<Deposit> Deposits { get; set; }
|
||||
public virtual DbSet<FileCategory> FileCategories { get; set; }
|
||||
public virtual DbSet<FileInfo> Files { get; set; }
|
||||
@ -31,6 +30,7 @@ namespace AsbCloudDb.Model
|
||||
public virtual DbSet<User> Users { get; set; }
|
||||
public virtual DbSet<UserRole> UserRoles { get; set; }
|
||||
public virtual DbSet<Well> Wells { get; set; }
|
||||
public virtual DbSet<WellComposite> WellComposites { get; set; }
|
||||
public virtual DbSet<WellOperation> WellOperations { get; set; }
|
||||
public virtual DbSet<WellOperationCategory> WellOperationCategories { get; set; }
|
||||
public virtual DbSet<WellSectionType> WellSectionTypes { get; set; }
|
||||
|
@ -26,6 +26,7 @@ namespace AsbCloudDb.Model
|
||||
DbSet<WellOperationCategory> WellOperationCategories { get; set; }
|
||||
DbSet<TelemetryAnalysis> TelemetryAnalysis { get; set; }
|
||||
DbSet<Well> Wells { get; set; }
|
||||
DbSet<WellComposite> WellComposites { get; set; }
|
||||
DbSet<WellSectionType> WellSectionTypes { get; set; }
|
||||
DbSet<WellOperation> WellOperations { get; set; }
|
||||
DbSet<WellType> WellTypes { get; set; }
|
||||
|
44
AsbCloudInfrastructure/Services/WellCompositeService.cs
Normal file
44
AsbCloudInfrastructure/Services/WellCompositeService.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudDb.Model;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudInfrastructure.Services
|
||||
{
|
||||
public class WellCompositeService : IWellCompositeService
|
||||
{
|
||||
private readonly IAsbCloudDbContext context;
|
||||
|
||||
public WellCompositeService(IAsbCloudDbContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<WellCompositeDto>> GetAsync(int idWell, CancellationToken token)
|
||||
{
|
||||
var entities = await context.WellComposites
|
||||
.Where(c => c.IdWell == idWell)
|
||||
.AsNoTracking()
|
||||
.ToListAsync(token)
|
||||
.ConfigureAwait(false);
|
||||
return entities.Adapt<WellCompositeDto>();
|
||||
}
|
||||
|
||||
public Task<int> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token)
|
||||
{
|
||||
context.WellComposites.RemoveRange(context.WellComposites
|
||||
.Where(c => c.IdWell == idWell));
|
||||
var entities = wellComposites
|
||||
.Adapt<WellComposite>(s => s.IdWell = idWell);
|
||||
context.WellComposites.AddRange(entities);
|
||||
return context.SaveChangesAsync(token);
|
||||
}
|
||||
}
|
||||
}
|
66
AsbCloudWebApi/Controllers/WellCompositeController.cs
Normal file
66
AsbCloudWebApi/Controllers/WellCompositeController.cs
Normal file
@ -0,0 +1,66 @@
|
||||
using AsbCloudApp.Data;
|
||||
using AsbCloudApp.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AsbCloudWebApi.Controllers
|
||||
{
|
||||
[Route("api/well/{idWell}/composite")]
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class WellCompositeController : ControllerBase
|
||||
{
|
||||
private readonly IWellCompositeService service;
|
||||
private readonly IWellService wellService;
|
||||
|
||||
public WellCompositeController(IWellCompositeService service, IWellService wellService)
|
||||
{
|
||||
this.service = service;
|
||||
this.wellService = wellService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает композитную скважину для скважины
|
||||
/// </summary>
|
||||
/// <param name="idWell">id скважины для которой собрана композитная скважина</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
[ProducesResponseType(typeof(IEnumerable<WellCompositeDto>), (int)System.Net.HttpStatusCode.OK)]
|
||||
public async Task<IActionResult> GetAsync(int idWell, CancellationToken token = default)
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var result = await service.GetAsync(idWell, token).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Создает или заменяет композитную скважину для скважины с idWell
|
||||
/// </summary>
|
||||
/// <param name="idWell">id скважины для которой собрана композитная скважина</param>
|
||||
/// <param name="wellComposites">Секции композитной скважины</param>
|
||||
/// <param name="token"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SaveAsync(int idWell, IEnumerable<WellCompositeDto> wellComposites, CancellationToken token = default)
|
||||
{
|
||||
if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false))
|
||||
return Forbid();
|
||||
|
||||
var result = await service.SaveAsync(idWell, wellComposites, token).ConfigureAwait(false);
|
||||
return Ok(result);
|
||||
}
|
||||
|
||||
private async Task<bool> CanUserAccessToWellAsync(int idWell, CancellationToken token = default)
|
||||
{
|
||||
int? idCompany = User.GetCompanyId();
|
||||
return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany,
|
||||
idWell, token).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user