Skip to content

Commit 738830e

Browse files
authored
feat: implement get current instance endpoint (#69)
1 parent d393a0f commit 738830e

File tree

22 files changed

+460
-123
lines changed

22 files changed

+460
-123
lines changed

src/Adapters/Driving/Web/EndpointDefinitions/Analysis/DownloadAnalysisFileResultsEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private static async Task<DownloadResult> DownloadIndividualFile(
5959
namedStream.Name
6060
),
6161
error => error.ToProblem(context),
62-
error =>error.ToProblem(context)
62+
error => error.ToProblem(context)
6363
);
6464
}
6565

src/Adapters/Driving/Web/EndpointDefinitions/Analysis/GetAnalysisResultsEndpoint.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static async Task<
5151

5252
return result.Match<Results<Ok<WebGetAnalysisResultConsole>, NotFound<ProblemDetails>>>(
5353
content => TypedResults.Ok(new WebGetAnalysisResultConsole(content)),
54-
error =>error.ToProblem(context)
54+
error => error.ToProblem(context)
5555
);
5656
}
5757

@@ -95,7 +95,7 @@ private static async Task<
9595

9696
return result.Match<Results<Ok<WebGetAnalysisResultFileList>, NotFound<ProblemDetails>>>(
9797
files => TypedResults.Ok(WebGetAnalysisResultFileList.Map(files)),
98-
error =>error.ToProblem(context)
98+
error => error.ToProblem(context)
9999
);
100100
}
101101
}

src/Adapters/Driving/Web/EndpointDefinitions/Context/ProjectContextLinkEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CancellationToken cancellationToken
4343
return result.Match<Results<NoContent, NotFound<ProblemDetails>>>(
4444
_ => TypedResults.NoContent(),
4545
error => error.ToProblem(context),
46-
error =>error.ToProblem(context)
46+
error => error.ToProblem(context)
4747
);
4848
}
4949
}

src/Adapters/Driving/Web/EndpointDefinitions/Context/ProjectContextReloadEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CancellationToken cancellationToken
4141
return result.Match<Results<NoContent, NotFound<ProblemDetails>>>(
4242
_ => TypedResults.NoContent(),
4343
error => error.ToProblem(context),
44-
error =>error.ToProblem(context)
44+
error => error.ToProblem(context)
4545
);
4646
}
4747
}

src/Adapters/Driving/Web/EndpointDefinitions/Instances/Contracts/WebGetProjectInstanceInfo.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Microsoft.AspNetCore.Http.HttpResults;
2+
using Microsoft.AspNetCore.Mvc;
3+
using ScriptBee.Common.Web;
4+
using ScriptBee.Domain.Model.Project;
5+
using ScriptBee.Service.Project.Analysis;
6+
using ScriptBee.UseCases.Project.Analysis;
7+
using ScriptBee.Web.EndpointDefinitions.Instances.Contracts;
8+
using ScriptBee.Web.Exceptions;
9+
10+
namespace ScriptBee.Web.EndpointDefinitions.Instances;
11+
12+
using GetCurrentInstanceType = Results<Ok<WebProjectInstance>, NotFound<ProblemDetails>>;
13+
14+
public class GetCurrentInstanceEndpoint : IEndpointDefinition
15+
{
16+
public void DefineServices(IServiceCollection services)
17+
{
18+
services.AddSingleton<IGetCurrentInstanceUseCase, GetCurrentInstanceService>();
19+
}
20+
21+
public void DefineEndpoints(IEndpointRouteBuilder app)
22+
{
23+
app.MapGet("/api/projects/{projectId}/instances/current", GetCurrentInstance);
24+
}
25+
26+
private static async Task<GetCurrentInstanceType> GetCurrentInstance(
27+
HttpContext context,
28+
[FromRoute] string projectId,
29+
IGetCurrentInstanceUseCase useCase,
30+
CancellationToken cancellationToken
31+
)
32+
{
33+
var result = await useCase.GetCurrentInstance(
34+
ProjectId.FromValue(projectId),
35+
cancellationToken
36+
);
37+
38+
return result.Match<GetCurrentInstanceType>(
39+
instanceInfo => TypedResults.Ok(WebProjectInstance.Map(instanceInfo)),
40+
error => error.ToNotFoundProblem(context)
41+
);
42+
}
43+
}

