Пример #1
0
func (ps PlayerService) handleGetSpecificPlayers(c *gin.Context, idList []string) {
	playerDao := dao.CreatePlayerDao(c)

	playerIds := make([]int64, len(idList))
	idx := 0
	for _, id := range idList {
		playerID, err := strconv.ParseInt(id, 10, 64)
		if err != nil {
			utils.GetGaeContext(c).Errorf("Not an integer: %v", err)
			c.AbortWithError(500, err)
			return
		}
		playerIds[idx] = playerID
		idx++
	}

	playerArray, err := playerDao.GetAllPlayersByID(playerIds)
	if err != nil {
		utils.GetGaeContext(c).Errorf("Error getting players by id: %v", err)
		c.AbortWithError(500, err)
		return
	}

	for index := range playerArray {
		addPlayerLinks(&playerArray[index], c)
	}

	players := &domain.Players{
		Players: playerArray,
	}

	c.JSON(200, players)
}
Пример #2
0
func (gs GameService) getGame(c *gin.Context) {
	leagueID := getLeagueIDFromURL(c)

	if leagueID <= 0 {
		c.Redirect(302, "/api/leagues")
		return
	}

	gameID := getGameIDFromURL(c)

	if gameID <= 0 {
		c.Redirect(302, fmt.Sprintf("/api/leagues/%d/games", leagueID))
		return
	}

	gameDao := dao.CreateGameDao(c)

	game, err := gameDao.GetGame(leagueID, gameID)

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error loading game: %v", err)
		c.AbortWithError(500, err)
		return
	}

	gs.addGameLinks(leagueID, game, c)
	c.JSON(200, game)
}
Пример #3
0
func (ps PlayerService) handleGetAllPlayers(c *gin.Context) {
	var currentPage = getCurrentPage(c)
	var recordsPerPage = 50
	var start = getStartRecord(currentPage, recordsPerPage)

	playerDao := dao.CreatePlayerDao(c)

	playerArray, totalPlayerCount, err := playerDao.GetPlayers(start, recordsPerPage)

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error loading players: %v", err)
		c.AbortWithError(500, err)
		return
	}

	if playerArray == nil {
		playerArray = []domain.Player{}
	}

	for index := range playerArray {
		addPlayerLinks(&playerArray[index], c)
	}

	players := &domain.Players{
		Players: playerArray,
	}

	addPaginationLinks(players, "/api/players", currentPage, recordsPerPage, totalPlayerCount)
	if isAuthenticated(c) {
		players.AddLink(domain.RelCreate, "/api/players")
	}

	c.JSON(200, players)
}
Пример #4
0
func (gs GameService) doSaveGame(game domain.Game, c *gin.Context) {
	gameDao := dao.CreateGameDao(c)

	savedGame, err := gameDao.SaveGame(game)

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error saving game: %v", err)
		c.AbortWithError(500, err)
	}

	gs.addGameLinks(game.LeagueID, savedGame, c)
	c.JSON(200, savedGame)
}
Пример #5
0
func (ps PlayerService) doSavePlayer(player domain.Player, c *gin.Context) {
	playerDao := dao.CreatePlayerDao(c)

	savedPlayer, err := playerDao.SavePlayer(player)

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error saving player: %v", err)
		c.AbortWithError(500, err)
	}

	addPlayerLinks(savedPlayer, c)
	c.JSON(200, savedPlayer)
}
Пример #6
0
func addGetPlayerListByIDLinks(games *domain.Games, playerIds []int64, c *gin.Context) {
	playerListURL, err := url.Parse("/api/players")

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error parsing URL: %v", err)
		c.AbortWithError(500, err)
		return
	}

	q := playerListURL.Query()
	for _, playerID := range playerIds {
		q.Add("id", fmt.Sprintf("%d", playerID))
	}
	playerListURL.RawQuery = q.Encode()

	games.AddLink(relPlayerList, playerListURL.String())
}
Пример #7
0
func (ps PlayerService) getPlayer(c *gin.Context) {
	playerID := getPlayerIDFromURL(c)

	if playerID <= 0 {
		c.Redirect(304, "/api/players")
		return
	}

	playerDao := dao.CreatePlayerDao(c)

	player, err := playerDao.GetPlayer(playerID)

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error loading player: %v", err)
		c.AbortWithError(500, err)
		return
	}

	addPlayerLinks(player, c)
	c.JSON(200, player)
}
Пример #8
0
func (gs GameService) getGames(c *gin.Context) {
	leagueID := getLeagueIDFromURL(c)

	if leagueID <= 0 {
		c.Redirect(302, "/api/leagues")
		return
	}

	currentPage := getCurrentPage(c)
	recordsPerPage := 50
	start := getStartRecord(currentPage, recordsPerPage)

	gameDao := dao.CreateGameDao(c)

	gameArray, totalGameCount, err := gameDao.GetGames(start, recordsPerPage, leagueID)

	if err != nil {
		utils.GetGaeContext(c).Errorf("Error loading games: %v", err)
		c.AbortWithError(500, err)
		return
	}

	if gameArray == nil {
		gameArray = []domain.Game{}
	}

	for index := range gameArray {
		gs.addGameLinks(leagueID, &gameArray[index], c)
	}

	games := &domain.Games{
		Games: gameArray,
		Total: totalGameCount,
	}

	gs.addGamesLinks(games, leagueID, currentPage, recordsPerPage, totalGameCount, c)

	c.JSON(200, games)
}
Пример #9
0
func CreateGameDao(c *gin.Context) GameDao {
	dao := createDao(utils.GetGaeContext(c))
	return GameDao{dao}
}
Пример #10
0
func CreateImportDao(c *gin.Context) ImportDao {
	dao := createDao(utils.GetGaeContext(c))
	return ImportDao{dao}
}
Пример #11
0
func CreatePlayerDao(c *gin.Context) PlayerDao {
	dao := createDao(utils.GetGaeContext(c))
	return PlayerDao{dao}
}
func (as AdminService) doImportScoreBoardV1(c *gin.Context) {
	utils.GetGaeRootContext(c).Infof("%#v", c.Request)

	importDao := dao.CreateImportDao(c)

	body, err := ioutil.ReadAll(c.Request.Body)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling ioutil.ReadAll(c.Request.Body) in doImportScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
		return
	}

	dbDumpUrl := string(body)

	httpClient := urlfetch.Client(utils.GetGaeRootContext(c))
	response, err := httpClient.Get(dbDumpUrl)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling httpClient.Get in doImportScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
		return
	}

	data, err := ioutil.ReadAll(response.Body)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling ioutil.ReadAll(response.Body) in doImportScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
		return
	}

	dump := MysqlDump{}

	err = xml.Unmarshal(data, &dump)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling xml.Unmarshal in doImportScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
		return
	}

	database := dump.Databases[0]

	playerTable := as._getTableByName(database, "player")
	leagueTable := as._getTableByName(database, "league")
	gameTable := as._getTableByName(database, "game")
	gameTeamTable := as._createLookupMap(as._getTableByName(database, "game_team"), "id")
	teamPlayersTable := as._createLookupMap(as._getTableByName(database, "team_players"), "team_id")

	playerTotal := len(playerTable.Rows)
	playerCount := 0
	leagueTotal := len(leagueTable.Rows)
	leagueCount := 0
	gameTotal := len(gameTable.Rows)
	gameCount := 0
	_, err = importDao.SetStatus(true, playerTotal, playerCount, leagueTotal, leagueCount, gameTotal, gameCount)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("importDao.setStatus: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	// Add players first
	as._deleteAll(dao.EntityPlayer, utils.GetGaeContext(c))
	playerDao := dao.CreatePlayerDao(c)

	playerConvertIdMap := make(map[string]int64)
	for _, playerRow := range playerTable.Rows {
		id := as._getFieldValueByName(playerRow, "id")
		name := as._getFieldValueByName(playerRow, "name")

		savedPlayer, err := playerDao.SavePlayer(domain.Player{
			Active: true,
			Name:   name,
		})

		if err != nil {
			utils.GetGaeRootContext(c).Errorf("Error calling playerDao.savePlayer in doImportScoreBoardV1: %v", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
			return
		}

		playerConvertIdMap[id] = savedPlayer.ID

		playerCount++
		_, err = importDao.SetStatus(true, playerTotal, playerCount, leagueTotal, leagueCount, gameTotal, gameCount)

		if err != nil {
			utils.GetGaeRootContext(c).Errorf("importDao.setStatus: %v", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
	}

	// Add leagues
	as._deleteAll(dao.EntityLeague, utils.GetGaeContext(c))
	leagueDao := dao.CreateLeagueDao(c)

	leagueConvertIdMap := make(map[string]int64)
	for _, leagueRow := range leagueTable.Rows {
		id := as._getFieldValueByName(leagueRow, "id")
		name := as._getFieldValueByName(leagueRow, "name")

		savedLeague, err := leagueDao.SaveLeague(domain.League{
			Active: true,
			Name:   name,
		})

		if err != nil {
			utils.GetGaeRootContext(c).Errorf("Error calling leagueDao.saveLeague in doImportScoreBoardV1: %v", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
			return
		}

		leagueConvertIdMap[id] = savedLeague.ID

		leagueCount++
		_, err = importDao.SetStatus(true, playerTotal, playerCount, leagueTotal, leagueCount, gameTotal, gameCount)

		if err != nil {
			utils.GetGaeRootContext(c).Errorf("importDao.setStatus: %v", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
	}

	// Add games
	as._deleteAll(dao.EntityGame, utils.GetGaeContext(c))
	gameDao := dao.CreateGameDao(c)

	for _, gameRow := range gameTable.Rows {
		gameDate := as._getFieldDateValueByName(gameRow, "game_date")
		team1IDString := as._getFieldValueByName(gameRow, "team1_id")
		team2IDString := as._getFieldValueByName(gameRow, "team2_id")
		leagueIDString := as._getFieldValueByName(gameRow, "league_id")

		team1 := as._createTeam(team1IDString, gameTeamTable, teamPlayersTable, playerConvertIdMap)
		team2 := as._createTeam(team2IDString, gameTeamTable, teamPlayersTable, playerConvertIdMap)

		game := domain.Game{
			GameDate: gameDate,
			LeagueID: leagueConvertIdMap[leagueIDString],
			Team1:    team1,
			Team2:    team2,
		}

		_, err := gameDao.SaveGame(game)

		if err != nil {
			utils.GetGaeRootContext(c).Errorf("Error calling gameDao.saveGame in doImportScoreBoardV1: %v", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			importDao.SetStatus(false, 0, 0, 0, 0, 0, 0)
			return
		}

		gameCount++
		_, err = importDao.SetStatus(true, playerTotal, playerCount, leagueTotal, leagueCount, gameTotal, gameCount)

		if err != nil {
			utils.GetGaeRootContext(c).Errorf("importDao.setStatus: %v", err)
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}
	}

	_, err = importDao.SetStatus(false, playerTotal, playerCount, leagueTotal, leagueCount, gameTotal, gameCount)

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("importDao.setStatus: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}
}
Пример #13
0
func CreateLeagueDao(c *gin.Context) LeagueDao {
	dao := createDao(utils.GetGaeContext(c))
	return LeagueDao{dao}
}