Skip to content

Commit

Permalink
Merge pull request #111 from ezraroi/109-posting-general-bet-is-faili…
Browse files Browse the repository at this point in the history
…ng-on-the-server-side-validation

109 posting general bet is failing on the server side validation
  • Loading branch information
ezraroi authored Sep 12, 2024
2 parents d5686d6 + 22bd247 commit 9de009a
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
17 changes: 9 additions & 8 deletions Mundialito/Client/src/Games/Game.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input id="GameHomeScore" name="GameHomeScore" type="number" ng-model="game.HomeScore"
class="form-control" placeholder="Home Team Score" required min="0">
class="form-control" placeholder="{{game.HomeTeam.Name}} Score" required min="0">
</div>
</div>
<div class="form-group" ng-if="!game.IsOpen">
Expand All @@ -74,7 +74,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input id="GameAwayScore" name="GameAwayScore" type="number" ng-model="game.AwayScore"
class="form-control" placeholder="Away Team Score" required min="0">
class="form-control" placeholder="{{game.AwayTeam.Name}} Score" required min="0">
</div>
</div>
<div class="form-group" ng-if="!game.IsOpen">
Expand Down Expand Up @@ -211,11 +211,11 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
class="fa-solid pull-right"> </i>
</div>
<div class="panel-body" cg-busy="updateBetPromise">
<div ng-if="!game.IsOpen && userBet.BetId == -1" class="text-danger">
<div ng-if="!game.IsOpen && userBet.BetId === -1" class="text-danger">
<i class="fa fa-warning"></i> You haven't placed a bet on this game and it is already closed for
betting
</div>
<div ng-show="!game.IsOpen">
<div ng-show="!game.IsOpen && userBet.BetId !== -1">
<div class="table-responsive">
<table class="table table-striped">
<thead>
Expand Down Expand Up @@ -254,7 +254,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input name="{{game.HomeTeam.TeamId}}" id="{{game.HomeTeam.TeamId}}" type="number"
ng-model="userBet.HomeScore" class="form-control" placeholder="Home Team Score" required
ng-model="userBet.HomeScore" class="form-control" placeholder="{{game.HomeTeam.Name}} Score" required
min="0">
</div>
</div>
Expand All @@ -265,7 +265,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input name="{{game.AwayTeam.TeamId}}" id="{{game.AwayTeam.TeamId}}" type="number"
ng-model="userBet.AwayScore" class="form-control" placeholder="Away Team Score" required
ng-model="userBet.AwayScore" class="form-control" placeholder="{{game.AwayTeam.Name}} Score" required
min="0">
</div>
</div>
Expand Down Expand Up @@ -522,17 +522,18 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input id="GameHomeScore" name="GameHomeScore" type="number"
ng-model="simulatedGame.HomeScore" class="form-control" placeholder="Home Team Score"
ng-model="simulatedGame.HomeScore" class="form-control" placeholder="{{game.HomeTeam.Name}} Score"
required min="0">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="GameAwayScore">
<team-flag team="game.AwayTeam"></team-flag>
{{game.AwayTeam.Name}} Score
</label>
<div class="col-md-2">
<input id="GameAwayScore" name="GameAwayScore" type="number"
ng-model="simulatedGame.AwayScore" class="form-control" placeholder="Away Team Score"
ng-model="simulatedGame.AwayScore" class="form-control" placeholder="{{game.AwayTeam.Name}} Score"
required min="0">
</div>
</div>
Expand Down
35 changes: 25 additions & 10 deletions Mundialito/Controllers/GeneralBetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Mundialito.DAL.Accounts;
using Mundialito.DAL.ActionLogs;
using Mundialito.DAL.GeneralBets;
using Mundialito.DAL.Players;
using Mundialito.DAL.Teams;
using Mundialito.Logic;
using Mundialito.Models;

Expand All @@ -21,16 +23,20 @@ public class GeneralBetsController : ControllerBase
private readonly IHttpContextAccessor httpContextAccessor;
private readonly TournamentTimesUtils tournamentTimesUtils;
private readonly UserManager<MundialitoUser> userManager;
private readonly ITeamsRepository teamsRepository;
private readonly IPlayersRepository playersRepository;
private readonly ILogger logger;

