Skip to content

Commit

Permalink
fixed general bets
Browse files Browse the repository at this point in the history
  • Loading branch information
ezraroi committed Sep 12, 2024
1 parent 2068a31 commit 22bd247
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
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
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

0 comments on commit 22bd247

Please sign in to comment.