Skip to content

Commit

Permalink
fixed the bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ezraroi committed Jul 11, 2024
1 parent c9acb9c commit 9c81205
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Mundialito/Controllers/TeamsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Mundialito.Models;
using Mundialito.DAL.Bets;

namespace Mundialito.Controllers;

Expand All @@ -11,12 +12,16 @@ namespace Mundialito.Controllers;
public class TeamsController : ControllerBase
{
private readonly ITeamsRepository teamsRepository;
private readonly IBetsRepository betsRepository;
private readonly IHttpContextAccessor httpContextAccessor;
private readonly ILogger logger;


public TeamsController(ILogger<TeamsController> logger, ITeamsRepository teamsRepository)
public TeamsController(ILogger<TeamsController> logger, ITeamsRepository teamsRepository, IBetsRepository betsRepository, IHttpContextAccessor httpContextAccessor)
{
this.teamsRepository = teamsRepository;
this.betsRepository = betsRepository;
this.httpContextAccessor = httpContextAccessor;
this.logger = logger;
}

Expand All @@ -38,8 +43,10 @@ public ActionResult<Team> GetTeamById(int id)
[HttpGet("{id}/Games")]
public IEnumerable<GameViewModel> GetTeamGames(int id)
{
var res = teamsRepository.GetTeamGames(id);
return res.Select((game) => new GameViewModel(game));
var games = teamsRepository.GetTeamGames(id);
var res = games.Select((game) => new GameViewModel(game)).ToList();
AddUserBetsData(res);
return res;
}

[HttpPost]
Expand Down Expand Up @@ -77,5 +84,14 @@ public void DeleteTeam(int id)
logger.LogInformation("Deleted Team {0}", id);
}

private void AddUserBetsData(IEnumerable<GameViewModel> res)
{
var allBets = betsRepository.GetUserBets(httpContextAccessor.HttpContext?.User.Identity.Name).ToDictionary(bet => bet.GameId, bet => bet);
foreach (var game in res)
{
game.UserHasBet = allBets.ContainsKey(game.GameId);
}
}

}

35 changes: 35 additions & 0 deletions Mundialito/Models/UserModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ public UserModel(MundialitoUser user)
[JsonPropertyName("Points")]
public int Points { get; set; }

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

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

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

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

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

public void SetGeneralBet(GeneralBetViewModel generalBet)
{
GeneralBet = generalBet;
Expand All @@ -43,6 +58,26 @@ public void SetGeneralBet(GeneralBetViewModel generalBet)
Points += generalBet.Points;
}
}

public void AddBet(BetViewModel bet)
{
if (bet.IsResolved)
{
Points += bet.Points;
if (bet.ResultWin)
{
Results++;
}
else if (bet.GameMarkWin)
{
Marks++;
}
if (bet.CardsWin)
YellowCards++;
if (bet.CornersWin)
Corners++;
}
}

}

Expand Down

0 comments on commit 9c81205

Please sign in to comment.