public GeneralBetsController(ILogger<GeneralBetsController> logger, IGeneralBetsRepository generalBetsRepository, IDateTimeProvider dateTimeProvider, IActionLogsRepository actionLogsRepository, IHttpContextAccessor httpContextAccessor, TournamentTimesUtils tournamentTimesUtils, UserManager<MundialitoUser> userManager)
public GeneralBetsController(ILogger<GeneralBetsController> logger, IGeneralBetsRepository generalBetsRepository, IDateTimeProvider dateTimeProvider, IActionLogsRepository actionLogsRepository, IHttpContextAccessor httpContextAccessor, TournamentTimesUtils tournamentTimesUtils, UserManager<MundialitoUser> userManager, ITeamsRepository teamsRepository, IPlayersRepository playersRepository)
{
this.httpContextAccessor = httpContextAccessor;
this.dateTimeProvider = dateTimeProvider;
this.generalBetsRepository = generalBetsRepository;
this.dateTimeProvider = dateTimeProvider;
this.actionLogsRepository = actionLogsRepository;
this.httpContextAccessor = httpContextAccessor;
this.tournamentTimesUtils = tournamentTimesUtils;
this.userManager = userManager;
this.teamsRepository = teamsRepository;
this.playersRepository = playersRepository;
this.logger = logger;
}

