-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement available opponent controller
- Loading branch information
Showing
11 changed files
with
288 additions
and
10 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
ArenaService.Tests/Controllers/AvailableOpponentControllerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using System.Security.Claims; | ||
using ArenaService.Controllers; | ||
using ArenaService.Dtos; | ||
using ArenaService.Models; | ||
using ArenaService.Repositories; | ||
using ArenaService.Services; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Moq; | ||
|
||
namespace ArenaService.Tests.Controllers; | ||
|
||
public class AvailableOpponentControllerTests | ||
{ | ||
private readonly AvailableOpponentController _controller; | ||
private Mock<IAvailableOpponentRepository> _availableOpponentRepositoryMock; | ||
private AvailableOpponentService _availableOpponentService; | ||
private Mock<IParticipantRepository> _participantRepositoryMock; | ||
private ParticipantService _participantService; | ||
|
||
public AvailableOpponentControllerTests() | ||
{ | ||
var availableOpponentRepositoryMock = new Mock<IAvailableOpponentRepository>(); | ||
_availableOpponentRepositoryMock = availableOpponentRepositoryMock; | ||
_availableOpponentService = new AvailableOpponentService( | ||
_availableOpponentRepositoryMock.Object | ||
); | ||
var participantRepositoryMock = new Mock<IParticipantRepository>(); | ||
_participantRepositoryMock = participantRepositoryMock; | ||
_participantService = new ParticipantService(_participantRepositoryMock.Object); | ||
_controller = new AvailableOpponentController( | ||
_availableOpponentService, | ||
_participantService | ||
); | ||
} | ||
|
||
[Fact] | ||
public async Task GetAvailableOpponents_WithValidHeader_ReturnsOk() | ||
{ | ||
var avatarAddress = "DDF1472fD5a79B8F46C28e7643eDEF045e36BD3d"; | ||
|
||
_participantRepositoryMock | ||
.Setup(repo => repo.GetParticipantByAvatarAddressAsync(1, avatarAddress)) | ||
.ReturnsAsync( | ||
new Participant | ||
{ | ||
Id = 1, | ||
AvatarAddress = avatarAddress, | ||
NameWithHash = "test", | ||
PortraitId = 1 | ||
} | ||
); | ||
|
||
_availableOpponentRepositoryMock | ||
.Setup(repo => repo.GetAvailableOpponents(1)) | ||
.ReturnsAsync( | ||
[ | ||
new AvailableOpponent | ||
{ | ||
Id = 1, | ||
ParticipantId = 1, | ||
OpponentId = 2, | ||
Opponent = new Participant | ||
{ | ||
Id = 2, | ||
AvatarAddress = "test", | ||
NameWithHash = "opponent1", | ||
PortraitId = 1 | ||
}, | ||
RefillBlockIndex = 1 | ||
}, | ||
new AvailableOpponent | ||
{ | ||
Id = 2, | ||
ParticipantId = 1, | ||
OpponentId = 3, | ||
Opponent = new Participant | ||
{ | ||
Id = 3, | ||
AvatarAddress = "test", | ||
NameWithHash = "opponent2", | ||
PortraitId = 1 | ||
}, | ||
RefillBlockIndex = 1 | ||
} | ||
] | ||
); | ||
|
||
_controller.ControllerContext = new ControllerContext | ||
{ | ||
HttpContext = new DefaultHttpContext() | ||
}; | ||
_controller.ControllerContext.HttpContext.User = new ClaimsPrincipal( | ||
new ClaimsIdentity([new Claim("avatar", avatarAddress)]) | ||
); | ||
|
||
var result = await _controller.GetAvailableOpponents(1); | ||
|
||
var okResult = Assert.IsType<Ok<AvailableOpponentsResponse>>(result.Result); | ||
Assert.Equal(2, okResult.Value?.AvailableOpponents.Count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
namespace ArenaService.Controllers; | ||
|
||
using System.Security.Claims; | ||
using ArenaService.Dtos; | ||
using ArenaService.Extensions; | ||
using ArenaService.Services; | ||
using Microsoft.AspNetCore.Http.HttpResults; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
[Route("seasons/{seasonId}/available-opponents")] | ||
[ApiController] | ||
public class AvailableOpponentController : ControllerBase | ||
{ | ||
private readonly AvailableOpponentService _availableOpponentService; | ||
private readonly ParticipantService _participantService; | ||
|
||
public AvailableOpponentController( | ||
AvailableOpponentService availableOpponentService, | ||
ParticipantService participantService | ||
) | ||
{ | ||
_availableOpponentService = availableOpponentService; | ||
_participantService = participantService; | ||
} | ||
|
||
private string? ExtractAvatarAddress() | ||
{ | ||
if (HttpContext.User.Identity is ClaimsIdentity identity) | ||
{ | ||
var claim = identity.FindFirst("avatar"); | ||
return claim?.Value; | ||
} | ||
return null; | ||
} | ||
|
||
[HttpGet] | ||
public async Task< | ||
Results<UnauthorizedHttpResult, NotFound<string>, Ok<AvailableOpponentsResponse>> | ||
> GetAvailableOpponents(int seasonId) | ||
{ | ||
var avatarAddress = ExtractAvatarAddress(); | ||
|
||
if (avatarAddress is null) | ||
{ | ||
return TypedResults.Unauthorized(); | ||
} | ||
|
||
var participant = await _participantService.GetParticipantByAvatarAddressAsync( | ||
seasonId, | ||
avatarAddress | ||
); | ||
|
||
if (participant is null) | ||
{ | ||
return TypedResults.NotFound("Not participant user."); | ||
} | ||
|
||
var opponents = await _availableOpponentService.GetAvailableOpponents(participant.Id); | ||
|
||
return TypedResults.Ok( | ||
new AvailableOpponentsResponse { AvailableOpponents = opponents.ToResponse() } | ||
); | ||
} | ||
|
||
[HttpGet] | ||
public async Task<Results<UnauthorizedHttpResult, NotFound<string>, Created>> ResetOpponents( | ||
Check warning on line 66 in ArenaService/Controllers/AvailableOpponentController.cs GitHub Actions / Test (ArenaService.Tests)
|
||
int seasonId | ||
) | ||
{ | ||
var avatarAddress = ExtractAvatarAddress(); | ||
|
||
if (avatarAddress is null) | ||
{ | ||
return TypedResults.Unauthorized(); | ||
} | ||
|
||
// Dummy implementation | ||
return TypedResults.Created(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ArenaService.Dtos; | ||
|
||
public class AvailableOpponentsResponse | ||
{ | ||
public required List<ParticipantResponse> AvailableOpponents { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
ArenaService/SeasonRepositories/AvailableOpponentRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace ArenaService.Repositories; | ||
|
||
using ArenaService.Data; | ||
using ArenaService.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
public interface IAvailableOpponentRepository | ||
{ | ||
Task<List<AvailableOpponent>> GetAvailableOpponents(int participantId); | ||
} | ||
|
||
public class AvailableOpponentRepository : IAvailableOpponentRepository | ||
{ | ||
private readonly ArenaDbContext _context; | ||
|
||
public AvailableOpponentRepository(ArenaDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task<List<AvailableOpponent>> GetAvailableOpponents(int participantId) | ||
{ | ||
return await _context | ||
.AvailableOpponents.Where(ao => ao.ParticipantId == participantId) | ||
.ToListAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace ArenaService.Services; | ||
|
||
using ArenaService.Dtos; | ||
using ArenaService.Extensions; | ||
using ArenaService.Models; | ||
using ArenaService.Repositories; | ||
|
||
public class AvailableOpponentService | ||
{ | ||
private readonly IAvailableOpponentRepository _availableOpponentRepository; | ||
|
||
public AvailableOpponentService(IAvailableOpponentRepository availableOpponentRepository) | ||
{ | ||
_availableOpponentRepository = availableOpponentRepository; | ||
} | ||
|
||
public async Task<List<Participant>> GetAvailableOpponents(int participantId) | ||
{ | ||
var availableOpponents = await _availableOpponentRepository.GetAvailableOpponents( | ||
participantId | ||
); | ||
var opponents = availableOpponents.Select(ao => ao.Opponent).ToList(); | ||
return opponents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters