Skip to content

Commit

Permalink
Fix join query
Browse files Browse the repository at this point in the history
  • Loading branch information
Atralupus committed Jan 3, 2025
1 parent bef373d commit 41e6c8d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ await handler.InitializeAsync(
Assert.NotNull(result.Principal);
Assert.Equal(publicKey.ToString(), result.Principal.FindFirst("public_key")?.Value);
Assert.Equal(address, result.Principal.FindFirst("address")?.Value);
Assert.Equal("test", result.Principal.FindFirst("avatar_address")?.Value);
Assert.IsType<string>(result.Principal.FindFirst("avatar_address")?.Value);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion ArenaService.Tests/Auth/JwtCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static string CreateJwt(PrivateKey privateKey, string role = "user")
sub = privateKey.PublicKey.ToHex(true),
role,
iat = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
exp = DateTimeOffset.UtcNow.AddMinutes(60).ToUnixTimeSeconds()
exp = DateTimeOffset.UtcNow.AddYears(1).ToUnixTimeSeconds()
};

string payloadJson = JsonConvert.SerializeObject(payload);
Expand Down
21 changes: 19 additions & 2 deletions ArenaService.Tests/Controllers/LeaderboardControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,35 @@ namespace ArenaService.Tests.Controllers;
public class LeaderboardControllerTests
{
private readonly LeaderboardController _controller;
private Mock<IParticipantRepository> _participantRepoMock;
private Mock<ILeaderboardRepository> _leaderboardRepoMock;

public LeaderboardControllerTests()
{
var participantRepoMock = new Mock<IParticipantRepository>();
_participantRepoMock = participantRepoMock;
var leaderboardRepoMock = new Mock<ILeaderboardRepository>();
_leaderboardRepoMock = leaderboardRepoMock;
_controller = new LeaderboardController(_leaderboardRepoMock.Object);
_controller = new LeaderboardController(
_leaderboardRepoMock.Object,
_participantRepoMock.Object
);
}

[Fact]
public async Task Join_ActivatedSeasonsExist_ReturnsOk()
{
_participantRepoMock
.Setup(repo => repo.GetParticipantByAvatarAddressAsync(1, "test"))
.ReturnsAsync(
new Participant
{
Id = 1,
AvatarAddress = "test",
NameWithHash = "test",
PortraitId = 1
}
);
_leaderboardRepoMock
.Setup(repo => repo.GetMyRankAsync(1, 1))
.ReturnsAsync(
Expand All @@ -41,7 +58,7 @@ public async Task Join_ActivatedSeasonsExist_ReturnsOk()
}
);

var result = await _controller.GetMyRank(1, 1);
var result = await _controller.GetMyRank(1, "test");

var okResult = Assert.IsType<Ok<LeaderboardEntryResponse>>(result.Result);
}
Expand Down
25 changes: 21 additions & 4 deletions ArenaService/Controllers/LeaderboardController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ namespace ArenaService.Controllers;
public class LeaderboardController : ControllerBase
{
private readonly ILeaderboardRepository _leaderboardRepo;
private readonly IParticipantRepository _participantRepo;

public LeaderboardController(ILeaderboardRepository leaderboardRepo)
public LeaderboardController(
ILeaderboardRepository leaderboardRepo,
IParticipantRepository participantRepo
)
{
_leaderboardRepo = leaderboardRepo;
_participantRepo = participantRepo;
}

// [HttpGet]
Expand All @@ -27,22 +32,34 @@ public LeaderboardController(ILeaderboardRepository leaderboardRepo)
// var leaderboard = await _leaderboardRepository.GetLeaderboard(seasonId, offset, limit);
// }

[HttpGet("participants/{participantId}")]
[HttpGet("participants/{avatarAddress}")]
[ProducesResponseType(typeof(LeaderboardEntryResponse), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(UnauthorizedHttpResult), StatusCodes.Status401Unauthorized)]
[ProducesResponseType(typeof(NotFound<string>), StatusCodes.Status404NotFound)]
public async Task<Results<NotFound<string>, Ok<LeaderboardEntryResponse>>> GetMyRank(
int seasonId,
int participantId
string avatarAddress
)
{
var leaderboardEntry = await _leaderboardRepo.GetMyRankAsync(seasonId, participantId);
var participant = await _participantRepo.GetParticipantByAvatarAddressAsync(
seasonId,
avatarAddress
);

if (participant is null)
{
return TypedResults.NotFound("Not participant user.");
}

var leaderboardEntry = await _leaderboardRepo.GetMyRankAsync(seasonId, participant.Id);

if (leaderboardEntry == null)
{
return TypedResults.NotFound("No leaderboardEntry found.");
}

leaderboardEntry.Participant = participant;

return TypedResults.Ok(leaderboardEntry.ToResponse());
}
}
3 changes: 2 additions & 1 deletion ArenaService/Repositories/AvailableOpponentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public AvailableOpponentRepository(ArenaDbContext context)
public async Task<List<AvailableOpponent>> GetAvailableOpponents(int participantId)
{
return await _context
.AvailableOpponents.Where(ao => ao.ParticipantId == participantId)
.AvailableOpponents.Include(lb => lb.Opponent)
.Where(ao => ao.ParticipantId == participantId)
.ToListAsync();
}
}

0 comments on commit 41e6c8d

Please sign in to comment.