Expand Down Expand Up @@ -81,6 +87,8 @@ public ActionResult<GeneralBetViewModel> GetGeneralBetById(int id)
[Authorize(Roles = "Active,Admin")]
public async Task<ActionResult<NewGeneralBetModel>> PostBet(NewGeneralBetModel newBet)
{
if (generalBetsRepository.IsGeneralBetExists(httpContextAccessor.HttpContext?.User.Identity.Name))
return BadRequest(new ErrorMessage { Message = "You have already submitted your general bet, only update is permitted"});
var validate = Validate();
if (!string.IsNullOrEmpty(validate))
{
Expand All @@ -90,11 +98,23 @@ public async Task<ActionResult<NewGeneralBetModel>> PostBet(NewGeneralBetModel n
var user = await userManager.FindByNameAsync(httpContextAccessor.HttpContext?.User.Identity.Name);
if (user == null)
return Unauthorized();
var winningTeam = teamsRepository.GetTeam(newBet.WinningTeam.TeamId);
if (winningTeam == null)
{
AddLog(ActionType.ERROR, string.Format("Team with id '{0}' dosen't exits", newBet.WinningTeam.TeamId));
return NotFound(new ErrorMessage { Message = string.Format("Team with id '{0}' dosen't exits", newBet.WinningTeam.TeamId)});
}
var goldenBootPlayer = playersRepository.GetPlayer(newBet.GoldenBootPlayer.PlayerId);
if (goldenBootPlayer == null)
{
AddLog(ActionType.ERROR, string.Format("Player with id '{0}' dosen't exits", newBet.GoldenBootPlayer.PlayerId));
return NotFound(new ErrorMessage { Message = string.Format("Player with id '{0}' dosen't exits", newBet.GoldenBootPlayer.PlayerId)});
}
var generalBet = new GeneralBet
{
User = user,
WinningTeam = newBet.WinningTeam,
GoldBootPlayer = newBet.GoldenBootPlayer
WinningTeam = winningTeam,
GoldBootPlayer = goldenBootPlayer
};
var res = generalBetsRepository.InsertGeneralBet(generalBet);
logger.LogInformation("Posting new general bet {} from {}", generalBet, user.UserName);
Expand Down Expand Up @@ -156,11 +176,6 @@ public IActionResult ResolveGeneralBet(int id, ResolveGeneralBetModel resolvedBe

private string Validate()
{
if (generalBetsRepository.IsGeneralBetExists(httpContextAccessor.HttpContext?.User.Identity.Name))
{
AddLog(ActionType.ERROR, "You have already submitted your general bet, only update is permitted");
return "You have already submitted your general bet, only update is permitted";
}
if (dateTimeProvider.UTCNow > tournamentTimesUtils.GetGeneralBetsCloseTime())
{
AddLog(ActionType.ERROR, "General bets are already closed for betting");
Expand Down
9 changes: 9 additions & 0 deletions Mundialito/Models/BetsModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ public BetGameTeam(Team team)
TeamId = team.TeamId;
Name = team.Name;
ShortName = team.ShortName;
Logo = team.Logo;
Flag = team.Flag;
}

[JsonPropertyName("TeamId")]
public int TeamId { get; set; }

Expand All @@ -245,4 +248,10 @@ public BetGameTeam(Team team)
[JsonPropertyName("ShortName")]
public string ShortName { get; set; }

[JsonPropertyName("Flag")]
public string Flag { get; set; }

[JsonPropertyName("Logo")]
public string Logo { get; set; }

}
16 changes: 14 additions & 2 deletions Mundialito/Models/GeneralBetsModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,27 @@ public GeneralBetViewModel(GeneralBet bet, DateTime closeTime)
public class NewGeneralBetModel
{
[JsonPropertyName("WinningTeam")]
public Team WinningTeam { get; set; }
public NewGeneralBetModelWinningTeam WinningTeam { get; set; }

[JsonPropertyName("GoldenBootPlayer")]
public Player GoldenBootPlayer { get; set; }
public NewGeneralBetModelGoldenBootPlayer GoldenBootPlayer { get; set; }

[JsonPropertyName("GeneralBetId")]
public int GeneralBetId { get; set; }
}

public class NewGeneralBetModelWinningTeam
{
[JsonPropertyName("TeamId")]
public int TeamId { get; set; }
}

public class NewGeneralBetModelGoldenBootPlayer
{
[JsonPropertyName("PlayerId")]
public int PlayerId { get; set; }
}

public class UpdateGenralBetModel
{
[JsonPropertyName("WinningTeam")]
Expand Down
17 changes: 9 additions & 8 deletions Mundialito/wwwroot/App/Games/Game.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input id="GameHomeScore" name="GameHomeScore" type="number" ng-model="game.HomeScore"
class="form-control" placeholder="Home Team Score" required min="0">
class="form-control" placeholder="{{game.HomeTeam.Name}} Score" required min="0">
</div>
</div>
<div class="form-group" ng-if="!game.IsOpen">
Expand All @@ -74,7 +74,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input id="GameAwayScore" name="GameAwayScore" type="number" ng-model="game.AwayScore"
class="form-control" placeholder="Away Team Score" required min="0">
class="form-control" placeholder="{{game.AwayTeam.Name}} Score" required min="0">
</div>
</div>
<div class="form-group" ng-if="!game.IsOpen">
Expand Down Expand Up @@ -211,11 +211,11 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
class="fa-solid pull-right"> </i>
</div>
<div class="panel-body" cg-busy="updateBetPromise">
<div ng-if="!game.IsOpen && userBet.BetId == -1" class="text-danger">
<div ng-if="!game.IsOpen && userBet.BetId === -1" class="text-danger">
<i class="fa fa-warning"></i> You haven't placed a bet on this game and it is already closed for
betting
</div>
<div ng-show="!game.IsOpen">
<div ng-show="!game.IsOpen && userBet.BetId !== -1">
<div class="table-responsive">
<table class="table table-striped">
<thead>
Expand Down Expand Up @@ -254,7 +254,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input name="{{game.HomeTeam.TeamId}}" id="{{game.HomeTeam.TeamId}}" type="number"
ng-model="userBet.HomeScore" class="form-control" placeholder="Home Team Score" required
ng-model="userBet.HomeScore" class="form-control" placeholder="{{game.HomeTeam.Name}} Score" required
min="0">
</div>
</div>
Expand All @@ -265,7 +265,7 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input name="{{game.AwayTeam.TeamId}}" id="{{game.AwayTeam.TeamId}}" type="number"
ng-model="userBet.AwayScore" class="form-control" placeholder="Away Team Score" required
ng-model="userBet.AwayScore" class="form-control" placeholder="{{game.AwayTeam.Name}} Score" required
min="0">
</div>
</div>
Expand Down Expand Up @@ -522,17 +522,18 @@ <h5 ng-show="game.GameStatsPage !== null"><a target="_blank" ng-href="{{game.Gam
</label>
<div class="col-md-2">
<input id="GameHomeScore" name="GameHomeScore" type="number"
ng-model="simulatedGame.HomeScore" class="form-control" placeholder="Home Team Score"
ng-model="simulatedGame.HomeScore" class="form-control" placeholder="{{game.HomeTeam.Name}} Score"
required min="0">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="GameAwayScore">
<team-flag team="game.AwayTeam"></team-flag>
{{game.AwayTeam.Name}} Score
</label>
<div class="col-md-2">
<input id="GameAwayScore" name="GameAwayScore" type="number"
ng-model="simulatedGame.AwayScore" class="form-control" placeholder="Away Team Score"
ng-model="simulatedGame.AwayScore" class="form-control" placeholder="{{game.AwayTeam.Name}} Score"
required min="0">
</div>
</div>
Expand Down

0 comments on commit 9de009a

Please sign in to comment.