Ejemplo n.º 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)
}
Ejemplo n.º 2
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)
}
func (as AdminService) prepareImportScoreBoardV1(c *gin.Context) {
	importDefinition := domain.ScoreBoardV1Import{}

	importDefinition.AddLink(relImport, "/api/admin/import/scoreboardv1")

	c.JSON(200, importDefinition)
}
Ejemplo n.º 4
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)
}
func (cds contextDefinitionService) checkID(c *gin.Context) {
	var checkResult struct {
		ID          string `json:"id"`
		IDAvailable bool   `json:"available"`
		Valid       bool   `json:"valid"`
	}

	checkResult.ID = c.Params.ByName("id")

	if cds._isIDValid(checkResult.ID) {
		contextDefinitionDao := createContextDefinitionDao(c)
		idExists, err := contextDefinitionDao.checkIDExists(checkResult.ID)

		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		checkResult.IDAvailable = idExists == false
		checkResult.Valid = true
	} else {
		checkResult.Valid = false
	}

	c.JSON(http.StatusOK, checkResult)

}
Ejemplo n.º 6
0
func (ls LeagueService) getLeagues(c *gin.Context) {
	var currentPage = getCurrentPage(c)
	var recordsPerPage = 50
	var start = getStartRecord(currentPage, recordsPerPage)

	leagueDao := dao.CreateLeagueDao(c)

	leagueArray, totalLeagueCount, err := leagueDao.GetLeagues(start, recordsPerPage)

	if err != nil {
		c.AbortWithError(500, err)
		return
	}

	if leagueArray == nil {
		leagueArray = []domain.League{}
	}

	for index := range leagueArray {
		addLeagueLinks(&leagueArray[index], c)
	}

	leagues := &domain.Leagues{
		Leagues: leagueArray,
	}

	addPaginationLinks(leagues, "/api/leagues", currentPage, recordsPerPage, totalLeagueCount)

	if isAuthenticated(c) {
		leagues.AddLink(domain.RelCreate, "/api/leagues")
	}

	c.JSON(200, leagues)
}
func (cds contextDefinitionService) saveContext(c *gin.Context) {
	var contextDefinition ContextDefinition

	c.Bind(&contextDefinition)

	if !cds._isValid(contextDefinition) {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	user := getCurrentUserFromGinContext(c)

	// Run all of this in a transaction (even if we are on service level)
	err := datastore.RunInTransaction(getGaeContext(c), func(gaeCtx appengine.Context) error {
		contextDao := contextDefinitionDao{dao{gaeCtx}}
		idExists, err := contextDao.checkIDExists(contextDefinition.ID)

		if err != nil {
			return err
		}

		if idExists {
			return ErrIDAlreadyExists
		}

		contextDefinition.Owner = datastore.NewKey(gaeCtx, entityUser, user.UserID, 0, nil)
		contextDefinition.Active = true

		err = contextDao.saveContext(contextDefinition)

		if err != nil {
			return err
		}

		return nil
	}, &datastore.TransactionOptions{
		XG: true,
	})

	if err != nil {
		returnStatus := http.StatusInternalServerError
		if err == ErrIDAlreadyExists {
			returnStatus = http.StatusConflict
		}

		c.AbortWithError(returnStatus, err)
		return
	}

	contextDefinition.RemoveLink(relCreate)
	contextDefinition.RemoveLink(relCheckID)

	if productionDomain := os.Getenv("PRODUCTION_DOMAIN"); productionDomain != "" {
		contextDefinition.AddLink(relSelf, fmt.Sprintf("http://%s.%s", contextDefinition.ID, productionDomain))
	}

	c.JSON(200, contextDefinition)
}
func (cds contextDefinitionService) prepareNewContext(c *gin.Context) {
	newContextDefinition := ContextDefinition{}
	newContextDefinition.Active = true

	newContextDefinition.AddLink(relCheckID, "/api/context/checkid/")
	newContextDefinition.AddLink(relCreate, "/api/context")

	c.JSON(http.StatusOK, newContextDefinition)
}
Ejemplo n.º 9
0
func (ls LeagueService) doSaveLeague(league domain.League, c *gin.Context) {
	leagueDao := dao.CreateLeagueDao(c)

	savedLeague, err := leagueDao.SaveLeague(league)

	if err != nil {
		c.AbortWithError(500, err)
	}

	addLeagueLinks(savedLeague, c)
	c.JSON(200, savedLeague)
}
Ejemplo n.º 10
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)
}
Ejemplo n.º 11
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)
}
func (as AdminService) importScoreBoardV1Status(c *gin.Context) {
	importDao := dao.CreateImportDao(c)

	importStatus, err := importDao.GetStatus()

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

	if importStatus == nil {
		importStatus = &domain.ScoreBoardV1ImportStatus{}
	}

	importStatus.AddLink(relImportStatus, "/api/admin/import/scoreboardv1/status")

	c.JSON(200, importStatus)
}
Ejemplo n.º 13
0
func (ls LeagueService) getLeague(c *gin.Context) {
	leagueID := getLeagueIDFromURL(c)

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

	leagueDao := dao.CreateLeagueDao(c)

	league, err := leagueDao.GetLeague(leagueID)

	if err != nil {
		c.AbortWithError(500, err)
		return
	}

	addLeagueLinks(league, c)
	c.JSON(200, league)
}
Ejemplo n.º 14
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)
}
func (as AdminService) importScoreBoardV1(c *gin.Context) {
	var importDefinition domain.ScoreBoardV1Import

	c.Bind(&importDefinition)

	if importDefinition.DbDumpUrl == "" {
		c.AbortWithStatus(http.StatusBadRequest)
		return
	}

	createTask := &taskqueue.Task{
		Path:    "/tasks/import/scoreboardv1",
		Payload: []byte(importDefinition.DbDumpUrl),
	}
	hostName, _ := appengine.ModuleHostname(utils.GetGaeRootContext(c), appengine.ModuleName(utils.GetGaeRootContext(c)), "", "")
	createTask.Header = http.Header{}
	createTask.Header.Set("Host", hostName)
	createTask.Header.Set(utils.NamespaceHeader, utils.GetNamespace(c))

	_, err := taskqueue.Add(utils.GetGaeRootContext(c), createTask, "contextqueue")

	if err != nil {
		utils.GetGaeRootContext(c).Errorf("Error calling taskqueue.Add in importScoreBoardV1: %v", err)
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	importDao := dao.CreateImportDao(c)
	importStatus, err := importDao.SetStatus(true, 0, 0, 0, 0, 0, 0)

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

	importStatus.AddLink(relImportStatus, "/api/admin/import/scoreboardv1/status")

	c.JSON(200, importStatus)
}
Ejemplo n.º 16
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)
}
func (cds ContextDefinitionService) getContext(c *gin.Context) {
	c.JSON(200, GetGameContext(c))
}
Ejemplo n.º 18
0
func (us UserService) getCurrentUser(c *gin.Context) {
	user := getCurrentUserFromGinContext(c)
	c.JSON(200, user)
}
Ejemplo n.º 19
0
func (us userService) getCurrentUser(c *gin.Context) {
	user := getCurrentUserFromGinContext(c)
	c.JSON(http.StatusOK, user)
}