Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor create case and remove create methods from assets #1299

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion backend/api/Controllers/CasesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ 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;
}

[HttpPost]
public async Task<ProjectWithAssetsDto> CreateCase([FromRoute] Guid projectId, [FromBody] CreateCaseDto caseDto)
{
return await _caseService.CreateCase(projectId, caseDto);
return await _createCaseService.CreateCase(projectId, caseDto);
}

[HttpPost("copy", Name = "Duplicate")]
Expand Down
13 changes: 3 additions & 10 deletions backend/api/Controllers/PROSPController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,10 @@ public async Task<ActionResult<ProjectWithAssetsDto>> 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)
{
Expand Down
1 change: 1 addition & 0 deletions backend/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@
builder.Services.AddScoped<IProjectService, ProjectService>();
builder.Services.AddScoped<IFusionService, FusionService>();
builder.Services.AddScoped<ICaseService, CaseService>();
builder.Services.AddScoped<ICreateCaseService, CreateCaseService>();
builder.Services.AddScoped<IDrainageStrategyService, DrainageStrategyService>();
builder.Services.AddScoped<IWellProjectService, WellProjectService>();
builder.Services.AddScoped<IExplorationService, ExplorationService>();
Expand Down
8 changes: 8 additions & 0 deletions backend/api/Repositories/Case/CaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ ILogger<CaseRepository> logger
_logger = logger;
}

public async Task<Case> AddCase(Case caseItem)
{
_context.Cases.Add(caseItem);
await _context.SaveChangesAsync();

return caseItem;
}

