public Scorer GetScorerById(long id) { Scorer s = new Scorer(); string sql = $"SELECT * FROM Scorers WHERE id={id}"; using (var _dbConn = new TriviaDbContext()) { _dbConn.Open(); using (SQLiteCommand command = new SQLiteCommand(sql, _dbConn.Connection)) { using (SQLiteDataReader reader = command.ExecuteReader()) { reader.Read(); s.Id = id; s.Name = (string)reader["name"]; s.Teams = new List <Team>(); string teamIdsString = (string)reader["team_id_list"]; if (teamIdsString != " ") { TeamRepository teamRepo = new TeamRepository(); long[] teamIds = Array.ConvertAll(teamIdsString.Split(','), x => long.Parse(x)); foreach (var t in teamIds) { Team team = teamRepo.GetTeamById(t); s.Teams.Add(team); } } s.CreatedAt = new DateTime(long.Parse((string)reader["created_at"])); } } } return(s); }
public void Remove(Scorer scorer) { if (scorer.Teams.Count > 0) { TeamRepository teamRepo = new TeamRepository(); foreach (var t in scorer.Teams) { Team team = teamRepo.GetTeamById(t.Id); team.NumScorers -= 1; if (team.NumScorers == 0) { team.HasScorer = false; } teamRepo.Update(team); } } long id = scorer.Id; string sql = $"DELETE FROM Scorers WHERE id={id}"; using (var _dbConn = new TriviaDbContext()) { _dbConn.Open(); using (SQLiteCommand command = new SQLiteCommand(sql, _dbConn.Connection)) { command.ExecuteNonQuery(); } } }
public void AddTeamToScorer(Scorer scorer, Team team) { team.NumScorers += 1; team.HasScorer = true; TeamRepository teamRepo = new TeamRepository(); teamRepo.Update(team); Update(scorer); }
public void RemoveTeamFromScorer(Scorer scorer, Team team) { team.NumScorers -= 1; if (team.NumScorers == 0) { team.HasScorer = false; } TeamRepository teamRepo = new TeamRepository(); teamRepo.Update(team); scorer.Teams.Remove(team); Update(scorer); }