Add WellCompositeController

This commit is contained in:
Фролов 2021-10-12 12:17:46 +05:00
parent bf7c0895b3
commit 3bff1d0daf
5 changed files with 128 additions and 1 deletions

View 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);
}
}

View File

@ -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; }

View File

@ -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; }

View 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);
}
}
}

View 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);
}
}
}