public async Task<Case?> GetCase(Guid caseId)
{
return await Get<Case>(caseId);
Expand Down
1 change: 1 addition & 0 deletions backend/api/Repositories/Case/ICaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace api.Repositories;

public interface ICaseRepository : IBaseRepository
{
Task<Case> AddCase(Case caseItem);
Task<Case?> GetCase(Guid caseId);
Task<Case?> GetCaseWithIncludes(Guid caseId, params Expression<Func<Case, object>>[] includes);
Case UpdateCase(Case updatedCase);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Linq.Expressions;

using api.Models;

namespace api.Repositories;

public interface ISubstructureRepository : IBaseRepository
{
Task<Substructure?> GetSubstructure(Guid substructureId);
Task<Substructure?> GetSubstructureWithIncludes(Guid substructureId, params Expression<Func<Substructure, object>>[] includes);
Task<Substructure?> GetSubstructureWithCostProfile(Guid substructureId);
Task<bool> SubstructureHasCostProfileOverride(Guid substructureId);
Substructure UpdateSubstructure(Substructure substructure);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq.Expressions;

using api.Context;
using api.Models;

Expand All @@ -18,6 +20,11 @@ public SubstructureRepository(DcdDbContext context) : base(context)
return await Get<Substructure>(substructureId);
}

public async Task<Substructure?> GetSubstructureWithIncludes(Guid substructureId, params Expression<Func<Substructure, object>>[] includes)
{
return await GetWithIncludes(substructureId, includes);
}

public async Task<Substructure?> GetSubstructureWithCostProfile(Guid substructureId)
{
return await _context.Substructures
Expand Down
3 changes: 3 additions & 0 deletions backend/api/Repositories/Surf/ISurfRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System.Linq.Expressions;

using api.Models;

namespace api.Repositories;

public interface ISurfRepository : IBaseRepository
{
Task<Surf?> GetSurfWithIncludes(Guid surfId, params Expression<Func<Surf, object>>[] includes);
Task<Surf?> GetSurf(Guid surfId);
Task<Surf?> GetSurfWithCostProfile(Guid surfId);
Task<bool> SurfHasCostProfileOverride(Guid topsideId);
Expand Down
7 changes: 7 additions & 0 deletions backend/api/Repositories/Surf/SurfRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq.Expressions;

using api.Context;
using api.Models;

Expand All @@ -18,6 +20,11 @@ public SurfRepository(DcdDbContext context) : base(context)
return await Get<Surf>(surfId);
}

public async Task<Surf?> GetSurfWithIncludes(Guid surfId, params Expression<Func<Surf, object>>[] includes)
{
return await GetWithIncludes(surfId, includes);
}

public async Task<Surf?> GetSurfWithCostProfile(Guid surfId)
{
return await _context.Surfs
Expand Down
3 changes: 3 additions & 0 deletions backend/api/Repositories/Transport/ITransportRepository.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Linq.Expressions;

using api.Models;

namespace api.Repositories;

public interface ITransportRepository : IBaseRepository
{
Task<Transport?> GetTransport(Guid transportId);
Task<Transport?> GetTransportWithIncludes(Guid transportId, params Expression<Func<Transport, object>>[] includes);
Task<Transport?> GetTransportWithCostProfile(Guid transportId);
Task<bool> TransportHasCostProfileOverride(Guid transportId);
Transport UpdateTransport(Transport transport);
Expand Down
7 changes: 7 additions & 0 deletions backend/api/Repositories/Transport/TransportRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq.Expressions;

using api.Context;
using api.Models;

Expand All @@ -18,6 +20,11 @@ public TransportRepository(DcdDbContext context) : base(context)
return await Get<Transport>(transportId);
}

public async Task<Transport?> GetTransportWithIncludes(Guid transportId, params Expression<Func<Transport, object>>[] includes)
{
return await GetWithIncludes(transportId, includes);
}

public async Task<Transport?> GetTransportWithCostProfile(Guid transportId)
{
return await _context.Transports
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Linq.Expressions;

using api.Enums;
using api.Models;

Expand All @@ -6,6 +8,7 @@ namespace api.Repositories;
public interface IWellProjectRepository : IBaseRepository
{
Task<WellProject?> GetWellProject(Guid wellProjectId);
Task<WellProject?> GetWellProjectWithIncludes(Guid wellProjectId, params Expression<Func<WellProject, object>>[] includes);
Task<Well?> GetWell(Guid wellId);
Task<bool> WellProjectHasProfile(Guid WellProjectId, WellProjectProfileNames profileType);
WellProject UpdateWellProject(WellProject wellProject);
Expand Down
5 changes: 5 additions & 0 deletions backend/api/Repositories/WellProject/WellProjectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public WellProjectRepository(DcdDbContext context) : base(context)
return await Get<WellProject>(wellProjectId);
}

public async Task<WellProject?> GetWellProjectWithIncludes(Guid wellProjectId, params Expression<Func<WellProject, object>>[] includes)
{
return await GetWithIncludes(wellProjectId, includes);
}

public async Task<Well?> GetWell(Guid wellId)
{
return await Get<Well>(wellId);
Expand Down
135 changes: 1 addition & 134 deletions backend/api/Services/Entities/Case/CaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CaseService> _logger;
private readonly IMapper _mapper;
private readonly IMapperService _mapperService;
Expand All @@ -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,
Expand All @@ -49,114 +35,13 @@ IProjectAccessService projectAccessService
{
_context = context;
_projectService = projectService;
_drainageStrategyService = drainageStrategyService;
_topsideService = topsideService;
_surfService = surfService;
_substructureService = substructureService;
_transportService = transportService;
_explorationService = explorationService;
_wellProjectService = wellProjectService;
_logger = loggerFactory.CreateLogger<CaseService>();
_mapper = mapper;
_mapperService = mapperService;
_repository = repository;
_projectAccessService = projectAccessService;
}

public async Task<ProjectWithAssetsDto> CreateCase(Guid projectId, CreateCaseDto createCaseDto)
{
var caseItem = _mapper.Map<Case>(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<ProjectWithAssetsDto> UpdateCaseAndProfiles<TDto>(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<ProjectWithAssetsDto> DeleteCase(Guid projectId, Guid caseId)
{
// Need to verify that the project from the URL is the same as the project of the resource
Expand Down Expand Up @@ -208,25 +93,7 @@ public async Task<Case> GetCaseWithIncludes(Guid caseId, params Expression<Func<
// TODO: Delete this method
public async Task<IEnumerable<Case>> 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<CaseDto> UpdateCase<TDto>(
Expand Down
Loading