From 9c81205e2005efaeeddbbb3144720212254a0c75 Mon Sep 17 00:00:00 2001 From: "roi.ezra" Date: Thu, 11 Jul 2024 23:35:26 +0300 Subject: [PATCH] fixed the bug --- Mundialito/Controllers/TeamsController.cs | 22 ++++++++++++-- Mundialito/Models/UserModel.cs | 35 +++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/Mundialito/Controllers/TeamsController.cs b/Mundialito/Controllers/TeamsController.cs index ca27446..f379df0 100644 --- a/Mundialito/Controllers/TeamsController.cs +++ b/Mundialito/Controllers/TeamsController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using Mundialito.Models; +using Mundialito.DAL.Bets; namespace Mundialito.Controllers; @@ -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 logger, ITeamsRepository teamsRepository) + public TeamsController(ILogger logger, ITeamsRepository teamsRepository, IBetsRepository betsRepository, IHttpContextAccessor httpContextAccessor) { this.teamsRepository = teamsRepository; + this.betsRepository = betsRepository; + this.httpContextAccessor = httpContextAccessor; this.logger = logger; } @@ -38,8 +43,10 @@ public ActionResult GetTeamById(int id) [HttpGet("{id}/Games")] public IEnumerable 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] @@ -77,5 +84,14 @@ public void DeleteTeam(int id) logger.LogInformation("Deleted Team {0}", id); } + private void AddUserBetsData(IEnumerable 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); + } + } + } diff --git a/Mundialito/Models/UserModel.cs b/Mundialito/Models/UserModel.cs index 9ff04e2..cace0f6 100644 --- a/Mundialito/Models/UserModel.cs +++ b/Mundialito/Models/UserModel.cs @@ -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; @@ -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++; + } + } }