Skip to content

Commit

Permalink
fix(api): return expected model instead of task
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed Nov 11, 2024
1 parent ccb9542 commit 26ede0f
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/GZCTF/Controllers/GameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ public async Task<IActionResult> GetChallengesWithTrafficCapturing([FromRoute] i
{
var challenges = await challengeRepository.GetChallengesWithTrafficCapturing(id, token);

return Ok(challenges.Select(async c => await ChallengeTrafficModel.FromChallenge(c, storage, token)));
var results = await Task.WhenAll(
challenges.Select(async c =>
await ChallengeTrafficModel.FromChallenge(c, storage, token))
);

return Ok(results);
}

/// <summary>
Expand Down Expand Up @@ -425,7 +430,12 @@ public async Task<IActionResult> GetChallengeTraffic([FromRoute] int challengeId

Participation[] participation = await participationRepository.GetParticipationsByIds(participationIds, token);

return Ok(participation.Select(async p => await TeamTrafficModel.FromParticipation(p, challengeId, storage, token)));
var results = await Task.WhenAll(
participation.Select(async p =>
await TeamTrafficModel.FromParticipation(p, challengeId, storage, token))
);

return Ok(results);
}

/// <summary>
Expand Down Expand Up @@ -1003,8 +1013,7 @@ public async Task<IActionResult> CreateContainer([FromRoute] int id, [FromRoute]

if (instance.IsContainerOperationTooFrequent)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Game_OperationTooFrequent)],
StatusCodes.Status429TooManyRequests))
{ StatusCode = StatusCodes.Status429TooManyRequests };
StatusCodes.Status429TooManyRequests)) { StatusCode = StatusCodes.Status429TooManyRequests };

if (instance.Container is not null)
{
Expand All @@ -1017,15 +1026,15 @@ public async Task<IActionResult> CreateContainer([FromRoute] int id, [FromRoute]

return await gameInstanceRepository.CreateContainer(instance, context.Participation!.Team, context.User!,
context.Game!, token) switch
{
null or (TaskStatus.Failed, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerCreationFailed)])),
(TaskStatus.Denied, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerNumberLimitExceeded),
context.Game!.ContainerCountLimit])),
(TaskStatus.Success, var x) => Ok(ContainerInfoModel.FromContainer(x!)),
_ => throw new UnreachableException()
};
{
null or (TaskStatus.Failed, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerCreationFailed)])),
(TaskStatus.Denied, null) => BadRequest(
new RequestResponse(localizer[nameof(Resources.Program.Game_ContainerNumberLimitExceeded),
context.Game!.ContainerCountLimit])),
(TaskStatus.Success, var x) => Ok(ContainerInfoModel.FromContainer(x!)),
_ => throw new UnreachableException()
};
}

/// <summary>
Expand Down Expand Up @@ -1120,8 +1129,7 @@ public async Task<IActionResult> DeleteContainer([FromRoute] int id, [FromRoute]

if (instance.IsContainerOperationTooFrequent)
return new JsonResult(new RequestResponse(localizer[nameof(Resources.Program.Game_OperationTooFrequent)],
StatusCodes.Status429TooManyRequests))
{ StatusCode = StatusCodes.Status429TooManyRequests };
StatusCodes.Status429TooManyRequests)) { StatusCode = StatusCodes.Status429TooManyRequests };

var destroyId = instance.Container.ContainerId;

Expand Down Expand Up @@ -1154,8 +1162,7 @@ async Task<ContextInfo> GetContextInfo(int id, int challengeId = 0, bool withFla
{
ContextInfo res = new()
{
User = await userManager.GetUserAsync(User),
Game = await gameRepository.GetGameById(id, token)
User = await userManager.GetUserAsync(User), Game = await gameRepository.GetGameById(id, token)
};

if (res.Game is null)
Expand Down

0 comments on commit 26ede0f

Please sign in to comment.