Example #1
0
func GameWithTeams(row MultiScanner) (*model.Game, error) {
	var game model.Game
	var homeLeague model.League
	var awayLeague model.League
	var season model.Season
	var home model.Team
	var away model.Team

	err := row.Scan(&game.ID, &season.ID, &game.StartTime, &game.HomeScore, &game.AwayScore, &game.Created, &game.Modified,
		&home.ID, &homeLeague.ID, &home.Name, &home.Created, &home.Modified,
		&away.ID, &awayLeague.ID, &away.Name, &away.Created, &away.Modified)

	if err != nil {
		return nil, err
	}

	home.League = &homeLeague
	away.League = &awayLeague

	game.Season = &season
	game.HomeTeam = &home
	game.AwayTeam = &away

	return &game, nil
}
Example #2
0
func (repo *PgRepository) CreateGame(game *model.Game) error {
	err := game.Validate(repo)
	if err != nil {
		return err
	}

	t := time.Now()

	var id int
	err = repo.manager.db.QueryRow(`INSERT INTO game(season_id, start_time, home_team_id, away_team_id, created, modified)
	    VALUES($1, $2, $3, $4, $5, $6) RETURNING id`,
		game.Season.ID, game.StartTime, game.HomeTeam.ID, game.AwayTeam.ID, t, t).Scan(&id)

	if err != nil {
		return err
	}

	game.ID = id
	game.Created = t
	game.Modified = t

	return nil
}