src/Adapters/Driving/Web/EndpointDefinitions/Instances/GetProjectInstancesEndpoint.cs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@ public class GetProjectInstancesEndpoint : IEndpointDefinition
1313
public void DefineServices(IServiceCollection services)
1414
{
1515
services.AddSingleton<IGetProjectInstancesUseCase, GetProjectInstancesService>();
16-
services.AddSingleton<IGetCurrentInstanceUseCase, GetCurrentInstanceService>();
1716
}
1817

1918
public void DefineEndpoints(IEndpointRouteBuilder app)
2019
{
2120
app.MapGet("/api/projects/{projectId}/instances", GetAllInstances);
22-
app.MapGet("/api/projects/{projectId}/instances/current", GetCurrentInstance);
2321
}
2422

2523
private static async Task<Ok<WebGetProjectInstancesListResponse>> GetAllInstances(
@@ -33,27 +31,4 @@ private static async Task<Ok<WebGetProjectInstancesListResponse>> GetAllInstance
3331

3432
return TypedResults.Ok(WebGetProjectInstancesListResponse.Map(calculationInstanceInfos));
3533
}
36-
37-
private static async Task<Ok<WebGetProjectInstanceInfo>> GetCurrentInstance(
38-
[FromRoute] string projectId
39-
)
40-
{
41-
await Task.CompletedTask;
42-
43-
// TODO FIXIT: remove hardcoded value
44-
45-
return TypedResults.Ok(
46-
new WebGetProjectInstanceInfo(
47-
"instance-id",
48-
["honeydew", "InspectorGit"],
49-
["software-analysis"],
50-
new Dictionary<string, IEnumerable<string>>
51-
{
52-
{ "InspectorGit", ["honeydew.iglog"] },
53-
{ "honeydew", ["honeydew-raw.json"] },
54-
},
55-
DateTimeOffset.UtcNow
56-
)
57-
);
58-
}
5934
}

src/Adapters/Driving/Web/EndpointDefinitions/Loaders/UploadLoaderFilesEndpoint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static async Task<UploadResult> UploadLoaderFiles(
5454
TypedResults.Ok(
5555
new WebUploadLoaderFilesResponse(loaderId, fileData.Select(f => f.Name))
5656
),
57-
error =>error.ToProblem(context)
57+
error => error.ToProblem(context)
5858
);
5959
}
6060
}

src/Adapters/Driving/Web/EndpointDefinitions/ProjectStructure/CreateProjectScriptsEndpoint.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace ScriptBee.Web.EndpointDefinitions.ProjectStructure;
1313
using CreateResponse = Results<
1414
Created<WebScriptData>,
1515
NotFound<ProblemDetails>,
16+
BadRequest<ProblemDetails>,
1617
ValidationProblem,
1718
Conflict<ProblemDetails>
1819
>;
@@ -50,6 +51,7 @@ private static async Task<CreateResponse> CreateProjectScript(
5051
WebScriptData.Map(script)
5152
),
5253
error => error.ToProblem(context),
54+
error => error.ToBadRequestProblem(context),
5355
error => error.ToProblem(context),
5456
error => error.ToProblem(context)
5557
);

src/Adapters/Driving/Web/Exceptions/ApiErrorExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.AspNetCore.Http.HttpResults;
22
using Microsoft.AspNetCore.Mvc;
33
using ScriptBee.Common.Web.Extensions;
4+
using ScriptBee.Domain.Model.Analysis;
45
using ScriptBee.Domain.Model.Errors;
56
using ScriptBee.Web.EndpointDefinitions.ProjectStructure.Contracts;
67

@@ -101,4 +102,30 @@ HttpContext context
101102
)
102103
);
103104
}
105+
106+
public static BadRequest<ProblemDetails> ToBadRequestProblem(
107+
this NoInstanceAllocatedForProjectError error,
108+
HttpContext context
109+
)
110+
{
111+
return TypedResults.BadRequest(
112+
context.ToProblemDetails(
113+
"No Instance Allocated For Project",
114+
$"There is no instance allocated for project with the ID '{error.ProjectId}'"
115+
)
116+
);
117+
}
118+
119+
public static NotFound<ProblemDetails> ToNotFoundProblem(
120+
this NoInstanceAllocatedForProjectError error,
121+
HttpContext context
122+
)
123+
{
124+
return TypedResults.NotFound(
125+
context.ToProblemDetails(
126+
"No Instance Allocated For Project",
127+
$"There is no instance allocated for project with the ID '{error.ProjectId}'"
128+
)
129+
);
130+
}
104131
}

0 commit comments

Comments
 (0)