Skip to content

Commit

Permalink
"Added ScoreBoard model and related CRUD operations, updated leaderbo…
Browse files Browse the repository at this point in the history
…ard.db.operations.go to create and update ScoreBoard entries, and modified API routes to include ScoreBoard endpoint."
  • Loading branch information
p-shubh committed Sep 28, 2024
1 parent 83f66f0 commit 96ab220
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 3 deletions.
154 changes: 151 additions & 3 deletions api/v1/leaderboard/leaderboard.db.operations.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package leaderboard

import (
"fmt"
"log"

"github.com/NetSepio/gateway/config/dbconfig"
Expand Down Expand Up @@ -42,12 +43,28 @@ func DynamicLeaderBoardUpdate(user_id, column_name string) {
newLeaderBoard.Telegram = 1
default:
log.Printf("Invalid column name")
return
}
newLeaderBoard.ID = uuid.New().String()
// Initialize the specific column with 1 (assuming it's an integer field)
err = db.Debug().Create(&newLeaderBoard).Error
if err != nil {
log.Fatal("failed to insert new record:", err)
log.Println("[ ERROR ] failed to insert new record:", err)
} 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,
})
}
log.Println("New record inserted and reviews count initialized successfully!")
return
Expand All @@ -61,9 +78,140 @@ func DynamicLeaderBoardUpdate(user_id, column_name string) {
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)
}
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,
}, column_name, data.XP)

log.Println("Reviews count incremented successfully!")
}
func InsertDataInScoreBoard() {


// Function to create a new ScoreBoard entry
func CreateScoreBoard(score ScoreBoard) error {
db := dbconfig.GetDb()
result := db.Create(&score)
if result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
log.Println("ScoreBoard record not found")
return gorm.ErrRecordNotFound
} else {
log.Printf("failed to create a new ScoreBoard entry: %v", result.Error)
return result.Error
}
}
fmt.Println("New scoreboard created with ID:", score.ID)
return nil
}

// Function to fetch all ScoreBoard records
func GetAllScoreBoards() ([]ScoreBoard, error) {
var scoreboards []ScoreBoard
db := dbconfig.GetDb()

result := db.Find(&scoreboards)
if result.Error != nil {
return nil, result.Error
}
return scoreboards, nil
}

// Function to fetch a ScoreBoard by ID
func GetScoreBoardByID(id string) (*ScoreBoard, error) {
var score ScoreBoard
db := dbconfig.GetDb()

result := db.First(&score, "id = ?", id)
if result.Error != nil {
return nil, result.Error
}
return &score, nil
}

// Function to update an existing ScoreBoard
func UpdateScoreBoard(id string, updatedScore ScoreBoard, column_name string, value int) error {
var score ScoreBoard
db := dbconfig.GetDb()

result := db.First(&score, "id = ?", id)
if result.Error != nil {
return result.Error
}

switch column_name {
case "reviews":
score.Reviews = updatedScore.Reviews * value
case "domain":
score.Domain = updatedScore.Domain * value
case "nodes":
score.Nodes = updatedScore.Nodes * value
case "d_wifi":
score.DWifi = updatedScore.DWifi * value
case "discord":
score.Discord = updatedScore.Discord * value
case "twitter":
score.Twitter = updatedScore.Twitter * value
case "telegram":
score.Telegram = updatedScore.Telegram * value
default:
log.Printf("Invalid column name: %s", column_name)
// return
}

// Save the changes
result = db.Save(&score)
if result.Error != nil {
return result.Error
}

fmt.Println("ScoreBoard updated:", score.ID)
return nil
}

// GetAllActivityUnitXp retrieves all records from the ActivityUnitXp table.
func GetAllActivityUnitXp() ([]ActivityUnitXp, error) {
var activities []ActivityUnitXp
db := dbconfig.GetDb()

if err := db.Find(&activities).Error; err != nil {
return nil, fmt.Errorf("error retrieving all activity unit xp records: %v", err)
}
return activities, nil
}

func GetActivityUnitXpByActivity(activity string) (*ActivityUnitXp, error) {
var activityUnitXp ActivityUnitXp
db := dbconfig.GetDb()
if err := db.Where("activity = ?", activity).First(&activityUnitXp).Error; err != nil {
return nil, fmt.Errorf("error retrieving activity unit xp: %v", err)
}
return &activityUnitXp, nil
}

func UpdateActivityUnitXp(activity string, xp int) error {
db := dbconfig.GetDb()
if err := db.Model(&ActivityUnitXp{}).Where("activity = ?", activity).Update("xp", xp).Error; err != nil {
return fmt.Errorf("error updating activity unit xp: %v", err)
}
return nil
}

func DeleteActivityUnitXp(activity string) error {
db := dbconfig.GetDb()
if err := db.Where("activity = ?", activity).Delete(&ActivityUnitXp{}).Error; err != nil {
return fmt.Errorf("error deleting activity unit xp: %v", err)
}
return nil
}
22 changes: 22 additions & 0 deletions api/v1/leaderboard/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func ApplyRoutes(r *gin.RouterGroup) {
{
g.GET("", getLeaderboard)
}
h := r.Group("/getScoreboard")
{
h.GET("", getScoreBoard)
}
}

func getLeaderboard(c *gin.Context) {
Expand All @@ -37,3 +41,21 @@ func getLeaderboard(c *gin.Context) {

httpo.NewSuccessResponseP(200, "Leaderboard fetched successfully", leaderboard).SendD(c)
}
func getScoreBoard(c *gin.Context) {
db := dbconfig.GetDb()

var scoreBoard []models.ScoreBoard

if err := db.Order("reviews desc").Find(&scoreBoard).Error; err != nil {
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c)
logwrapper.Error("failed to get scoreBoard", err)
return
}

if len(scoreBoard) == 0 {
httpo.NewErrorResponse(404, "No ScoreBoard entries found").SendD(c)
return
}

httpo.NewSuccessResponseP(200, "ScoreBoard fetched successfully", scoreBoard).SendD(c)
}
13 changes: 13 additions & 0 deletions api/v1/leaderboard/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,16 @@ type ActivityUnitXp struct {
Activity string `gorm:"not null;unique"` // Name of the activity (e.g., Reviews, Domain, etc.)
XP int `gorm:"not null"`
}
type ScoreBoard struct {
ID string `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
Reviews int
Domain int
UserId string `gorm:"type:uuid;not null"`
Nodes int
DWifi int
Discord int
Twitter int
Telegram int
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}
2 changes: 2 additions & 0 deletions config/dbconfig/dbconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ func Init() error {
&migrate.Leaderboard{},
&migrate.NftSubscription{},
&migrate.DVPNNFTRecord{},
&migrate.ScoreBoard{},
&migrate.ActivityUnitXp{},
); err != nil {
log.Fatal(err)
}
Expand Down
14 changes: 14 additions & 0 deletions models/leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,17 @@ type Leaderboard struct {
func (Leaderboard) TableName() string {
return "leaderboards"
}

type ScoreBoard struct {
ID string `gorm:"type:uuid;default:uuid_generate_v4();primary_key"`
Reviews int
Domain int
UserId string `gorm:"type:uuid;not null"`
Nodes int
DWifi int
Discord int
Twitter int
Telegram int
CreatedAt time.Time `gorm:"autoCreateTime"`
UpdatedAt time.Time `gorm:"autoUpdateTime"`
}

0 comments on commit 96ab220

Please sign in to comment.