Skip to content

Commit eec7ce0

Browse files
author
Andrii Bondarchuk
committed
Make subcategory parameter optional (will work if only one category exists)
1 parent e756ebc commit eec7ce0

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

src/FitSyncHub.Functions/Functions/ZwiftEventRidersCompetitionMetricsHttpTriggerFunction.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,21 @@ public async Task<IActionResult> Run(
2020
[HttpTrigger(AuthorizationLevel.Function, "get", Route = "zwift-event-competition-metrics")] HttpRequest req,
2121
CancellationToken cancellationToken)
2222
{
23-
string? zwiftEventUrl = req.Query["eventUrl"];
23+
string? eventUrl = req.Query["eventUrl"];
2424
string? subcategory = req.Query["subcategory"];
2525

26-
if (string.IsNullOrWhiteSpace(zwiftEventUrl)
27-
|| string.IsNullOrWhiteSpace(subcategory))
26+
if (string.IsNullOrWhiteSpace(eventUrl))
2827
{
29-
return new BadRequestObjectResult("wrong request");
28+
return new BadRequestObjectResult($"Specify params: {nameof(eventUrl)}");
3029
}
3130

32-
if (!Uri.TryCreate(zwiftEventUrl, UriKind.Absolute, out _))
31+
if (!Uri.TryCreate(eventUrl, UriKind.Absolute, out _))
3332
{
34-
return new BadRequestObjectResult("wrong url");
33+
return new BadRequestObjectResult($"Wrong '{nameof(eventUrl)}' url");
3534
}
3635

3736
var entrants = await _zwiftEventsService
38-
.GetEntrants(zwiftEventUrl, subcategory, includeMyself: true, cancellationToken);
37+
.GetEntrants(eventUrl, subcategory, includeMyself: true, cancellationToken);
3938

4039
var result = await GetRidersCompetitionMetrics(entrants, cancellationToken);
4140
result = [.. result.OrderByDescending(x => x.RacingScore)];

src/FitSyncHub.Functions/Functions/ZwiftEventVELORatingHttpTriggerFunction.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,22 @@ public async Task<IActionResult> Run(
2929
CancellationToken cancellationToken)
3030
{
3131
string? cookie = req.Query["cookie"];
32-
string? zwiftEventUrl = req.Query["eventUrl"];
32+
string? eventUrl = req.Query["eventUrl"];
3333
string? subcategory = req.Query["subcategory"];
3434

3535
if (string.IsNullOrWhiteSpace(cookie)
36-
|| string.IsNullOrWhiteSpace(zwiftEventUrl)
37-
|| string.IsNullOrWhiteSpace(subcategory))
36+
|| string.IsNullOrWhiteSpace(eventUrl))
3837
{
39-
return new BadRequestObjectResult("wrong request");
38+
return new BadRequestObjectResult($"Specify params: {nameof(cookie)}, {nameof(eventUrl)}");
4039
}
4140

42-
if (!Uri.TryCreate(zwiftEventUrl, UriKind.Absolute, out _))
41+
if (!Uri.TryCreate(eventUrl, UriKind.Absolute, out _))
4342
{
44-
return new BadRequestObjectResult("wrong url");
43+
return new BadRequestObjectResult($"Wrong '{nameof(eventUrl)}' url");
4544
}
4645

4746
var entrants = await _zwiftEventsService
48-
.GetEntrants(zwiftEventUrl, subcategory, includeMyself: true, cancellationToken);
47+
.GetEntrants(eventUrl, subcategory, includeMyself: true, cancellationToken);
4948

5049
var result = await GetEntrantsVELO(entrants, cancellationToken);
5150
result = [.. result.OrderByDescending(x => x.MaxVELO)];

src/FitSyncHub.Functions/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
nameof(MacroNutrientsCalculatorHttpTriggerFunction),
3030
nameof(LactateSyncHttpTriggerFunction),
3131
nameof(XertWorkoutToIntervalsICUExporterHttpTriggerFunction),
32+
nameof(ZwiftEventRidersCompetitionMetricsHttpTriggerFunction),
33+
nameof(ZwiftEventVELORatingHttpTriggerFunction),
3234
];
3335

3436
var builder = FunctionsApplication.CreateBuilder(args);

src/FitSyncHub.Zwift/Services/ZwiftEventsService.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,26 @@ public async Task<ZwiftPlayerCompetitionMetrics> GetCompetitionMetrics(
4343

4444
public Task<IReadOnlyCollection<ZwiftEntrantResponseModel>> GetEntrants(
4545
string zwiftEventUrl,
46-
string subgroupLabel,
46+
string? subgroupLabel,
4747
CancellationToken cancellationToken)
4848
{
4949
return GetEntrants(zwiftEventUrl, subgroupLabel, includeMyself: false, cancellationToken);
5050
}
5151

52-
5352
public async Task<IReadOnlyCollection<ZwiftEntrantResponseModel>> GetEntrants(
5453
string zwiftEventUrl,
55-
string subgroupLabel,
54+
string? subgroupLabel,
5655
bool includeMyself,
5756
CancellationToken cancellationToken)
5857
{
5958
var zwiftEvent = await _zwiftHttpClient.GetEventFromZwfitEventViewUrl(zwiftEventUrl, cancellationToken);
60-
var eventSubgroupId = zwiftEvent.EventSubgroups
61-
.Single(x => x.SubgroupLabel == subgroupLabel).Id;
59+
60+
var eventSubgroupId = (string.IsNullOrWhiteSpace(subgroupLabel), zwiftEvent.EventSubgroups) switch
61+
{
62+
(true, { Length: 1 }) => zwiftEvent.EventSubgroups.Single().Id,
63+
(false, { Length: >= 1 }) => zwiftEvent.EventSubgroups.Single(x => x.SubgroupLabel == subgroupLabel).Id,
64+
_ => throw new InvalidDataException("Can not map subgroupLabel to eventSubgroup")
65+
};
6266

6367
var entrants = await _zwiftHttpClient
6468
.GetEventSubgroupEntrants(eventSubgroupId, cancellationToken: cancellationToken);

0 commit comments

Comments
 (0)