Skip to content

Commit

Permalink
Added new fields to Leaderboard, ScoreBoard, and UserScoreBoard struc…
Browse files Browse the repository at this point in the history
…ts; updated corresponding models and database migrations; added new API handlers for BetaGraphqlQueryForLeaderboard and ErebrusQueryForLeaderboard; updated existing API handlers to include
  • Loading branch information
p-shubh committed Oct 22, 2024
1 parent f4500f1 commit 4bc8182
Show file tree
Hide file tree
Showing 11 changed files with 499 additions and 163 deletions.
48 changes: 26 additions & 22 deletions api/v1/leaderboard/cron.job.operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ func CronJobLeaderBoardUpdate(column_name string, leaderboard Leaderboard) {
if err == gorm.ErrRecordNotFound {

CreateScoreBoard(ScoreBoard{
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
Subscription: leaderboard.Subscription,
BetaTester: leaderboard.BetaTester,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
})
log.Println("New record inserted and " + column_name + " count initialized successfully!")
return
Expand All @@ -52,17 +54,19 @@ func CronJobLeaderBoardUpdate(column_name string, leaderboard Leaderboard) {
} else {

err = UpdateScoreBoard(leaderboard.ID, ScoreBoard{
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
Subscription: leaderboard.Subscription,
BetaTester: leaderboard.BetaTester,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
}, column_name, data.XP)
if err != nil {
log.Printf("failed to update the Reviews count: %v", err)
Expand Down
117 changes: 117 additions & 0 deletions api/v1/leaderboard/graphql.leaderboard.opearationss.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package leaderboard

import (
"fmt"
"log"

"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/config/models"
"github.com/NetSepio/gateway/graphql"
"github.com/google/uuid"
"gorm.io/gorm"
)

func BetaGraphqlQueryForLeaderboard() ([]string, error) {
// Database connection setup (replace with your actual connection details)
db := dbconfig.GetDb()

var userIds []string

// Call Beta Test Query
betaTestResponse, err := graphql.BetaTestQuery()
if err != nil {
log.Printf("Error performing Beta Test query: %v", err)
return nil, err // Return the error if the query fails
}

fmt.Println("Beta Test Query Results:")
for _, token := range betaTestResponse.Data.CurrentTokenDatasV2 {
fmt.Printf("Token Name: %s, Description: %s\n", token.TokenName, token.Description)
for _, ownership := range token.CurrentTokenOwnerships {
fmt.Printf("Owner Address: %s, Last Transaction: %s\n", ownership.OwnerAddress, ownership.LastTransactionTimestamp)

// Check if the user exists or create a new one if not
var user models.User
result := db.Where("wallet_address = ?", ownership.OwnerAddress).First(&user)
if result.Error != nil {
// If the user does not exist, create a new one
if result.Error == gorm.ErrRecordNotFound {
// Generate a new UUID for the user
user = models.User{
UserId: uuid.New().String(), // UUID generation
WalletAddress: &ownership.OwnerAddress,
}
if err := db.Create(&user).Error; err != nil {
log.Printf("Error creating new user: %v", err)
return userIds, err // Return if error occurs while creating user
} else {
userIds = append(userIds, user.UserId)
// leaderboard.DynamicLeaderBoardUpdate(user.UserId, "reviews")

}
fmt.Printf("Created new user with UserID: %s\n", user.UserId)
} else {
// If another error occurred, log and return it
log.Printf("Error querying user: %v", result.Error)
return nil, result.Error
}
} else {
// leaderboard.DynamicLeaderBoardUpdate(user.UserId, "reviews")
userIds = append(userIds, user.UserId)
}
}
}
return userIds, nil
}
func ErebrusQueryForLeaderboard() ([]string, error) {
// Database connection setup (replace with your actual connection details)
db := dbconfig.GetDb()

var userIds []string

// Call Beta Test Query
betaTestResponse, err := graphql.ErebrusQuery()
if err != nil {
log.Printf("Error performing Beta Test query: %v", err)
return nil, err // Return the error if the query fails
}

fmt.Println("Beta Test Query Results:")
for _, token := range betaTestResponse.Data.CurrentTokenDatasV2 {
fmt.Printf("Token Name: %s, Description: %s\n", token.TokenName, token.Description)
for _, ownership := range token.CurrentTokenOwnerships {
fmt.Printf("Owner Address: %s, Last Transaction: %s\n", ownership.OwnerAddress, ownership.LastTransactionTimestamp)

// Check if the user exists or create a new one if not
var user models.User
result := db.Where("wallet_address = ?", ownership.OwnerAddress).First(&user)
if result.Error != nil {
// If the user does not exist, create a new one
if result.Error == gorm.ErrRecordNotFound {
// Generate a new UUID for the user
user = models.User{
UserId: uuid.New().String(), // UUID generation
WalletAddress: &ownership.OwnerAddress,
}
if err := db.Create(&user).Error; err != nil {
log.Printf("Error creating new user: %v", err)
return userIds, err // Return if error occurs while creating user
} else {
userIds = append(userIds, user.UserId)
// leaderboard.DynamicLeaderBoardUpdate(user.UserId, "reviews")

}
fmt.Printf("Created new user with UserID: %s\n", user.UserId)
} else {
// If another error occurred, log and return it
log.Printf("Error querying user: %v", result.Error)
return nil, result.Error
}
} else {
// leaderboard.DynamicLeaderBoardUpdate(user.UserId, "reviews")
userIds = append(userIds, user.UserId)
}
}
}
return userIds, nil
}
84 changes: 55 additions & 29 deletions api/v1/leaderboard/leaderboard.db.operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ func DynamicLeaderBoardUpdate(user_id, column_name string) {
newLeaderBoard.Twitter = 1
case "telegram":
newLeaderBoard.Telegram = 1
case "subscription":
newLeaderBoard.Subscription = true
case "beta_tester":
newLeaderBoard.BetaTester = 1
default:
log.Printf("Invalid column name")
return
Expand All @@ -53,17 +57,19 @@ func DynamicLeaderBoardUpdate(user_id, column_name string) {
} else {

CreateScoreBoard(ScoreBoard{
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
Subscription: leaderboard.Subscription,
BetaTester: leaderboard.BetaTester,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
})
}
log.Println("New record inserted and reviews count initialized successfully!")
Expand All @@ -72,28 +78,44 @@ func DynamicLeaderBoardUpdate(user_id, column_name string) {
log.Printf("failed to query the LeaderBoard: %v", err)
}

// If user_id exists, increment the Reviews column by 1
err = db.Debug().Model(&leaderboard).Update(column_name, gorm.Expr(column_name+" + ?", 1)).Error
if err != nil {
log.Printf("failed to update the Reviews count: %v", err)
if column_name == "subscription" {
// If user_id exists, increment the Reviews column by 1
err = db.Debug().Model(&leaderboard).Update(column_name, true).Error
if err != nil {
log.Printf("failed to update the Reviews count: %v", err)
}
} else {
// If user_id exists, increment the Reviews column by 1
err = db.Debug().Model(&leaderboard).Update(column_name, gorm.Expr(column_name+" + ?", 1)).Error
if err != nil {
log.Printf("failed to update the Reviews count: %v", err)
}
}

data, err := GetActivityUnitXpByActivity(column_name)
if err != nil {
log.Printf("failed to get the ScoreBoard by ID: %v", err)
var data *ActivityUnitXp

if column_name != "subscription" {
data, err = GetActivityUnitXpByActivity(column_name)
if err != nil {
log.Printf("failed to get the ScoreBoard by ID: %v", err)
}
} else {
data.Activity = "true"
}
err = UpdateScoreBoard(leaderboard.ID, ScoreBoard{
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
ID: uuid.New().String(),
Reviews: leaderboard.Reviews,
Domain: leaderboard.Domain,
UserId: leaderboard.UserId,
Nodes: leaderboard.Nodes,
DWifi: leaderboard.DWifi,
Discord: leaderboard.Discord,
Twitter: leaderboard.Twitter,
Telegram: leaderboard.Telegram,
Subscription: leaderboard.Subscription,
BetaTester: leaderboard.BetaTester,
CreatedAt: leaderboard.CreatedAt,
UpdatedAt: leaderboard.UpdatedAt,
}, column_name, data.XP)
if err != nil {
log.Println("failed to update the ScoreBoard by ID : ", err.Error())
Expand Down Expand Up @@ -166,6 +188,8 @@ func UpdateScoreBoard(id string, updatedScore ScoreBoard, column_name string, va
score.Twitter = updatedScore.Twitter * value
case "telegram":
score.Telegram = updatedScore.Telegram * value
case "subscription":
score.Subscription = true
default:
log.Printf("Invalid column name: %s", column_name)
// return
Expand Down Expand Up @@ -202,6 +226,8 @@ func UpdateScoreBoard(id string, updatedScore ScoreBoard, column_name string, va
columnValue = updatedScore.Twitter * value
case "telegram":
columnValue = updatedScore.Telegram * value
case "subscription":
score.Subscription = true
default:
log.Printf("Invalid column name: %s", column_name)
// return
Expand Down
Loading

0 comments on commit 4bc8182

Please sign in to comment.