diff --git a/backend/api/Controllers/CasesController.cs b/backend/api/Controllers/CasesController.cs index 31e746e00..095b87860 100644 --- a/backend/api/Controllers/CasesController.cs +++ b/backend/api/Controllers/CasesController.cs @@ -21,16 +21,19 @@ namespace api.Controllers; public class CasesController : ControllerBase { private readonly ICaseService _caseService; + private readonly ICreateCaseService _createCaseService; private readonly ICaseTimeSeriesService _caseTimeSeriesService; private readonly IDuplicateCaseService _duplicateCaseService; public CasesController( ICaseService caseService, + ICreateCaseService createCaseService, ICaseTimeSeriesService caseTimeSeriesService, IDuplicateCaseService duplicateCaseService ) { _caseService = caseService; + _createCaseService = createCaseService; _caseTimeSeriesService = caseTimeSeriesService; _duplicateCaseService = duplicateCaseService; } @@ -38,7 +41,7 @@ IDuplicateCaseService duplicateCaseService [HttpPost] public async Task CreateCase([FromRoute] Guid projectId, [FromBody] CreateCaseDto caseDto) { - return await _caseService.CreateCase(projectId, caseDto); + return await _createCaseService.CreateCase(projectId, caseDto); } [HttpPost("copy", Name = "Duplicate")] diff --git a/backend/api/Controllers/PROSPController.cs b/backend/api/Controllers/PROSPController.cs index 63a7a23a4..f2c525462 100644 --- a/backend/api/Controllers/PROSPController.cs +++ b/backend/api/Controllers/PROSPController.cs @@ -78,17 +78,10 @@ public async Task> ImportFilesFromSharepointA { try { - var projectDto = await _prospSharepointImportService.ConvertSharepointFilesToProjectDto(projectId, dtos); + await _prospSharepointImportService.ConvertSharepointFilesToProjectDto(projectId, dtos); + var projectDto = await _projectService.GetProjectDto(projectId); - if (projectDto.Id == projectId) - { - return Ok(projectDto); - } - else - { - var fallbackDto = _projectService.GetProjectDto(projectId); - return Ok(fallbackDto); - } + return Ok(projectDto); } catch (ServiceException ex) when (ex.StatusCode == System.Net.HttpStatusCode.Forbidden) { diff --git a/backend/api/Program.cs b/backend/api/Program.cs index a2d994f3c..01ccc1952 100644 --- a/backend/api/Program.cs +++ b/backend/api/Program.cs @@ -172,6 +172,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); diff --git a/backend/api/Repositories/Case/CaseRepository.cs b/backend/api/Repositories/Case/CaseRepository.cs index 3ebbef824..9ab7497c6 100644 --- a/backend/api/Repositories/Case/CaseRepository.cs +++ b/backend/api/Repositories/Case/CaseRepository.cs @@ -20,6 +20,14 @@ ILogger logger _logger = logger; } + public async Task AddCase(Case caseItem) + { + _context.Cases.Add(caseItem); + await _context.SaveChangesAsync(); + + return caseItem; + } + public async Task GetCase(Guid caseId) { return await Get(caseId); diff --git a/backend/api/Repositories/Case/ICaseRepository.cs b/backend/api/Repositories/Case/ICaseRepository.cs index 73832c7cb..f7f98bca4 100644 --- a/backend/api/Repositories/Case/ICaseRepository.cs +++ b/backend/api/Repositories/Case/ICaseRepository.cs @@ -8,6 +8,7 @@ namespace api.Repositories; public interface ICaseRepository : IBaseRepository { + Task AddCase(Case caseItem); Task GetCase(Guid caseId); Task GetCaseWithIncludes(Guid caseId, params Expression>[] includes); Case UpdateCase(Case updatedCase); diff --git a/backend/api/Repositories/Substructure/ISubstructureRepository.cs b/backend/api/Repositories/Substructure/ISubstructureRepository.cs index 8cc05c531..c7d4c5699 100644 --- a/backend/api/Repositories/Substructure/ISubstructureRepository.cs +++ b/backend/api/Repositories/Substructure/ISubstructureRepository.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Models; namespace api.Repositories; @@ -5,6 +7,7 @@ namespace api.Repositories; public interface ISubstructureRepository : IBaseRepository { Task GetSubstructure(Guid substructureId); + Task GetSubstructureWithIncludes(Guid substructureId, params Expression>[] includes); Task GetSubstructureWithCostProfile(Guid substructureId); Task SubstructureHasCostProfileOverride(Guid substructureId); Substructure UpdateSubstructure(Substructure substructure); diff --git a/backend/api/Repositories/Substructure/SubstructureRepository.cs b/backend/api/Repositories/Substructure/SubstructureRepository.cs index f0da4062c..3cf96e95e 100644 --- a/backend/api/Repositories/Substructure/SubstructureRepository.cs +++ b/backend/api/Repositories/Substructure/SubstructureRepository.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Context; using api.Models; @@ -18,6 +20,11 @@ public SubstructureRepository(DcdDbContext context) : base(context) return await Get(substructureId); } + public async Task GetSubstructureWithIncludes(Guid substructureId, params Expression>[] includes) + { + return await GetWithIncludes(substructureId, includes); + } + public async Task GetSubstructureWithCostProfile(Guid substructureId) { return await _context.Substructures diff --git a/backend/api/Repositories/Surf/ISurfRepository.cs b/backend/api/Repositories/Surf/ISurfRepository.cs index 694da1d2a..ec8cbd04c 100644 --- a/backend/api/Repositories/Surf/ISurfRepository.cs +++ b/backend/api/Repositories/Surf/ISurfRepository.cs @@ -1,9 +1,12 @@ +using System.Linq.Expressions; + using api.Models; namespace api.Repositories; public interface ISurfRepository : IBaseRepository { + Task GetSurfWithIncludes(Guid surfId, params Expression>[] includes); Task GetSurf(Guid surfId); Task GetSurfWithCostProfile(Guid surfId); Task SurfHasCostProfileOverride(Guid topsideId); diff --git a/backend/api/Repositories/Surf/SurfRepository.cs b/backend/api/Repositories/Surf/SurfRepository.cs index d96d8ffc9..a9d836357 100644 --- a/backend/api/Repositories/Surf/SurfRepository.cs +++ b/backend/api/Repositories/Surf/SurfRepository.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Context; using api.Models; @@ -18,6 +20,11 @@ public SurfRepository(DcdDbContext context) : base(context) return await Get(surfId); } + public async Task GetSurfWithIncludes(Guid surfId, params Expression>[] includes) + { + return await GetWithIncludes(surfId, includes); + } + public async Task GetSurfWithCostProfile(Guid surfId) { return await _context.Surfs diff --git a/backend/api/Repositories/Transport/ITransportRepository.cs b/backend/api/Repositories/Transport/ITransportRepository.cs index a5c922ae5..294c42dc6 100644 --- a/backend/api/Repositories/Transport/ITransportRepository.cs +++ b/backend/api/Repositories/Transport/ITransportRepository.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Models; namespace api.Repositories; @@ -5,6 +7,7 @@ namespace api.Repositories; public interface ITransportRepository : IBaseRepository { Task GetTransport(Guid transportId); + Task GetTransportWithIncludes(Guid transportId, params Expression>[] includes); Task GetTransportWithCostProfile(Guid transportId); Task TransportHasCostProfileOverride(Guid transportId); Transport UpdateTransport(Transport transport); diff --git a/backend/api/Repositories/Transport/TransportRepository.cs b/backend/api/Repositories/Transport/TransportRepository.cs index c300c36ef..c3fb98f00 100644 --- a/backend/api/Repositories/Transport/TransportRepository.cs +++ b/backend/api/Repositories/Transport/TransportRepository.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Context; using api.Models; @@ -18,6 +20,11 @@ public TransportRepository(DcdDbContext context) : base(context) return await Get(transportId); } + public async Task GetTransportWithIncludes(Guid transportId, params Expression>[] includes) + { + return await GetWithIncludes(transportId, includes); + } + public async Task GetTransportWithCostProfile(Guid transportId) { return await _context.Transports diff --git a/backend/api/Repositories/WellProject/IWellProjectRepository.cs b/backend/api/Repositories/WellProject/IWellProjectRepository.cs index dbdfa271f..adb1a3ec1 100644 --- a/backend/api/Repositories/WellProject/IWellProjectRepository.cs +++ b/backend/api/Repositories/WellProject/IWellProjectRepository.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Enums; using api.Models; @@ -6,6 +8,7 @@ namespace api.Repositories; public interface IWellProjectRepository : IBaseRepository { Task GetWellProject(Guid wellProjectId); + Task GetWellProjectWithIncludes(Guid wellProjectId, params Expression>[] includes); Task GetWell(Guid wellId); Task WellProjectHasProfile(Guid WellProjectId, WellProjectProfileNames profileType); WellProject UpdateWellProject(WellProject wellProject); diff --git a/backend/api/Repositories/WellProject/WellProjectRepository.cs b/backend/api/Repositories/WellProject/WellProjectRepository.cs index ea16f6fdc..a5a1b6449 100644 --- a/backend/api/Repositories/WellProject/WellProjectRepository.cs +++ b/backend/api/Repositories/WellProject/WellProjectRepository.cs @@ -21,6 +21,11 @@ public WellProjectRepository(DcdDbContext context) : base(context) return await Get(wellProjectId); } + public async Task GetWellProjectWithIncludes(Guid wellProjectId, params Expression>[] includes) + { + return await GetWithIncludes(wellProjectId, includes); + } + public async Task GetWell(Guid wellId) { return await Get(wellId); diff --git a/backend/api/Services/Entities/Case/CaseService.cs b/backend/api/Services/Entities/Case/CaseService.cs index 13964ec6c..4c45b0dbd 100644 --- a/backend/api/Services/Entities/Case/CaseService.cs +++ b/backend/api/Services/Entities/Case/CaseService.cs @@ -18,13 +18,6 @@ public class CaseService : ICaseService private readonly DcdDbContext _context; private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; - private readonly IDrainageStrategyService _drainageStrategyService; - private readonly ITopsideService _topsideService; - private readonly ISurfService _surfService; - private readonly ISubstructureService _substructureService; - private readonly ITransportService _transportService; - private readonly IExplorationService _explorationService; - private readonly IWellProjectService _wellProjectService; private readonly ILogger _logger; private readonly IMapper _mapper; private readonly IMapperService _mapperService; @@ -34,13 +27,6 @@ public CaseService( DcdDbContext context, IProjectService projectService, ILoggerFactory loggerFactory, - IDrainageStrategyService drainageStrategyService, - ITopsideService topsideService, - ISurfService surfService, - ISubstructureService substructureService, - ITransportService transportService, - IExplorationService explorationService, - IWellProjectService wellProjectService, ICaseRepository repository, IMapperService mapperService, IMapper mapper, @@ -49,13 +35,6 @@ IProjectAccessService projectAccessService { _context = context; _projectService = projectService; - _drainageStrategyService = drainageStrategyService; - _topsideService = topsideService; - _surfService = surfService; - _substructureService = substructureService; - _transportService = transportService; - _explorationService = explorationService; - _wellProjectService = wellProjectService; _logger = loggerFactory.CreateLogger(); _mapper = mapper; _mapperService = mapperService; @@ -63,100 +42,6 @@ IProjectAccessService projectAccessService _projectAccessService = projectAccessService; } - public async Task CreateCase(Guid projectId, CreateCaseDto createCaseDto) - { - var caseItem = _mapper.Map(createCaseDto); - if (caseItem == null) - { - throw new ArgumentNullException(nameof(caseItem)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - caseItem.Project = project; - caseItem.CapexFactorFeasibilityStudies = 0.015; - caseItem.CapexFactorFEEDStudies = 0.015; - - if (caseItem.DG4Date == DateTimeOffset.MinValue) - { - caseItem.DG4Date = new DateTimeOffset(2030, 1, 1, 0, 0, 0, TimeSpan.Zero); - } - - caseItem.CreateTime = DateTimeOffset.UtcNow; - - var createdCase = _context.Cases!.Add(caseItem); - await _context.SaveChangesAsync(); - - var drainageStrategyDto = new CreateDrainageStrategyDto - { - Name = "Drainage strategy", - Description = "" - }; - var drainageStrategy = await _drainageStrategyService.CreateDrainageStrategy(projectId, createdCase.Entity.Id, drainageStrategyDto); - caseItem.DrainageStrategyLink = drainageStrategy.Id; - - var topsideDto = new CreateTopsideDto - { - Name = "Topside", - Source = Source.ConceptApp, - }; - var topside = await _topsideService.CreateTopside(projectId, createdCase.Entity.Id, topsideDto); - caseItem.TopsideLink = topside.Id; - - var surfDto = new CreateSurfDto - { - Name = "Surf", - Source = Source.ConceptApp, - }; - var surf = await _surfService.CreateSurf(projectId, createdCase.Entity.Id, surfDto); - caseItem.SurfLink = surf.Id; - - var substructureDto = new CreateSubstructureDto - { - Name = "Substructure", - Source = Source.ConceptApp, - }; - var substructure = await _substructureService.CreateSubstructure(projectId, createdCase.Entity.Id, substructureDto); - caseItem.SubstructureLink = substructure.Id; - - var transportDto = new CreateTransportDto - { - Name = "Transport", - Source = Source.ConceptApp, - }; - var transport = await _transportService.CreateTransport(projectId, createdCase.Entity.Id, transportDto); - caseItem.TransportLink = transport.Id; - - var explorationDto = new CreateExplorationDto - { - Name = "Exploration", - }; - var exploration = await _explorationService.CreateExploration(projectId, createdCase.Entity.Id, explorationDto); - caseItem.ExplorationLink = exploration.Id; - - var wellProjectDto = new CreateWellProjectDto - { - Name = "WellProject", - }; - var wellProject = await _wellProjectService.CreateWellProject(projectId, createdCase.Entity.Id, wellProjectDto); - caseItem.WellProjectLink = wellProject.Id; - - return await _projectService.GetProjectDto(project.Id); - } - - // TODO: Delete this method - public async Task UpdateCaseAndProfiles(Guid caseId, TDto updatedCaseDto) - where TDto : BaseUpdateCaseDto - { - var caseItem = await GetCase(caseId); - - _mapper.Map(updatedCaseDto, caseItem); - - _context.Cases!.Update(caseItem); - await _repository.UpdateModifyTime(caseId); - await _context.SaveChangesAsync(); - - return await _projectService.GetProjectDto(caseItem.ProjectId); - } - public async Task DeleteCase(Guid projectId, Guid caseId) { // Need to verify that the project from the URL is the same as the project of the resource @@ -208,25 +93,7 @@ public async Task GetCaseWithIncludes(Guid caseId, params Expression> GetAll() { - return await _context.Cases - .Include(c => c.TotalFeasibilityAndConceptStudies) - .Include(c => c.TotalFeasibilityAndConceptStudiesOverride) - .Include(c => c.TotalFEEDStudies) - .Include(c => c.TotalFEEDStudiesOverride) - .Include(c => c.TotalOtherStudiesCostProfile) - .Include(c => c.HistoricCostCostProfile) - .Include(c => c.WellInterventionCostProfile) - .Include(c => c.WellInterventionCostProfileOverride) - .Include(c => c.OffshoreFacilitiesOperationsCostProfile) - .Include(c => c.OffshoreFacilitiesOperationsCostProfileOverride) - .Include(c => c.OnshoreRelatedOPEXCostProfile) - .Include(c => c.AdditionalOPEXCostProfile) - .Include(c => c.CessationWellsCost) - .Include(c => c.CessationWellsCostOverride) - .Include(c => c.CessationOffshoreFacilitiesCost) - .Include(c => c.CessationOffshoreFacilitiesCostOverride) - .Include(c => c.CessationOnshoreFacilitiesCostProfile) - .ToListAsync(); + return await _context.Cases.ToListAsync(); } public async Task UpdateCase( diff --git a/backend/api/Services/Entities/Case/CreateCaseService.cs b/backend/api/Services/Entities/Case/CreateCaseService.cs new file mode 100644 index 000000000..70c27e595 --- /dev/null +++ b/backend/api/Services/Entities/Case/CreateCaseService.cs @@ -0,0 +1,135 @@ +using api.Context; +using api.Dtos; +using api.Models; +using api.Repositories; + +namespace api.Services; + +public class CreateCaseService : ICreateCaseService +{ + private readonly ICaseRepository _caseRepository; + private readonly IProjectService _projectService; + private readonly IMapperService _mapperService; + + public CreateCaseService( + DcdDbContext context, + ICaseRepository caseRepository, + IProjectService projectService, + IMapperService mapperService + ) + { + _caseRepository = caseRepository; + _projectService = projectService; + _mapperService = mapperService; + } + + public async Task CreateCase(Guid projectId, CreateCaseDto createCaseDto) + { + var caseItem = new Case(); + _mapperService.MapToEntity(createCaseDto, caseItem, Guid.Empty); + + var project = await _projectService.GetProject(projectId); + caseItem.Project = project; + caseItem.CapexFactorFeasibilityStudies = 0.015; + caseItem.CapexFactorFEEDStudies = 0.015; + + if (caseItem.DG4Date == DateTimeOffset.MinValue) + { + caseItem.DG4Date = new DateTimeOffset(2030, 1, 1, 0, 0, 0, TimeSpan.Zero); + } + + caseItem.CreateTime = DateTimeOffset.UtcNow; + + caseItem.DrainageStrategy = CreateDrainageStrategy(project); + caseItem.Topside = CreateTopside(project); + caseItem.Surf = CreateSurf(project); + caseItem.Substructure = CreateSubstructure(project); + caseItem.Transport = CreateTransport(project); + caseItem.Exploration = CreateExploration(project); + caseItem.WellProject = CreateWellProject(project); + + await _caseRepository.AddCase(caseItem); + + return await _projectService.GetProjectDto(project.Id); + } + + private static DrainageStrategy CreateDrainageStrategy(Project project) + { + return new DrainageStrategy + { + Name = "Drainage Strategy", + Description = "Drainage Strategy", + Project = project, + }; + } + + private static Topside CreateTopside(Project project) + { + return new Topside + { + Name = "Topside", + Project = project, + CostProfileOverride = new TopsideCostProfileOverride + { + Override = true, + }, + }; + } + + private static Surf CreateSurf(Project project) + { + return new Surf + { + Name = "Surf", + Project = project, + CostProfileOverride = new SurfCostProfileOverride + { + Override = true, + }, + }; + } + + private static Substructure CreateSubstructure(Project project) + { + return new Substructure + { + Name = "Substructure", + Project = project, + CostProfileOverride = new SubstructureCostProfileOverride + { + Override = true, + }, + }; + } + + private static Transport CreateTransport(Project project) + { + return new Transport + { + Name = "Transport", + Project = project, + CostProfileOverride = new TransportCostProfileOverride + { + Override = true, + }, + }; + } + + private static Exploration CreateExploration(Project project) + { + return new Exploration + { + Name = "Exploration", + Project = project, + }; + } + + private static WellProject CreateWellProject(Project project) + { + return new WellProject + { + Name = "Well Project", + Project = project, + }; + } +} diff --git a/backend/api/Services/Entities/Case/ICaseService.cs b/backend/api/Services/Entities/Case/ICaseService.cs index 9e8e4cb1a..d95b27f1d 100644 --- a/backend/api/Services/Entities/Case/ICaseService.cs +++ b/backend/api/Services/Entities/Case/ICaseService.cs @@ -7,8 +7,6 @@ namespace api.Services; public interface ICaseService { - Task CreateCase(Guid projectId, CreateCaseDto createCaseDto); - Task UpdateCaseAndProfiles(Guid caseId, TDto updatedCaseDto) where TDto : BaseUpdateCaseDto; Task DeleteCase(Guid projectId, Guid caseId); Task GetCase(Guid caseId); Task GetCaseWithIncludes(Guid caseId, params Expression>[] includes); diff --git a/backend/api/Services/Entities/Case/ICreateCaseService.cs b/backend/api/Services/Entities/Case/ICreateCaseService.cs new file mode 100644 index 000000000..0bf192031 --- /dev/null +++ b/backend/api/Services/Entities/Case/ICreateCaseService.cs @@ -0,0 +1,11 @@ +using System.Linq.Expressions; + +using api.Dtos; +using api.Models; + +namespace api.Services; + +public interface ICreateCaseService +{ + Task CreateCase(Guid projectId, CreateCaseDto createCaseDto); +} diff --git a/backend/api/Services/Entities/DrainageStrategy/DrainageStrategyService.cs b/backend/api/Services/Entities/DrainageStrategy/DrainageStrategyService.cs index cc4cc5123..23abdc37f 100644 --- a/backend/api/Services/Entities/DrainageStrategy/DrainageStrategyService.cs +++ b/backend/api/Services/Entities/DrainageStrategy/DrainageStrategyService.cs @@ -15,21 +15,15 @@ namespace api.Services; public class DrainageStrategyService : IDrainageStrategyService { - private readonly DcdDbContext _context; - private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; private readonly ILogger _logger; - private readonly IMapper _mapper; private readonly ICaseRepository _caseRepository; private readonly IDrainageStrategyRepository _repository; private readonly IConversionMapperService _conversionMapperService; private readonly IProjectRepository _projectRepository; public DrainageStrategyService( - DcdDbContext context, - IProjectService projectService, ILoggerFactory loggerFactory, - IMapper mapper, ICaseRepository caseRepository, IDrainageStrategyRepository repository, IConversionMapperService conversionMapperService, @@ -37,10 +31,7 @@ public DrainageStrategyService( IProjectAccessService projectAccessService ) { - _context = context; - _projectService = projectService; _logger = loggerFactory.CreateLogger(); - _mapper = mapper; _caseRepository = caseRepository; _repository = repository; _conversionMapperService = conversionMapperService; @@ -48,61 +39,6 @@ IProjectAccessService projectAccessService _projectAccessService = projectAccessService; } - public async Task CreateDrainageStrategy(Guid projectId, Guid sourceCaseId, CreateDrainageStrategyDto drainageStrategyDto) - { - var drainageStrategy = _mapper.Map(drainageStrategyDto); - if (drainageStrategy == null) - { - throw new ArgumentNullException(nameof(drainageStrategy)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - drainageStrategy.Project = project; - var createdDrainageStrategy = _context.DrainageStrategies!.Add(drainageStrategy); - await _context.SaveChangesAsync(); - await SetCaseLink(drainageStrategy, sourceCaseId, project); - return createdDrainageStrategy.Entity; - } - - private async Task SetCaseLink(DrainageStrategy drainageStrategy, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.DrainageStrategyLink = drainageStrategy.Id; - await _context.SaveChangesAsync(); - } - - public async Task GetDrainageStrategy(Guid drainageStrategyId) - { - var drainageStrategy = await _context.DrainageStrategies! - .Include(c => c.Project) - .Include(c => c.ProductionProfileOil) - .Include(c => c.AdditionalProductionProfileOil) - .Include(c => c.ProductionProfileGas) - .Include(c => c.AdditionalProductionProfileGas) - .Include(c => c.ProductionProfileWater) - .Include(c => c.ProductionProfileWaterInjection) - .Include(c => c.FuelFlaringAndLosses) - .Include(c => c.FuelFlaringAndLossesOverride) - .Include(c => c.NetSalesGas) - .Include(c => c.NetSalesGasOverride) - .Include(c => c.Co2Emissions) - .Include(c => c.Co2EmissionsOverride) - .Include(c => c.ProductionProfileNGL) - .Include(c => c.ImportedElectricity) - .Include(c => c.ImportedElectricityOverride) - .Include(c => c.DeferredOilProduction) - .Include(c => c.DeferredGasProduction) - .FirstOrDefaultAsync(o => o.Id == drainageStrategyId); - if (drainageStrategy == null) - { - throw new ArgumentException(string.Format("Drainage strategy {0} not found.", drainageStrategyId)); - } - return drainageStrategy; - } - public async Task GetDrainageStrategyWithIncludes( Guid drainageStrategyId, params Expression>[] includes diff --git a/backend/api/Services/Entities/DrainageStrategy/IDrainageStrategyService.cs b/backend/api/Services/Entities/DrainageStrategy/IDrainageStrategyService.cs index 565947756..b3502226a 100644 --- a/backend/api/Services/Entities/DrainageStrategy/IDrainageStrategyService.cs +++ b/backend/api/Services/Entities/DrainageStrategy/IDrainageStrategyService.cs @@ -7,8 +7,6 @@ namespace api.Services; public interface IDrainageStrategyService { - Task CreateDrainageStrategy(Guid projectId, Guid sourceCaseId, CreateDrainageStrategyDto drainageStrategyDto); - Task GetDrainageStrategy(Guid drainageStrategyId); Task GetDrainageStrategyWithIncludes( Guid drainageStrategyId, params Expression>[] includes diff --git a/backend/api/Services/Entities/Exploration/ExplorationService.cs b/backend/api/Services/Entities/Exploration/ExplorationService.cs index 91d3b66d3..c598bc4f7 100644 --- a/backend/api/Services/Entities/Exploration/ExplorationService.cs +++ b/backend/api/Services/Entities/Exploration/ExplorationService.cs @@ -1,13 +1,10 @@ using System.Linq.Expressions; -using api.Context; using api.Dtos; -using api.Enums; using api.Exceptions; using api.Models; using api.Repositories; -using AutoMapper; using Microsoft.EntityFrameworkCore; @@ -15,79 +12,27 @@ namespace api.Services; public class ExplorationService : IExplorationService { - private readonly DcdDbContext _context; - private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; private readonly ILogger _logger; - private readonly IMapper _mapper; private readonly ICaseRepository _caseRepository; private readonly IExplorationRepository _repository; private readonly IMapperService _mapperService; public ExplorationService( - DcdDbContext context, - IProjectService projectService, ILoggerFactory loggerFactory, - IMapper mapper, ICaseRepository caseRepository, IExplorationRepository repository, IMapperService mapperService, IProjectAccessService projectAccessService ) { - _context = context; - _projectService = projectService; _logger = loggerFactory.CreateLogger(); - _mapper = mapper; _caseRepository = caseRepository; _repository = repository; _mapperService = mapperService; _projectAccessService = projectAccessService; } - public async Task CreateExploration(Guid projectId, Guid sourceCaseId, CreateExplorationDto explorationDto) - { - var exploration = _mapper.Map(explorationDto); - if (exploration == null) - { - throw new ArgumentNullException(nameof(exploration)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - exploration.Project = project; - var createdExploration = _context.Explorations!.Add(exploration); - SetCaseLink(exploration, sourceCaseId, project); - await _context.SaveChangesAsync(); - return createdExploration.Entity; - } - - private static void SetCaseLink(Exploration exploration, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.ExplorationLink = exploration.Id; - } - - public async Task GetExploration(Guid explorationId) - { - var exploration = await _context.Explorations! - .Include(c => c.ExplorationWellCostProfile) - .Include(c => c.AppraisalWellCostProfile) - .Include(c => c.SidetrackCostProfile) - .Include(c => c.GAndGAdminCost) - .Include(c => c.GAndGAdminCostOverride) - .Include(c => c.SeismicAcquisitionAndProcessing) - .Include(c => c.CountryOfficeCost) - .FirstOrDefaultAsync(o => o.Id == explorationId); - if (exploration == null) - { - throw new ArgumentException(string.Format("Exploration {0} not found.", explorationId)); - } - return exploration; - } - public async Task GetExplorationWithIncludes(Guid explorationId, params Expression>[] includes) { return await _repository.GetExplorationWithIncludes(explorationId, includes) diff --git a/backend/api/Services/Entities/Exploration/IExplorationService.cs b/backend/api/Services/Entities/Exploration/IExplorationService.cs index 324ee7824..ad9d79b3b 100644 --- a/backend/api/Services/Entities/Exploration/IExplorationService.cs +++ b/backend/api/Services/Entities/Exploration/IExplorationService.cs @@ -7,8 +7,6 @@ namespace api.Services; public interface IExplorationService { - Task CreateExploration(Guid projectId, Guid sourceCaseId, CreateExplorationDto explorationDto); - Task GetExploration(Guid explorationId); Task GetExplorationWithIncludes(Guid explorationId, params Expression>[] includes); Task UpdateExploration( diff --git a/backend/api/Services/Entities/Substructure/ISubstructureService.cs b/backend/api/Services/Entities/Substructure/ISubstructureService.cs index 27a759589..c002cb6c2 100644 --- a/backend/api/Services/Entities/Substructure/ISubstructureService.cs +++ b/backend/api/Services/Entities/Substructure/ISubstructureService.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Dtos; using api.Models; @@ -5,7 +7,6 @@ namespace api.Services; public interface ISubstructureService { - Task CreateSubstructure(Guid projectId, Guid sourceCaseId, CreateSubstructureDto substructureDto); - Task GetSubstructure(Guid substructureId); + Task GetSubstructureWithIncludes(Guid substructureId, params Expression>[] includes); Task UpdateSubstructure(Guid projectId, Guid caseId, Guid substructureId, TDto updatedSubstructureDto) where TDto : BaseUpdateSubstructureDto; } diff --git a/backend/api/Services/Entities/Substructure/SubstructureService.cs b/backend/api/Services/Entities/Substructure/SubstructureService.cs index 06d159e77..a167ea071 100644 --- a/backend/api/Services/Entities/Substructure/SubstructureService.cs +++ b/backend/api/Services/Entities/Substructure/SubstructureService.cs @@ -1,4 +1,6 @@ +using System.Linq.Expressions; + using api.Context; using api.Dtos; using api.Exceptions; @@ -43,44 +45,10 @@ IProjectAccessService projectAccessService _projectAccessService = projectAccessService; } - public async Task CreateSubstructure(Guid projectId, Guid sourceCaseId, CreateSubstructureDto substructureDto) - { - var substructure = _mapper.Map(substructureDto); - if (substructure == null) - { - throw new ArgumentNullException(nameof(substructure)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - substructure.Project = project; - substructure.LastChangedDate = DateTimeOffset.UtcNow; - var createdSubstructure = _context.Substructures!.Add(substructure); - SetCaseLink(substructure, sourceCaseId, project); - await _context.SaveChangesAsync(); - return createdSubstructure.Entity; - } - - private static void SetCaseLink(Substructure substructure, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.SubstructureLink = substructure.Id; - } - - public async Task GetSubstructure(Guid substructureId) + public async Task GetSubstructureWithIncludes(Guid substructureId, params Expression>[] includes) { - var substructure = await _context.Substructures! - .Include(c => c.CostProfile) - .Include(c => c.CostProfileOverride) - .Include(c => c.CessationCostProfile) - .FirstOrDefaultAsync(o => o.Id == substructureId); - if (substructure == null) - { - throw new ArgumentException(string.Format("Substructure {0} not found.", substructureId)); - } - return substructure; + return await _repository.GetSubstructureWithIncludes(substructureId, includes) + ?? throw new NotFoundInDBException($"Substructure with id {substructureId} not found."); } public async Task UpdateSubstructure( diff --git a/backend/api/Services/Entities/Surf/ISurfService.cs b/backend/api/Services/Entities/Surf/ISurfService.cs index 19f84b0e7..4f60b2aed 100644 --- a/backend/api/Services/Entities/Surf/ISurfService.cs +++ b/backend/api/Services/Entities/Surf/ISurfService.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Dtos; using api.Models; @@ -5,8 +7,7 @@ namespace api.Services; public interface ISurfService { - Task GetSurf(Guid surfId); - Task CreateSurf(Guid projectId, Guid sourceCaseId, CreateSurfDto surfDto); + Task GetSurfWithIncludes(Guid surfId, params Expression>[] includes); Task UpdateSurf( Guid projectId, Guid caseId, diff --git a/backend/api/Services/Entities/Surf/SurfService.cs b/backend/api/Services/Entities/Surf/SurfService.cs index 7c4e7d436..8be911d81 100644 --- a/backend/api/Services/Entities/Surf/SurfService.cs +++ b/backend/api/Services/Entities/Surf/SurfService.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Context; using api.Dtos; using api.Exceptions; @@ -12,73 +14,30 @@ namespace api.Services; public class SurfService : ISurfService { - private readonly DcdDbContext _context; - private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; private readonly ILogger _logger; - private readonly IMapper _mapper; private readonly ISurfRepository _repository; private readonly ICaseRepository _caseRepository; private readonly IMapperService _mapperService; public SurfService( - DcdDbContext context, - IProjectService projectService, ILoggerFactory loggerFactory, - IMapper mapper, ISurfRepository repository, ICaseRepository caseRepository, IMapperService mapperService, IProjectAccessService projectAccessService ) { - _context = context; - _projectService = projectService; _logger = loggerFactory.CreateLogger(); - _mapper = mapper; _repository = repository; _caseRepository = caseRepository; _mapperService = mapperService; _projectAccessService = projectAccessService; } - public async Task GetSurf(Guid surfId) + public async Task GetSurfWithIncludes(Guid surfId, params Expression>[] includes) { - var surf = await _context.Surfs! - .Include(c => c.CostProfile) - .Include(c => c.CostProfileOverride) - .Include(c => c.CessationCostProfile) - .FirstOrDefaultAsync(o => o.Id == surfId); - if (surf == null) - { - throw new ArgumentException(string.Format("Surf {0} not found.", surfId)); - } - return surf; - } - - public async Task CreateSurf(Guid projectId, Guid sourceCaseId, CreateSurfDto surfDto) - { - var surf = _mapper.Map(surfDto); - if (surf == null) - { - throw new ArgumentNullException(nameof(surf)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - surf.Project = project; - surf.LastChangedDate = DateTimeOffset.UtcNow; - var createdSurf = _context.Surfs!.Add(surf); - SetCaseLink(surf, sourceCaseId, project); - await _context.SaveChangesAsync(); - return createdSurf.Entity; - } - - private static void SetCaseLink(Surf surf, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.SurfLink = surf.Id; + return await _repository.GetSurfWithIncludes(surfId, includes) + ?? throw new NotFoundInDBException($"Topside with id {surfId} not found."); } public async Task UpdateSurf( diff --git a/backend/api/Services/Entities/Topside/ITopsideService.cs b/backend/api/Services/Entities/Topside/ITopsideService.cs index acfa1db4b..8dd145a6b 100644 --- a/backend/api/Services/Entities/Topside/ITopsideService.cs +++ b/backend/api/Services/Entities/Topside/ITopsideService.cs @@ -7,8 +7,6 @@ namespace api.Services; public interface ITopsideService { - Task CreateTopside(Guid projectId, Guid sourceCaseId, CreateTopsideDto topsideDto); - Task GetTopside(Guid topsideId); Task UpdateTopside(Guid projectId, Guid caseId, Guid topsideId, TDto updatedTopsideDto) where TDto : BaseUpdateTopsideDto; Task GetTopsideWithIncludes(Guid topsideId, params Expression>[] includes); } diff --git a/backend/api/Services/Entities/Topside/TopsideService.cs b/backend/api/Services/Entities/Topside/TopsideService.cs index b1c1ba163..800492e7a 100644 --- a/backend/api/Services/Entities/Topside/TopsideService.cs +++ b/backend/api/Services/Entities/Topside/TopsideService.cs @@ -14,78 +14,27 @@ namespace api.Services; public class TopsideService : ITopsideService { - private readonly DcdDbContext _context; - private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; private readonly ILogger _logger; - private readonly IMapper _mapper; private readonly ITopsideRepository _repository; private readonly ICaseRepository _caseRepository; private readonly IMapperService _mapperService; public TopsideService( - DcdDbContext context, - IProjectService projectService, ILoggerFactory loggerFactory, - IMapper mapper, ITopsideRepository repository, ICaseRepository caseRepository, IMapperService mapperService, IProjectAccessService projectAccessService ) { - _context = context; - _projectService = projectService; _logger = loggerFactory.CreateLogger(); - _mapper = mapper; _repository = repository; _caseRepository = caseRepository; _mapperService = mapperService; _projectAccessService = projectAccessService; } - public async Task CreateTopside(Guid projectId, Guid sourceCaseId, CreateTopsideDto topsideDto) - { - var topside = _mapper.Map(topsideDto); - if (topside == null) - { - throw new ArgumentNullException(nameof(topside)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - topside.Project = project; - topside.LastChangedDate = DateTimeOffset.UtcNow; - var createdTopside = _context.Topsides!.Add(topside); - SetCaseLink(topside, sourceCaseId, project); - await _context.SaveChangesAsync(); - - return createdTopside.Entity; - } - - private static void SetCaseLink(Topside topside, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.TopsideLink = topside.Id; - } - - public async Task GetTopside(Guid topsideId) - { - var topside = await _context.Topsides! - .Include(c => c.Project) - .Include(c => c.CostProfile) - .Include(c => c.CostProfileOverride) - .Include(c => c.CessationCostProfile) - .FirstOrDefaultAsync(o => o.Id == topsideId); - if (topside == null) - { - throw new ArgumentException(string.Format("Topside {0} not found.", topsideId)); - } - return topside; - } - public async Task GetTopsideWithIncludes(Guid topsideId, params Expression>[] includes) { return await _repository.GetTopsideWithIncludes(topsideId, includes) diff --git a/backend/api/Services/Entities/Transport/ITransportService.cs b/backend/api/Services/Entities/Transport/ITransportService.cs index 8411b2ad2..a1353fcba 100644 --- a/backend/api/Services/Entities/Transport/ITransportService.cs +++ b/backend/api/Services/Entities/Transport/ITransportService.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Dtos; using api.Models; @@ -5,8 +7,7 @@ namespace api.Services; public interface ITransportService { - Task CreateTransport(Guid projectId, Guid sourceCaseId, CreateTransportDto transportDto); - Task GetTransport(Guid transportId); + Task GetTransportWithIncludes(Guid transportId, params Expression>[] includes); Task UpdateTransport(Guid projectId, Guid caseId, Guid transportId, TDto updatedTransportDto) where TDto : BaseUpdateTransportDto; } diff --git a/backend/api/Services/Entities/Transport/TransportService.cs b/backend/api/Services/Entities/Transport/TransportService.cs index c1aaa0bfb..1ec24a05c 100644 --- a/backend/api/Services/Entities/Transport/TransportService.cs +++ b/backend/api/Services/Entities/Transport/TransportService.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Context; using api.Dtos; using api.Exceptions; @@ -12,75 +14,31 @@ namespace api.Services; public class TransportService : ITransportService { - private readonly DcdDbContext _context; - private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; private readonly ILogger _logger; - private readonly IMapper _mapper; private readonly ICaseRepository _caseRepository; private readonly ITransportRepository _repository; private readonly IMapperService _mapperService; public TransportService( - DcdDbContext context, - IProjectService projectService, ILoggerFactory loggerFactory, - IMapper mapper, ICaseRepository caseRepository, ITransportRepository transportRepository, IMapperService mapperService, IProjectAccessService projectAccessService ) { - _context = context; - _projectService = projectService; _logger = loggerFactory.CreateLogger(); - _mapper = mapper; _caseRepository = caseRepository; _repository = transportRepository; _mapperService = mapperService; _projectAccessService = projectAccessService; } - public async Task CreateTransport(Guid projectId, Guid sourceCaseId, CreateTransportDto transportDto) - { - var transport = _mapper.Map(transportDto); - if (transport == null) - { - throw new ArgumentNullException(nameof(transport)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - transport.Project = project; - transport.LastChangedDate = DateTimeOffset.UtcNow; - var createdTransport = _context.Transports!.Add(transport); - await _context.SaveChangesAsync(); - await SetCaseLink(transport, sourceCaseId, project); - return createdTransport.Entity; - } - - private async Task SetCaseLink(Transport transport, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.TransportLink = transport.Id; - await _context.SaveChangesAsync(); - } - - public async Task GetTransport(Guid transportId) + public async Task GetTransportWithIncludes(Guid transportId, params Expression>[] includes) { - var transport = await _context.Transports! - .Include(c => c.CostProfile) - .Include(c => c.CostProfileOverride) - .Include(c => c.CessationCostProfile) - .FirstOrDefaultAsync(c => c.Id == transportId); - if (transport == null) - { - throw new ArgumentException(string.Format("Transport {0} not found.", transportId)); - } - return transport; + return await _repository.GetTransportWithIncludes(transportId, includes) + ?? throw new NotFoundInDBException($"Transport with id {transportId} not found."); } public async Task UpdateTransport(Guid projectId, Guid caseId, Guid transportId, TDto updatedTransportDto) diff --git a/backend/api/Services/Entities/WellProject/IWellProjectService.cs b/backend/api/Services/Entities/WellProject/IWellProjectService.cs index 1b6799654..f227911fb 100644 --- a/backend/api/Services/Entities/WellProject/IWellProjectService.cs +++ b/backend/api/Services/Entities/WellProject/IWellProjectService.cs @@ -1,3 +1,5 @@ +using System.Linq.Expressions; + using api.Dtos; using api.Models; @@ -5,8 +7,7 @@ namespace api.Services; public interface IWellProjectService { - Task CreateWellProject(Guid projectId, Guid sourceCaseId, CreateWellProjectDto wellProjectDto); - Task GetWellProject(Guid wellProjectId); + Task GetWellProjectWithIncludes(Guid wellProjectId, params Expression>[] includes); Task UpdateWellProject( Guid projectId, Guid caseId, diff --git a/backend/api/Services/Entities/WellProject/WellProjectService.cs b/backend/api/Services/Entities/WellProject/WellProjectService.cs index e3bd11ab6..dc1a9520b 100644 --- a/backend/api/Services/Entities/WellProject/WellProjectService.cs +++ b/backend/api/Services/Entities/WellProject/WellProjectService.cs @@ -1,95 +1,41 @@ -using api.Context; +using System.Linq.Expressions; + using api.Dtos; -using api.Enums; using api.Exceptions; using api.Models; using api.Repositories; -using AutoMapper; - using Microsoft.EntityFrameworkCore; namespace api.Services; public class WellProjectService : IWellProjectService { - private readonly DcdDbContext _context; - private readonly IProjectService _projectService; private readonly IProjectAccessService _projectAccessService; private readonly ILogger _logger; - private readonly IMapper _mapper; private readonly IWellProjectRepository _repository; private readonly ICaseRepository _caseRepository; private readonly IMapperService _mapperService; public WellProjectService( - DcdDbContext context, - IProjectService projectService, ILoggerFactory loggerFactory, - IMapper mapper, IWellProjectRepository repository, ICaseRepository caseRepository, IMapperService mapperService, IProjectAccessService projectAccessService - ) + ) { - _context = context; - _projectService = projectService; _logger = loggerFactory.CreateLogger(); - _mapper = mapper; _repository = repository; _caseRepository = caseRepository; _mapperService = mapperService; _projectAccessService = projectAccessService; } - public async Task CreateWellProject( - Guid projectId, - Guid sourceCaseId, - CreateWellProjectDto wellProjectDto - ) - { - var wellProject = _mapper.Map(wellProjectDto); - if (wellProject == null) - { - throw new ArgumentNullException(nameof(wellProject)); - } - var project = await _projectService.GetProjectWithCasesAndAssets(projectId); - wellProject.Project = project; - var createdWellProject = _context.WellProjects!.Add(wellProject); - await _context.SaveChangesAsync(); - await SetCaseLink(wellProject, sourceCaseId, project); - return createdWellProject.Entity; - } - - private async Task SetCaseLink(WellProject wellProject, Guid sourceCaseId, Project project) - { - var case_ = project.Cases!.FirstOrDefault(o => o.Id == sourceCaseId); - if (case_ == null) - { - throw new NotFoundInDBException(string.Format("Case {0} not found in database.", sourceCaseId)); - } - case_.WellProjectLink = wellProject.Id; - await _context.SaveChangesAsync(); - } - - public async Task GetWellProject(Guid wellProjectId) + public async Task GetWellProjectWithIncludes(Guid wellProjectId, params Expression>[] includes) { - var wellProject = await _context.WellProjects! - .Include(c => c.OilProducerCostProfile) - .Include(c => c.OilProducerCostProfileOverride) - .Include(c => c.GasProducerCostProfile) - .Include(c => c.GasProducerCostProfileOverride) - .Include(c => c.WaterInjectorCostProfile) - .Include(c => c.WaterInjectorCostProfileOverride) - .Include(c => c.GasInjectorCostProfile) - .Include(c => c.GasInjectorCostProfileOverride) - .FirstOrDefaultAsync(o => o.Id == wellProjectId); - if (wellProject == null) - { - throw new ArgumentException(string.Format("Well project {0} not found.", wellProjectId)); - } - return wellProject; + return await _repository.GetWellProjectWithIncludes(wellProjectId, includes) + ?? throw new NotFoundInDBException($"WellProject with id {wellProjectId} not found."); } public async Task UpdateWellProject( diff --git a/backend/api/Services/GenerateCostProfiles/CessationCostProfileService.cs b/backend/api/Services/GenerateCostProfiles/CessationCostProfileService.cs index 6fa00009e..118e7d401 100644 --- a/backend/api/Services/GenerateCostProfiles/CessationCostProfileService.cs +++ b/backend/api/Services/GenerateCostProfiles/CessationCostProfileService.cs @@ -72,9 +72,8 @@ private async Task CalculateCessationWellsCost(Case caseItem, Project project, i return; } - var wellProject = await _wellProjectService.GetWellProject(caseItem.WellProjectLink); caseItem.CessationWellsCost = await GenerateCessationWellsCost( - wellProject, + caseItem.WellProjectLink, project, lastYear.Value, caseItem.CessationWellsCost ?? new CessationWellsCost() @@ -94,7 +93,7 @@ private async Task GetCessationOffshoreFacilitiesCost(Case caseItem, int? lastYe return; } - var surf = await _surfService.GetSurf(caseItem.SurfLink); + var surf = await _surfService.GetSurfWithIncludes(caseItem.SurfLink); caseItem.CessationOffshoreFacilitiesCost = GenerateCessationOffshoreFacilitiesCost( surf, lastYear.Value, @@ -102,9 +101,9 @@ private async Task GetCessationOffshoreFacilitiesCost(Case caseItem, int? lastYe ); } - private async Task GenerateCessationWellsCost(WellProject wellProject, Project project, int lastYear, CessationWellsCost cessationWells) + private async Task GenerateCessationWellsCost(Guid wellProjectId, Project project, int lastYear, CessationWellsCost cessationWells) { - var linkedWells = await _wellProjectWellService.GetWellProjectWellsForWellProject(wellProject.Id); + var linkedWells = await _wellProjectWellService.GetWellProjectWellsForWellProject(wellProjectId); if (linkedWells == null) { return cessationWells; diff --git a/backend/api/Services/GenerateCostProfiles/Co2DrillingFlaringFuelTotalsService.cs b/backend/api/Services/GenerateCostProfiles/Co2DrillingFlaringFuelTotalsService.cs index a556f2e54..c1fd8c7c5 100644 --- a/backend/api/Services/GenerateCostProfiles/Co2DrillingFlaringFuelTotalsService.cs +++ b/backend/api/Services/GenerateCostProfiles/Co2DrillingFlaringFuelTotalsService.cs @@ -44,11 +44,10 @@ public async Task Generate(Guid caseId) d => d.AdditionalProductionProfileGas!, d => d.ProductionProfileWaterInjection! ); - var wellProject = await _wellProjectService.GetWellProject(caseItem.WellProjectLink); var fuelConsumptionsTotal = GetFuelConsumptionsProfileTotal(project, caseItem, topside, drainageStrategy); var flaringsTotal = GetFlaringsProfileTotal(project, drainageStrategy); - var drillingEmissionsTotal = await CalculateDrillingEmissionsTotal(project, wellProject); + var drillingEmissionsTotal = await CalculateDrillingEmissionsTotal(project, caseItem.WellProjectLink); var co2DrillingFlaringFuelTotals = new Co2DrillingFlaringFuelTotalsDto { @@ -90,9 +89,9 @@ DrainageStrategy drainageStrategy return fuelConsumptionsProfile.Values.Sum() / 1000; } - private async Task CalculateDrillingEmissionsTotal(Project project, WellProject wellProject) + private async Task CalculateDrillingEmissionsTotal(Project project, Guid wellProjectId) { - var linkedWells = await _wellProjectWellService.GetWellProjectWellsForWellProject(wellProject.Id); + var linkedWells = await _wellProjectWellService.GetWellProjectWellsForWellProject(wellProjectId); if (linkedWells == null) { return 0.0; diff --git a/backend/api/Services/GenerateCostProfiles/Co2IntensityProfileService.cs b/backend/api/Services/GenerateCostProfiles/Co2IntensityProfileService.cs index cc5474e5a..0db01b0aa 100644 --- a/backend/api/Services/GenerateCostProfiles/Co2IntensityProfileService.cs +++ b/backend/api/Services/GenerateCostProfiles/Co2IntensityProfileService.cs @@ -34,7 +34,15 @@ public async Task Generate(Guid caseId) { var caseItem = await _caseService.GetCase(caseId); var project = await _projectService.GetProjectWithoutAssets(caseItem.ProjectId); - var drainageStrategy = await _drainageStrategyService.GetDrainageStrategy(caseItem.DrainageStrategyLink); + var drainageStrategy = await _drainageStrategyService.GetDrainageStrategyWithIncludes( + caseItem.DrainageStrategyLink, + d => d.Co2Emissions!, + d => d.Co2EmissionsOverride!, + d => d.ProductionProfileOil!, + d => d.AdditionalProductionProfileOil!, + d => d.ProductionProfileGas!, + d => d.AdditionalProductionProfileGas! + ); var totalExportedVolumes = GetTotalExportedVolumes(drainageStrategy); diff --git a/backend/api/Services/GenerateCostProfiles/Co2IntensityTotalService.cs b/backend/api/Services/GenerateCostProfiles/Co2IntensityTotalService.cs index 3b580904a..60e0db21b 100644 --- a/backend/api/Services/GenerateCostProfiles/Co2IntensityTotalService.cs +++ b/backend/api/Services/GenerateCostProfiles/Co2IntensityTotalService.cs @@ -28,7 +28,15 @@ public async Task Calculate(Guid caseId) { var caseItem = await _caseService.GetCase(caseId); var project = await _projectService.GetProjectWithCasesAndAssets(caseItem.ProjectId); - var drainageStrategy = await _drainageStrategyService.GetDrainageStrategy(caseItem.DrainageStrategyLink); + var drainageStrategy = await _drainageStrategyService.GetDrainageStrategyWithIncludes( + caseItem.DrainageStrategyLink, + d => d.Co2Emissions!, + d => d.Co2EmissionsOverride!, + d => d.ProductionProfileOil!, + d => d.AdditionalProductionProfileOil!, + d => d.ProductionProfileGas!, + d => d.AdditionalProductionProfileGas! + ); var generateCo2EmissionsProfile = new Co2EmissionsDto(); if (drainageStrategy.Co2EmissionsOverride?.Override == true) diff --git a/backend/api/Services/GenerateCostProfiles/StudyCostProfileService.cs b/backend/api/Services/GenerateCostProfiles/StudyCostProfileService.cs index 0676240cb..6b14b0299 100644 --- a/backend/api/Services/GenerateCostProfiles/StudyCostProfileService.cs +++ b/backend/api/Services/GenerateCostProfiles/StudyCostProfileService.cs @@ -171,7 +171,11 @@ public async Task SumAllCostFacility(Case caseItem) { var sumFacilityCost = 0.0; - var substructure = await _substructureService.GetSubstructure(caseItem.SubstructureLink); + var substructure = await _substructureService.GetSubstructureWithIncludes( + caseItem.SubstructureLink, + s => s.CostProfileOverride!, + s => s.CostProfile! + ); if (substructure.CostProfileOverride?.Override == true) { sumFacilityCost += substructure.CostProfileOverride.Values.Sum(); @@ -182,7 +186,11 @@ public async Task SumAllCostFacility(Case caseItem) } - var surf = await _surfService.GetSurf(caseItem.SurfLink); + var surf = await _surfService.GetSurfWithIncludes( + caseItem.SurfLink, + s => s.CostProfileOverride!, + s => s.CostProfile! + ); if (surf.CostProfileOverride?.Override == true) { sumFacilityCost += surf.CostProfileOverride.Values.Sum(); @@ -193,7 +201,11 @@ public async Task SumAllCostFacility(Case caseItem) } - var topside = await _topsideService.GetTopside(caseItem.TopsideLink); + var topside = await _topsideService.GetTopsideWithIncludes( + caseItem.TopsideLink, + t => t.CostProfileOverride!, + t => t.CostProfile! + ); if (topside.CostProfileOverride?.Override == true) { sumFacilityCost += topside.CostProfileOverride.Values.Sum(); @@ -204,7 +216,11 @@ public async Task SumAllCostFacility(Case caseItem) } - var transport = await _transportService.GetTransport(caseItem.TransportLink); + var transport = await _transportService.GetTransportWithIncludes( + caseItem.TransportLink, + t => t.CostProfileOverride!, + t => t.CostProfile! + ); if (transport.CostProfileOverride?.Override == true) { sumFacilityCost += transport.CostProfileOverride.Values.Sum(); @@ -221,7 +237,17 @@ public async Task SumWellCost(Case caseItem) { var sumWellCost = 0.0; - var wellProject = await _wellProjectService.GetWellProject(caseItem.WellProjectLink); + var wellProject = await _wellProjectService.GetWellProjectWithIncludes( + caseItem.WellProjectLink, + w => w.OilProducerCostProfileOverride!, + w => w.OilProducerCostProfile!, + w => w.GasProducerCostProfileOverride!, + w => w.GasProducerCostProfile!, + w => w.WaterInjectorCostProfileOverride!, + w => w.WaterInjectorCostProfile!, + w => w.GasInjectorCostProfileOverride!, + w => w.GasInjectorCostProfile! + ); if (wellProject.OilProducerCostProfileOverride?.Override == true) { diff --git a/backend/api/Services/ProspExcelImportService.cs b/backend/api/Services/ProspExcelImportService.cs index 824e5a367..9d324c4ec 100644 --- a/backend/api/Services/ProspExcelImportService.cs +++ b/backend/api/Services/ProspExcelImportService.cs @@ -357,7 +357,7 @@ private async Task ImportTransport(List cellData, Guid sourceCaseId, Guid await _transportTimeSeriesService.AddOrUpdateTransportCostProfile(projectId, sourceCaseId, transportLink, costProfile); } - public async Task ImportProsp(Stream stream, Guid sourceCaseId, Guid projectId, Dictionary assets, + public async Task ImportProsp(Stream stream, Guid sourceCaseId, Guid projectId, Dictionary assets, string sharepointFileId, string? sharepointFileName, string? sharepointFileUrl) { using var document = SpreadsheetDocument.Open(stream, false); @@ -422,10 +422,8 @@ public async Task ImportProsp(Stream stream, Guid sourceCa SharepointFileUrl = sharepointFileUrl, }; - return await _caseService.UpdateCaseAndProfiles(sourceCaseId, caseDto); + await _caseService.UpdateCase(projectId, sourceCaseId, caseDto); } - - return await _projectService.GetProjectDto(projectId); } public async Task ClearImportedProspData(Guid sourceCaseId, Guid projectId) @@ -447,7 +445,7 @@ public async Task ClearImportedProspData(Guid sourceCaseId, Guid projectId) throw new Exception(); } - await _caseService.UpdateCaseAndProfiles(sourceCaseId, caseDto); + await _caseService.UpdateCase(projectId, sourceCaseId, caseDto); } private async Task ClearImportedSurf(Case caseItem) diff --git a/backend/api/Services/ProspSharepointImportService.cs b/backend/api/Services/ProspSharepointImportService.cs index c99919be9..6c3e55b5d 100644 --- a/backend/api/Services/ProspSharepointImportService.cs +++ b/backend/api/Services/ProspSharepointImportService.cs @@ -164,12 +164,12 @@ private async Task> GetSiteIdAndParentReferencePath(string? url) : null; } - public async Task ConvertSharepointFilesToProjectDto(Guid projectId, SharePointImportDto[] dtos) + public async Task ConvertSharepointFilesToProjectDto(Guid projectId, SharePointImportDto[] dtos) { var projectDto = new ProjectWithAssetsDto(); if (string.IsNullOrWhiteSpace(dtos.FirstOrDefault()?.SharePointSiteUrl)) { - return projectDto; + return; } foreach (var importDto in dtos) @@ -186,7 +186,7 @@ public async Task ConvertSharepointFilesToProjectDto(Guid var siteId = GetSiteIdAndParentReferencePath(dtos.FirstOrDefault()!.SharePointSiteUrl)?.Result[0]; if (siteId == null) { - return projectDto; + return; } var driveId = await GetDriveIdFromSharePointSiteUrl(dtos, siteId); @@ -224,7 +224,7 @@ public async Task ConvertSharepointFilesToProjectDto(Guid var assets = MapAssets(iteminfo.Surf, iteminfo.Substructure, iteminfo.Topside, iteminfo.Transport); - projectDto = await _prospExcelImportService.ImportProsp(caseWithFileStream.Value, caseWithFileStream.Key, + await _prospExcelImportService.ImportProsp(caseWithFileStream.Value, caseWithFileStream.Key, projectId, assets, iteminfo.SharePointFileId, @@ -232,8 +232,6 @@ public async Task ConvertSharepointFilesToProjectDto(Guid iteminfo.SharePointFileUrl); } } - - return projectDto; } private async Task GetDriveIdFromSharePointSiteUrl(SharePointImportDto[] dtos, string siteId) diff --git a/backend/tests/Services/SurfServiceTests.cs b/backend/tests/Services/SurfServiceTests.cs index 3e6312f67..b804e9412 100644 --- a/backend/tests/Services/SurfServiceTests.cs +++ b/backend/tests/Services/SurfServiceTests.cs @@ -17,9 +17,7 @@ namespace tests.Services public class SurfServiceTests { private readonly SurfService _surfService; - private readonly IProjectService _projectService = Substitute.For(); private readonly ILoggerFactory _loggerFactory = Substitute.For(); - private readonly IMapper _mapper = Substitute.For(); private readonly ISurfRepository _repository = Substitute.For(); private readonly ICaseRepository _caseRepository = Substitute.For(); private readonly IMapperService _mapperService = Substitute.For(); @@ -28,16 +26,8 @@ public class SurfServiceTests public SurfServiceTests() { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "TestDb") - .Options; - - var context = Substitute.For(options); _surfService = new SurfService( - context, - _projectService, _loggerFactory, - _mapper, _repository, _caseRepository, _mapperService, diff --git a/backend/tests/Services/TopsideServiceTests.cs b/backend/tests/Services/TopsideServiceTests.cs index 43fd47811..83ea04a7f 100644 --- a/backend/tests/Services/TopsideServiceTests.cs +++ b/backend/tests/Services/TopsideServiceTests.cs @@ -17,9 +17,7 @@ namespace tests.Services public class TopsideServiceTests { private readonly TopsideService _topsideService; - private readonly IProjectService _projectService = Substitute.For(); private readonly ILoggerFactory _loggerFactory = Substitute.For(); - private readonly IMapper _mapper = Substitute.For(); private readonly ITopsideRepository _repository = Substitute.For(); private readonly ICaseRepository _caseRepository = Substitute.For(); private readonly IMapperService _mapperService = Substitute.For(); @@ -28,16 +26,8 @@ public class TopsideServiceTests public TopsideServiceTests() { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "TestDb") - .Options; - - var context = Substitute.For(options); _topsideService = new TopsideService( - context, - _projectService, _loggerFactory, - _mapper, _repository, _caseRepository, _mapperService, diff --git a/backend/tests/Services/TransportServiceTests.cs b/backend/tests/Services/TransportServiceTests.cs index 05c21bb05..92585165e 100644 --- a/backend/tests/Services/TransportServiceTests.cs +++ b/backend/tests/Services/TransportServiceTests.cs @@ -17,9 +17,7 @@ namespace tests.Services public class TransportServiceTests { private readonly TransportService _transportService; - private readonly IProjectService _projectService = Substitute.For(); private readonly ILoggerFactory _loggerFactory = Substitute.For(); - private readonly IMapper _mapper = Substitute.For(); private readonly ITransportRepository _repository = Substitute.For(); private readonly ICaseRepository _caseRepository = Substitute.For(); private readonly IMapperService _mapperService = Substitute.For(); @@ -28,16 +26,8 @@ public class TransportServiceTests public TransportServiceTests() { - var options = new DbContextOptionsBuilder() - .UseInMemoryDatabase(databaseName: "TestDb") - .Options; - - var context = Substitute.For(options); _transportService = new TransportService( - context, - _projectService, _loggerFactory, - _mapper, _caseRepository, _repository, _mapperService,