// GET api/<controller> public IEnumerable<Game> GetAllGames() { try { List<Game> games = new List<Game>(); string connectionString = ConfigurationManager.ConnectionStrings["FreeFootieConnectionString"].ConnectionString; using (FreeFootieDataContext myData = new FreeFootieDataContext(connectionString)) { var results = (from c in myData.games join d in myData.locations on c.locationID equals d.id orderby c.id ascending select new { game = c, locationName = d.name }).ToList(); foreach (var temp in results) { Game game = new Game(); game.id = temp.game.id; game.away = temp.game.awayTeamID; game.duration = temp.game.duration; game.home = temp.game.homeTeamID; game.location = temp.game.locationID; game.locationName = temp.locationName; game.date = "<<" + temp.game.startTime.ToString("yyyy-MM-dd HH:mm:ss") + "~"+ temp.game.duration +"h>>"; game.state = temp.game.state; game.homeTeamScore = temp.game.homeTeamScore; game.awayTeamScore = temp.game.awayTeamScore; games.Add(game); } } return games; } catch (Exception ex) { Common.ErrorCatch(ex, "GameController.GetAllGames"); return null; } }
// GET api/<controller>/5 public Game GetGameById(int id) { try { string connectionString = Properties.Settings.Default.FreeFootieConnectionString; using (FreeFootieDataContext myData = new FreeFootieDataContext(connectionString)) { var temp = (from c in myData.games join d in myData.locations on c.locationID equals d.id where c.id == id select new { game = c, locationName = d.name }).SingleOrDefault(); Game game = new Game(); game.id = id; game.away = temp.game.awayTeamID; game.duration = temp.game.duration; game.home = temp.game.homeTeamID; game.location = temp.game.locationID; game.locationName = temp.locationName; game.date = "<<" + temp.game.startTime.ToString("yyyy-MM-dd HH:mm:ss") + "~" + temp.game.duration + "h>>"; game.state = temp.game.state; game.homeTeamScore = temp.game.homeTeamScore; game.awayTeamScore = temp.game.awayTeamScore; return game; } } catch (Exception ex) { Common.ErrorCatch(ex, "GameController.GetGameById"); return null; } }
// POST api/<controller> public void PostGame(Game game) { try { string connectionString = Properties.Settings.Default.FreeFootieConnectionString; using (FreeFootieDataContext myData = new FreeFootieDataContext(connectionString)) { using (TransactionScope myScope = new TransactionScope()) { game newGame = new game(); var newID = (from c in myData.games select c.id).Max(); newGame.id = newID + 1; newGame.awayTeamID = game.away; newGame.duration = game.duration; newGame.homeTeamID = game.home; newGame.locationID = game.location; string[] date_duration = game.date.Replace("<", string.Empty).Replace(">", string.Empty).Replace("h", string.Empty).Split('~'); if (date_duration.Count() > 0) { newGame.startTime = Convert.ToDateTime(date_duration[0]); } if (date_duration.Count() > 1) { newGame.duration = Convert.ToInt32(date_duration[1]); } newGame.state = game.state; newGame.homeTeamScore = game.homeTeamScore; newGame.awayTeamScore = game.awayTeamScore; myData.games.InsertOnSubmit(newGame); myData.SubmitChanges(); myScope.Complete(); } } } catch (Exception ex) { Common.ErrorCatch(ex, "GameController.PostGame"); } }