diff --git a/AsbCloudApp/Services/IWellCompositeService.cs b/AsbCloudApp/Services/IWellCompositeService.cs new file mode 100644 index 00000000..16132642 --- /dev/null +++ b/AsbCloudApp/Services/IWellCompositeService.cs @@ -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> GetAsync(int idWell, CancellationToken cancellationToken); + Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token); + } +} diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index de3c3473..b8fdb54a 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -14,7 +14,6 @@ namespace AsbCloudDb.Model { public virtual DbSet Clusters { get; set; } public virtual DbSet Companies { get; set; } - public virtual DbSet WellComposites { get; set; } public virtual DbSet Deposits { get; set; } public virtual DbSet FileCategories { get; set; } public virtual DbSet Files { get; set; } @@ -31,6 +30,7 @@ namespace AsbCloudDb.Model public virtual DbSet Users { get; set; } public virtual DbSet UserRoles { get; set; } public virtual DbSet Wells { get; set; } + public virtual DbSet WellComposites { get; set; } public virtual DbSet WellOperations { get; set; } public virtual DbSet WellOperationCategories { get; set; } public virtual DbSet WellSectionTypes { get; set; } diff --git a/AsbCloudDb/Model/IAsbCloudDbContext.cs b/AsbCloudDb/Model/IAsbCloudDbContext.cs index 7184c3e0..565fb4c9 100644 --- a/AsbCloudDb/Model/IAsbCloudDbContext.cs +++ b/AsbCloudDb/Model/IAsbCloudDbContext.cs @@ -26,6 +26,7 @@ namespace AsbCloudDb.Model DbSet WellOperationCategories { get; set; } DbSet TelemetryAnalysis { get; set; } DbSet Wells { get; set; } + DbSet WellComposites { get; set; } DbSet WellSectionTypes { get; set; } DbSet WellOperations { get; set; } DbSet WellTypes { get; set; } diff --git a/AsbCloudInfrastructure/Services/WellCompositeService.cs b/AsbCloudInfrastructure/Services/WellCompositeService.cs new file mode 100644 index 00000000..e00a89d7 --- /dev/null +++ b/AsbCloudInfrastructure/Services/WellCompositeService.cs @@ -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> GetAsync(int idWell, CancellationToken token) + { + var entities = await context.WellComposites + .Where(c => c.IdWell == idWell) + .AsNoTracking() + .ToListAsync(token) + .ConfigureAwait(false); + return entities.Adapt(); + } + + public Task SaveAsync(int idWell, IEnumerable wellComposites, CancellationToken token) + { + context.WellComposites.RemoveRange(context.WellComposites + .Where(c => c.IdWell == idWell)); + var entities = wellComposites + .Adapt(s => s.IdWell = idWell); + context.WellComposites.AddRange(entities); + return context.SaveChangesAsync(token); + } + } +} diff --git a/AsbCloudWebApi/Controllers/WellCompositeController.cs b/AsbCloudWebApi/Controllers/WellCompositeController.cs new file mode 100644 index 00000000..5db2d4fd --- /dev/null +++ b/AsbCloudWebApi/Controllers/WellCompositeController.cs @@ -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; + } + + /// + /// Получает композитную скважину для скважины + /// + /// id скважины для которой собрана композитная скважина + /// + /// + [HttpGet] + [ProducesResponseType(typeof(IEnumerable), (int)System.Net.HttpStatusCode.OK)] + public async Task 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); + } + + /// + /// Создает или заменяет композитную скважину для скважины с idWell + /// + /// id скважины для которой собрана композитная скважина + /// Секции композитной скважины + /// + /// + [HttpPost] + public async Task SaveAsync(int idWell, IEnumerable 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 CanUserAccessToWellAsync(int idWell, CancellationToken token = default) + { + int? idCompany = User.GetCompanyId(); + return idCompany is not null && await wellService.IsCompanyInvolvedInWellAsync((int)idCompany, + idWell, token).ConfigureAwait(false); + } + } +}