Skip to content

Commit

Permalink
added more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ezraroi committed May 21, 2024
1 parent 24a5724 commit 763321f
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 132 deletions.
13 changes: 13 additions & 0 deletions Mundialito/Configuration/JwtTokenSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

namespace Mundialito.Configuration;

public class JwtTokenSettings
{
public const string Key = "JwtTokenSettings";

public string? ValidIssuer { get; set; }
public string? ValidAudience { get; set; }
public string? SymmetricSecurityKey { get; set; }
public string? JwtRegisteredClaimNamesSub { get; set; }

}
14 changes: 8 additions & 6 deletions Mundialito/Controllers/BetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public ActionResult<BetViewModel> GetBetById(int id)
var item = betsRepository.GetBet(id);
if (item == null)
return NotFound(new ErrorMessage{ Message = string.Format("Bet with id '{0}' not found", id)});

return Ok(new BetViewModel(item, dateTimeProvider.UTCNow));
}

Expand Down Expand Up @@ -94,14 +93,15 @@ public async Task<ActionResult<NewBetModel>> PostBet(NewBetModel bet)
return BadRequest(new ErrorMessage{ Message = e.Message});
}
var res = betsRepository.InsertBet(newBet);
Trace.TraceInformation("Posting new Bet: {0}", newBet);
logger.LogInformation("Posting new Bet from {}", user.UserName);
betsRepository.Save();
bet.BetId = res.BetId;
AddLog(ActionType.CREATE, string.Format("Posting new Bet: {0}", res));
if (ShouldSendMail())
{
SendBetMail(newBet, user);
}
logger.LogInformation("Bet os user {} was saved", user.UserName);
return Ok(bet);
}

Expand Down Expand Up @@ -130,13 +130,14 @@ public async Task<ActionResult<NewBetModel>> UpdateBet(int id, UpdateBetModel be
} catch (Exception e) {
return BadRequest(new ErrorMessage{ Message = e.Message});
}
logger.LogInformation("Updating bet from {}", user.UserName);
betsRepository.Save();
Trace.TraceInformation("Updating Bet: {0}", betToUpdate);
AddLog(ActionType.UPDATE, string.Format("Updating Bet: {0}", betToUpdate));
if (ShouldSendMail())
{
SendBetMail(betToUpdate, user);
}
logger.LogInformation("Bet {} of {} was updated", id, user.UserName);
return Ok(new NewBetModel(id, bet));
}

Expand All @@ -156,10 +157,11 @@ public async Task<IActionResult> DeleteBet(int id)
} catch (Exception e) {
return BadRequest(new ErrorMessage{ Message = e.Message});
}
logger.LogInformation("Deleting bet {} of {}", id, user.UserName);
betsRepository.DeleteBet(id);
betsRepository.Save();
Trace.TraceInformation("Deleting Bet {0}", id);
AddLog(ActionType.DELETE, string.Format("Deleting Bet: {0}", id));
logger.LogInformation("Bet {} of {} was deleted", id, user.UserName);
return Ok();
}

Expand All @@ -172,7 +174,7 @@ private void AddLog(ActionType actionType, String message)
}
catch (Exception e)
{
Trace.TraceError("Exception during log. Exception: {0}", e.Message);
logger.LogError("Exception during log. Exception: {0}", e.Message);
}
}

Expand All @@ -197,7 +199,7 @@ private void SendBetMail(Bet bet, MundialitoUser user)
}
catch (Exception ex)
{
Trace.TraceError("Exception during mail sending. Exception: {0}", ex.Message);
logger.LogError("Exception during mail sending. Exception: {0}", ex.Message);
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions Mundialito/Controllers/GamesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ public ActionResult<IEnumerable<BetViewModel>> GetGameBets(int id)
var game = gamesRepository.GetGame(id);
if (game == null)
return NotFound(new ErrorMessage{ Message = string.Format("Game with id '{0}' not found", id)});

if (game.IsOpen(dateTimeProvider.UTCNow))
return BadRequest(new ErrorMessage{ Message = String.Format("Game '{0}' is stil open for betting", id)});

return Ok(betsRepository.GetGameBets(id).Select(item => new BetViewModel(item, dateTimeProvider.UTCNow)).OrderByDescending(bet => bet.Points));
}

