Skip to content

Commit dddd37d

Browse files
committed
Show descriptions and define responses
1 parent c47e3c2 commit dddd37d

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

App/Api/Setup/ApiEndpoints.cs

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Globalization;
2+
using System.Net;
23
using Asp.Versioning;
34
using Microsoft.OpenApi.Any;
45
using Services;
@@ -7,8 +8,8 @@ namespace Api.Setup;
78

89
internal static class ApiEndpoints
910
{
10-
private static readonly OpenApiString ExampleFromDate = new(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
11-
private static readonly OpenApiString ExampleToDate = new(DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
11+
private static readonly OpenApiString s_exampleFromDate = new(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
12+
private static readonly OpenApiString s_exampleToDate = new(DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture));
1213

1314
public static void ConfigureEndpoints(this WebApplication app)
1415
{
@@ -21,8 +22,7 @@ public static void ConfigureEndpoints(this WebApplication app)
2122
.MapGroup("api/v{version:apiVersion}")
2223
.WithApiVersionSet(apiVersionSet);
2324

24-
ConfigureEndpoint(group, "/longestdownwardtrend",
25-
"Get longest downward trend in days between given dates",
25+
group.MapGet("/longestdownwardtrend",
2626
async (IMarketService service, DateOnly fromDate, DateOnly toDate) =>
2727
{
2828
try
@@ -38,10 +38,21 @@ public static void ConfigureEndpoints(this WebApplication app)
3838
{
3939
return Results.Problem(statusCode: (int?)ex.StatusCode);
4040
}
41-
});
41+
})
42+
.WithDescription("Get longest downward trend in days between given dates")
43+
.WithOpenApi(operation =>
44+
{
45+
operation.Parameters[0].Example = s_exampleFromDate;
46+
operation.Parameters[^1].Example = s_exampleToDate;
47+
return operation;
48+
})
49+
.Produces<HighestTradingVolumeResponse>((int)HttpStatusCode.OK)
50+
.Produces((int)HttpStatusCode.NotFound)
51+
.Produces((int)HttpStatusCode.BadRequest)
52+
.ProducesProblem((int)HttpStatusCode.TooManyRequests)
53+
.ProducesProblem((int)HttpStatusCode.InternalServerError);
4254

43-
ConfigureEndpoint(group, "/highestradingvolume",
44-
"Get the date with the highest trading volume between given dates",
55+
group.MapGet("/highestradingvolume",
4556
async (IMarketService service, DateOnly fromDate, DateOnly toDate) =>
4657
{
4758
try
@@ -61,10 +72,21 @@ public static void ConfigureEndpoints(this WebApplication app)
6172
{
6273
return Results.Problem(statusCode: (int?)ex.StatusCode);
6374
}
64-
});
75+
})
76+
.WithDescription("Get the date with the highest trading volume between given dates")
77+
.WithOpenApi(operation =>
78+
{
79+
operation.Parameters[0].Example = s_exampleFromDate;
80+
operation.Parameters[^1].Example = s_exampleToDate;
81+
return operation;
82+
})
83+
.Produces<HighestTradingVolumeResponse>((int)HttpStatusCode.OK)
84+
.Produces((int)HttpStatusCode.NotFound)
85+
.Produces((int)HttpStatusCode.BadRequest)
86+
.ProducesProblem((int)HttpStatusCode.TooManyRequests)
87+
.ProducesProblem((int)HttpStatusCode.InternalServerError);
6588

66-
ConfigureEndpoint(group, "/buyandsell",
67-
"Get pair of dates when it is best to buy and sell between given dates",
89+
group.MapGet("/buyandsell",
6890
async (IMarketService service, DateOnly fromDate, DateOnly toDate) =>
6991
{
7092
try
@@ -84,19 +106,19 @@ public static void ConfigureEndpoints(this WebApplication app)
84106
{
85107
return Results.Problem(statusCode: (int?)ex.StatusCode);
86108
}
87-
});
88-
}
89-
90-
private static void ConfigureEndpoint(RouteGroupBuilder group, string route, string summary, Delegate handler)
91-
{
92-
group.MapGet(route, handler)
109+
})
110+
.WithDescription("Get pair of dates when it is best to buy and sell between given dates")
93111
.WithOpenApi(operation =>
94112
{
95-
operation.Summary = summary;
96-
operation.Parameters[0].Example = ExampleFromDate;
97-
operation.Parameters[^1].Example = ExampleToDate;
113+
operation.Parameters[0].Example = s_exampleFromDate;
114+
operation.Parameters[^1].Example = s_exampleToDate;
98115
return operation;
99-
});
116+
})
117+
.Produces<BuyAndSellResponse>((int)HttpStatusCode.OK)
118+
.Produces((int)HttpStatusCode.NotFound)
119+
.Produces((int)HttpStatusCode.BadRequest)
120+
.ProducesProblem((int)HttpStatusCode.TooManyRequests)
121+
.ProducesProblem((int)HttpStatusCode.InternalServerError);
100122
}
101123
}
102124

0 commit comments

Comments
 (0)