func (us UserService) startLoginProcess(c *gin.Context) {
	gaeCtx := utils.GetGaeRootContext(c)

	loginURL, err := appengineuser.LoginURL(gaeCtx, "")

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

	c.Redirect(302, loginURL)
}
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)
}
func ResolveUser() gin.HandlerFunc {
	return func(c *gin.Context) {
		gaeCtx := utils.GetGaeRootContext(c)
		currentGaeUser := appengineuser.Current(gaeCtx)

		var user *domain.User
		if currentGaeUser != nil {
			userDao := dao.CreateUserDao(c)

			var err error
			user, err = userDao.GetUserByID(currentGaeUser.ID)

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

			if user == nil {
				user = &domain.User{
					UserID: currentGaeUser.ID,
					Email:  currentGaeUser.Email,
				}
				userDao.SaveUser(user)
			}

			user.LoggedIn = true
			contextDefinition := GetGameContext(c)

			userKey := datastore.NewKey(gaeCtx, dao.EntityUser, user.UserID, 0, nil)

			if contextDefinition.IsUserOwner(userKey) {
				user.Role = domain.Admin
			} else {
				user.Role = domain.Standard
			}

			logoutURL, _ := appengineuser.LogoutURL(gaeCtx, "")
			user.AddLink(domain.RelLogout, logoutURL)
		} else {
			user = &domain.User{
				LoggedIn: false,
			}

			user.AddLink(domain.RelLogin, "/api/login")
		}

		c.Set(userKey, user)
	}
}
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)
}
Example #5
0
func CreateUserDao(c *gin.Context) UserDao {
	dao := createDao(utils.GetGaeRootContext(c))
	return UserDao{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
	}
}
func CreateContextDefinitionDao(c *gin.Context) ContextDefinitionDao {
	dao := createDao(utils.GetGaeRootContext(c))
	return ContextDefinitionDao{dao}
}