Expand All @@ -89,15 +87,13 @@ public async Task<ActionResult<BetViewModel>> GetGameUserBet(int id)
{
var user = await userManager.FindByNameAsync(httpContextAccessor.HttpContext?.User.Identity.Name);
if (user == null)
{
return Unauthorized();
}
var game = GetGameByID(id);
var uid = user.Id;
var item = betsRepository.GetGameBets(id).SingleOrDefault(bet => bet.User.Id == uid);
if (item == null)
{
Trace.TraceInformation("No bet found for game {0} and user {1}, creating empty Bet", game.Result, uid);
logger.LogInformation("No bet found for game {0} and user {1}, creating empty Bet", game.Result, uid);
return Ok(new BetViewModel() { BetId = -1, HomeScore = null, AwayScore = null, IsOpenForBetting = true, IsResolved = false, Game = new BetGame() { GameId = id } });
}
return Ok(new BetViewModel(item, dateTimeProvider.UTCNow));
Expand All @@ -119,13 +115,15 @@ public ActionResult<NewGameModel> PostGame(NewGameModel game)
{
return BadRequest(new ErrorMessage{ Message = "Home team and Away team can not be the same team"});
}
var newGame = new Game();
newGame.HomeTeamId = game.HomeTeam.TeamId;
newGame.AwayTeamId = game.AwayTeam.TeamId;
newGame.StadiumId = game.Stadium.StadiumId;
newGame.Date = game.Date;
var newGame = new Game
{
HomeTeamId = game.HomeTeam.TeamId,
AwayTeamId = game.AwayTeam.TeamId,
StadiumId = game.Stadium.StadiumId,
Date = game.Date
};
var res = gamesRepository.InsertGame(newGame);
Trace.TraceInformation("Posting new Game: {0}", game);
logger.LogInformation("Posting new Game: {0}", game);
gamesRepository.Save();
game.GameId = res.GameId;
game.IsOpen = true;
Expand All @@ -145,7 +143,7 @@ public ActionResult<PutGameModelResult> PutGame(int id, PutGameModel game)

if (item.IsOpen(dateTimeProvider.UTCNow) && (game.HomeScore != null || game.AwayScore != null || game.CornersMark != null || game.CardsMark != null))
return BadRequest(new ErrorMessage{ Message = "Open game can not be updated with results"});

logger.LogInformation("Resolving bet {} with {}", id, game);
item.AwayScore = game.AwayScore;
item.HomeScore = game.HomeScore;
item.CardsMark = game.CardsMark;
Expand All @@ -155,10 +153,11 @@ public ActionResult<PutGameModelResult> PutGame(int id, PutGameModel game)
if (item.IsBetResolved(dateTimeProvider.UTCNow))
{
AddLog(ActionType.UPDATE, String.Format("Will resolve bets of game {0}", item.GameId));
Trace.TraceInformation("Will reoslve Game {0} bets", id);
logger.LogInformation("Will reoslve Game {0} bets", id);
betsResolver.ResolveBets(item);
}
AddLog(ActionType.UPDATE, String.Format("Updating Game {0}", item));
logger.LogInformation("Bet {} was resolved", id);
return Ok(new PutGameModelResult(item, dateTimeProvider.UTCNow));
}

Expand All @@ -169,10 +168,11 @@ public IActionResult DeleteGame(int id)
var game = gamesRepository.GetGame(id);
if (game == null)
return NotFound(new ErrorMessage{ Message = string.Format("No such game with id '{0}'", id)});
Trace.TraceInformation("Deleting Game {0}", id);
logger.LogInformation("Deleting Game {0}", id);
gamesRepository.DeleteGame(id);
gamesRepository.Save();
AddLog(ActionType.DELETE, String.Format("Deleting Game {0}", id));
logger.LogInformation("Game {0} was deleted", id);
return Ok();
}

Expand All @@ -194,7 +194,7 @@ private void AddLog(ActionType actionType, String message)
}
catch (Exception e)
{
Trace.TraceError("Exception during log. Exception: {0}", e.Message);
logger.LogError("Exception during log. Exception: {0}", e.Message);
}
}

Expand All @@ -207,9 +207,10 @@ private void AddMonkeyBet(Game res)
{
if (task.Result == null)
{
Trace.TraceError("Monkey user {0} was not found, will not add monkey bet", monkeyUserName);
logger.LogError("Monkey user {0} was not found, will not add monkey bet", monkeyUserName);
return;
}
logger.LogInformation("Adding monkey user bet");
var randomResults = new RandomResults();
var result = randomResults.GetRandomResult();
betsRepository.InsertBet(new Bet()
Expand All @@ -222,10 +223,9 @@ private void AddMonkeyBet(Game res)
CornersMark = randomResults.GetRandomMark()
});
betsRepository.Save();
logger.LogInformation("Monkey bet was saved");
}
);


}
}
}
Expand Down
29 changes: 14 additions & 15 deletions Mundialito/Controllers/GeneralBetsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public ActionResult<GeneralBetViewModel> GetUserGeneralBet(string username)
var item = generalBetsRepository.GetUserGeneralBet(username);
if (item == null)
return NotFound(string.Format("User '{0}' dosen't have a general bet yet", username));

return Ok(new GeneralBetViewModel(item, tournamentTimesUtils.GetGeneralBetsCloseTime()));
}

Expand All @@ -76,9 +75,7 @@ public ActionResult<GeneralBetViewModel> GetGeneralBetById(int id)
{
var item = generalBetsRepository.GetGeneralBet(id);
if (item == null)
{
return NotFound(new ErrorMessage { Message = string.Format("General Bet with id '{0}' not found", id)});
}
return Ok(new GeneralBetViewModel(item, tournamentTimesUtils.GetGeneralBetsCloseTime()));
}

