-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7da211
commit b3077f6
Showing
6 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
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 TNRD.Zeepkist.GTR.Backend.Voting.Resources; | ||
|
||
public class VoteResource | ||
{ | ||
public string Level { get; set; } = null!; | ||
} |
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,106 @@ | ||
using System.Security.Claims; | ||
using Microsoft.AspNetCore.Mvc; | ||
using TNRD.Zeepkist.GTR.Backend.Jwt; | ||
using TNRD.Zeepkist.GTR.Backend.Voting.Resources; | ||
|
||
namespace TNRD.Zeepkist.GTR.Backend.Voting; | ||
|
||
[ApiController] | ||
[Route("votes")] | ||
public class VotingController : ControllerBase | ||
{ | ||
private readonly IVotingService _service; | ||
|
||
public VotingController(IVotingService service) | ||
{ | ||
_service = service; | ||
} | ||
|
||
[HttpPost("upvote")] | ||
public IActionResult Upvote([FromBody] VoteResource resource) | ||
{ | ||
string? value = User.FindFirstValue(IJwtService.SteamIdClaimName); | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (!ulong.TryParse(value, out ulong steamId)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (_service.Upvote(steamId, resource.Level).IsSuccess) | ||
{ | ||
return Ok(); | ||
} | ||
|
||
return Problem(); | ||
} | ||
|
||
[HttpPost("downvote")] | ||
public IActionResult Downvote([FromBody] VoteResource resource) | ||
{ | ||
string? value = User.FindFirstValue(IJwtService.SteamIdClaimName); | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (!ulong.TryParse(value, out ulong steamId)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (_service.Downvote(steamId, resource.Level).IsSuccess) | ||
{ | ||
return Ok(); | ||
} | ||
|
||
return Problem(); | ||
} | ||
|
||
[HttpPost("dupvote")] | ||
public IActionResult DoubleUpvote([FromBody] VoteResource resource) | ||
{ | ||
string? value = User.FindFirstValue(IJwtService.SteamIdClaimName); | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (!ulong.TryParse(value, out ulong steamId)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (_service.DoubleUpvote(steamId, resource.Level).IsSuccess) | ||
{ | ||
return Ok(); | ||
} | ||
|
||
return Problem(); | ||
} | ||
|
||
[HttpPost("ddownvote")] | ||
public IActionResult DoubleDownvote([FromBody] VoteResource resource) | ||
{ | ||
string? value = User.FindFirstValue(IJwtService.SteamIdClaimName); | ||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (!ulong.TryParse(value, out ulong steamId)) | ||
{ | ||
return Unauthorized(); | ||
} | ||
|
||
if (_service.DoubleDownvote(steamId, resource.Level).IsSuccess) | ||
{ | ||
return Ok(); | ||
} | ||
|
||
return Problem(); | ||
} | ||
} |
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,16 @@ | ||
using TNRD.Zeepkist.GTR.Backend.DataStore; | ||
using TNRD.Zeepkist.GTR.Database.Data.Entities; | ||
|
||
namespace TNRD.Zeepkist.GTR.Backend.Voting; | ||
|
||
public interface IVotingRepository : IBasicRepository<Vote> | ||
{ | ||
} | ||
|
||
public class VotingRepository : BasicRepository<Vote>, IVotingRepository | ||
{ | ||
public VotingRepository(IDatabase database, ILogger logger) | ||
: base(database, logger) | ||
{ | ||
} | ||
} |
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,84 @@ | ||
using FluentResults; | ||
using TNRD.Zeepkist.GTR.Backend.Levels; | ||
using TNRD.Zeepkist.GTR.Backend.Users; | ||
using TNRD.Zeepkist.GTR.Database.Data.Entities; | ||
|
||
namespace TNRD.Zeepkist.GTR.Backend.Voting; | ||
|
||
public interface IVotingService | ||
{ | ||
Result Upvote(ulong steamId, string levelHash); | ||
Result Downvote(ulong steamId, string levelHash); | ||
Result DoubleUpvote(ulong steamId, string levelHash); | ||
Result DoubleDownvote(ulong steamId, string levelHash); | ||
} | ||
|
||
public class VotingService : IVotingService | ||
{ | ||
private readonly ILogger<VotingService> _logger; | ||
private readonly IVotingRepository _repository; | ||
private readonly IUserService _userService; | ||
private readonly ILevelService _levelService; | ||
|
||
public VotingService(ILogger<VotingService> logger, IVotingRepository repository, IUserService userService, | ||
ILevelService levelService) | ||
{ | ||
_logger = logger; | ||
_repository = repository; | ||
_userService = userService; | ||
_levelService = levelService; | ||
} | ||
|
||
public Result Upvote(ulong steamId, string levelHash) | ||
{ | ||
return Upsert(steamId, levelHash, 1); | ||
} | ||
|
||
public Result Downvote(ulong steamId, string levelHash) | ||
{ | ||
return Upsert(steamId, levelHash, -1); | ||
} | ||
|
||
public Result DoubleUpvote(ulong steamId, string levelHash) | ||
{ | ||
return Upsert(steamId, levelHash, 2); | ||
} | ||
|
||
public Result DoubleDownvote(ulong steamId, string levelHash) | ||
{ | ||
return Upsert(steamId, levelHash, -2); | ||
} | ||
|
||
private Result Upsert(ulong steamId, string levelHash, int value) | ||
{ | ||
if (!_userService.TryGet(steamId, out User? user)) | ||
{ | ||
_logger.LogWarning("Unable to get user with steam id {SteamId}", steamId); | ||
return Result.Fail($"Unable to get user with steam id '{steamId}'"); | ||
} | ||
|
||
if (!_levelService.TryGetByHash(levelHash, out Level? level)) | ||
{ | ||
_logger.LogWarning("Unable to get level with hash '{Hash}'", levelHash); | ||
return Result.Fail($"Unable to get level with hash '{levelHash}'"); | ||
} | ||
|
||
Vote vote = _repository.Upsert(vote => vote.IdUser == user.Id && vote.IdLevel == level.Id, | ||
() => new Vote | ||
{ | ||
IdUser = user.Id, | ||
IdLevel = level.Id, | ||
Value = value | ||
}, | ||
vote => | ||
{ | ||
vote.Value = value; | ||
return vote; | ||
}); | ||
|
||
return Result.OkIf(vote.IdUser == user.Id && | ||
vote.IdLevel == level.Id && | ||
vote.Value == value, | ||
"Failed to upsert vote"); | ||
} | ||
} |
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