Skip to content

Commit 13eafeb

Browse files
committed
Type endpoints responses
1 parent b81bbfa commit 13eafeb

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

App/Api/Setup/ApiEndpoints.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Asp.Versioning;
33
using Microsoft.OpenApi.Any;
44
using Services;
5-
using System;
65

76
namespace Api.Setup;
87

@@ -33,10 +32,7 @@ public static void ConfigureEndpoints(this WebApplication app)
3332
{
3433
return Results.NotFound();
3534
}
36-
return Results.Ok(new
37-
{
38-
Days = result
39-
});
35+
return Results.Ok(new LongestDownwardTrendResponse(result.Value));
4036
}
4137
catch (HttpRequestException ex)
4238
{
@@ -55,11 +51,11 @@ public static void ConfigureEndpoints(this WebApplication app)
5551
{
5652
return Results.NotFound();
5753
}
58-
return Results.Ok(new
59-
{
54+
return Results.Ok(new HighestTradingVolumeResponse
55+
(
6056
result.Value.Date,
61-
result.Value.Volume,
62-
});
57+
result.Value.Volume
58+
));
6359
}
6460
catch (HttpRequestException ex)
6561
{
@@ -78,11 +74,11 @@ public static void ConfigureEndpoints(this WebApplication app)
7874
{
7975
return Results.NotFound();
8076
}
81-
return Results.Ok(new
82-
{
83-
result.Value.SellDate,
77+
return Results.Ok(new BuyAndSellResponse
78+
(
8479
result.Value.BuyDate,
85-
});
80+
result.Value.SellDate
81+
));
8682
}
8783
catch (HttpRequestException ex)
8884
{
@@ -103,3 +99,7 @@ private static void ConfigureEndpoint(RouteGroupBuilder group, string route, str
10399
});
104100
}
105101
}
102+
103+
public record LongestDownwardTrendResponse(int Days);
104+
public record HighestTradingVolumeResponse(DateOnly Date, decimal Volume);
105+
public record BuyAndSellResponse(DateOnly BuyDate, DateOnly SellDate);

Tests/IntegrationTests/ApiEndpointsTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Globalization;
22
using System.Net;
3+
using System.Net.Http.Json;
4+
using Api.Setup;
35
using Shouldly;
46
using Xunit;
57

@@ -24,6 +26,12 @@ public async Task LongestDownwardTrend(string? fromDate, string? toDate, HttpSta
2426
{
2527
var result = await _fixture.Client.GetAsync($"{BaseUrl}/longestdownwardtrend?fromDate={fromDate}&toDate={toDate}");
2628
result.StatusCode.ShouldBeOneOf(status, HttpStatusCode.TooManyRequests);
29+
30+
if (result.StatusCode == HttpStatusCode.OK)
31+
{
32+
var data = await result.Content.ReadFromJsonAsync<LongestDownwardTrendResponse>();
33+
data.ShouldNotBeNull();
34+
}
2735
}
2836

2937
[Theory]
@@ -32,6 +40,12 @@ public async Task HighestTradingVolume(string? fromDate, string? toDate, HttpSta
3240
{
3341
var result = await _fixture.Client.GetAsync($"{BaseUrl}/highestradingvolume?fromDate={fromDate}&toDate={toDate}");
3442
result.StatusCode.ShouldBeOneOf(status, HttpStatusCode.TooManyRequests);
43+
44+
if (result.StatusCode == HttpStatusCode.OK)
45+
{
46+
var data = await result.Content.ReadFromJsonAsync<HighestTradingVolumeResponse>();
47+
data.ShouldNotBeNull();
48+
}
3549
}
3650

3751
[Theory]
@@ -40,6 +54,12 @@ public async Task BuyAndSell(string? fromDate, string? toDate, HttpStatusCode st
4054
{
4155
var result = await _fixture.Client.GetAsync($"{BaseUrl}/buyandsell?fromDate={fromDate}&toDate={toDate}");
4256
result.StatusCode.ShouldBeOneOf(status, HttpStatusCode.TooManyRequests);
57+
58+
if (result.StatusCode == HttpStatusCode.OK)
59+
{
60+
var data = await result.Content.ReadFromJsonAsync<BuyAndSellResponse>();
61+
data.ShouldNotBeNull();
62+
}
4363
}
4464

4565
[Fact]

0 commit comments

Comments
 (0)