Expand All @@ -94,31 +91,31 @@ public async Task<ActionResult<NewGeneralBetModel>> PostBet(NewGeneralBetModel n
}
var user = await userManager.FindByNameAsync(httpContextAccessor.HttpContext?.User.Identity.Name);
if (user == null)
{
return Unauthorized();
}
var generalBet = new GeneralBet();
generalBet.User = user;
generalBet.WinningTeamId = newBet.WinningTeamId;
generalBet.GoldBootPlayerId = newBet.GoldenBootPlayerId;
var generalBet = new GeneralBet
{
User = user,
WinningTeamId = newBet.WinningTeamId,
GoldBootPlayerId = newBet.GoldenBootPlayerId
};
var res = generalBetsRepository.InsertGeneralBet(generalBet);
Trace.TraceInformation("Posting new General Bet: {0}", generalBet);
logger.LogInformation("Posting new general bet {} from {}", generalBet, user.UserName);
generalBetsRepository.Save();
newBet.GeneralBetId = res.GeneralBetId;
AddLog(ActionType.CREATE, String.Format("Posting new Generel Bet: {0}", res));
logger.LogInformation("Saved general bet of {}", user.UserName);
return Ok(newBet);
}

[HttpPut]
[Authorize]
public async Task<ActionResult<UpdateGenralBetModel>> UpdateBet(int id, UpdateGenralBetModel bet)
{
if (dateTimeProvider.UTCNow > tournamentTimesUtils.GetGeneralBetsCloseTime())
return BadRequest("General bets are already closed for betting");
var user = await userManager.FindByNameAsync(httpContextAccessor.HttpContext?.User.Identity.Name);
if (user == null)
{
return Unauthorized();
}
var betToUpdate = generalBetsRepository.GetGeneralBet(id);
if (betToUpdate.User.Id != user.Id)
{
Expand All @@ -127,8 +124,9 @@ public async Task<ActionResult<UpdateGenralBetModel>> UpdateBet(int id, UpdateGe
}
betToUpdate.WinningTeamId = bet.WinningTeamId;
betToUpdate.GoldBootPlayerId = bet.GoldenBootPlayerId;
logger.LogInformation("Updating general bet of {} with {}", user.UserName, betToUpdate);
generalBetsRepository.Save();
Trace.TraceInformation("Updating General Bet: {0}", betToUpdate);
logger.LogInformation("Updated general bet of {}", user.UserName);
AddLog(ActionType.UPDATE, String.Format("Updating new Generel Bet: {0}", betToUpdate));
return bet;
}
Expand All @@ -149,9 +147,10 @@ public IActionResult ResolveGeneralBet(int id, ResolveGeneralBetModel resolvedBe
AddLog(ActionType.ERROR, string.Format("General Bet '{0}' dosen't exits", id));
return NotFound(new ErrorMessage { Message = string.Format("General Bet '{0}' dosen't exits", id)});
}
Trace.TraceInformation("Resolved General Bet '{0}' with data: {1}", id, resolvedBet);
logger.LogInformation("Resolving general bet {0} with data: {1}", id, resolvedBet);
item.Resolve(resolvedBet.PlayerIsRight, resolvedBet.TeamIsRight);
generalBetsRepository.Save();
logger.LogInformation("Resolved general bet {0}", id);
AddLog(ActionType.UPDATE, String.Format("Resolved Generel Bet: {0}", item));
return Ok();
}
Expand Down Expand Up @@ -180,7 +179,7 @@ private void AddLog(ActionType actionType, String message)
}
catch (Exception e)
{
Trace.TraceError("Exception during log. Exception: {0}", e.Message);
logger.LogError("Exception during log. Exception: {0}", e.Message);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Mundialito/Controllers/StadiumsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public Stadium PutStadium(int id, Stadium stadium)
[Authorize(Roles = "Admin")]
public void DeleteStadium(int id)
{
Trace.TraceInformation("Deleting Stadium {0}", id);
logger.LogInformation("Deleting Stadium {0}", id);
stadiumsRepository.DeleteStadium(id);
stadiumsRepository.Save();
logger.LogInformation("Deleted Stadium {0}", id);
}

}
Expand Down
4 changes: 2 additions & 2 deletions Mundialito/Controllers/TeamsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public IEnumerable<Team> GetAllTeams()
public ActionResult<Team> GetTeamById(int id)
{
var item = teamsRepository.GetTeam(id);

if (item == null)
return NotFound(new ErrorMessage{ Message = string.Format("Team with id '{0}' not found", id)});
return Ok(item);
Expand Down Expand Up @@ -71,9 +70,10 @@ public Team PutTeam(int id, Team team)
[Authorize(Roles = "Admin")]
public void DeleteTeam(int id)
{
Trace.TraceInformation("Deleting Team {0}", id);
logger.LogInformation("Deleting Team {0}", id);
teamsRepository.DeleteTeam(id);
teamsRepository.Save();
logger.LogInformation("Deleted Team {0}", id);
}

}
Expand Down
Loading

0 comments on commit 763321f

Please sign in to comment.