-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from NetSepio/main
Merging mian with prod
- Loading branch information
Showing
12 changed files
with
247 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package leaderboard | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/NetSepio/gateway/config/dbconfig" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func DynamicLeaderBoardUpdate(user_id, column_name string) { | ||
// Database connection setup (replace with your actual connection details) | ||
db := dbconfig.GetDb() | ||
|
||
// Check if the user_id exists in the LeaderBoard table | ||
var leaderboard Leaderboard | ||
err := db.Debug().Where("user_id = ?", user_id).First(&leaderboard).Error | ||
|
||
// If user_id does not exist, insert a new record with the initial review count | ||
newLeaderBoard := Leaderboard{ | ||
UserId: user_id, | ||
} | ||
|
||
if err != nil { | ||
if err == gorm.ErrRecordNotFound { | ||
|
||
// Use reflection to dynamically set the specified column value | ||
switch column_name { | ||
case "reviews": | ||
newLeaderBoard.Reviews = 1 | ||
case "domain": | ||
newLeaderBoard.Domain = 1 | ||
case "nodes": | ||
newLeaderBoard.Nodes = 1 | ||
case "d_wifi": | ||
newLeaderBoard.DWifi = 1 | ||
case "discord": | ||
newLeaderBoard.Discord = 1 | ||
case "twitter": | ||
newLeaderBoard.Twitter = 1 | ||
case "telegram": | ||
newLeaderBoard.Telegram = 1 | ||
default: | ||
log.Printf("Invalid column name") | ||
} | ||
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("New record inserted and reviews count initialized successfully!") | ||
return | ||
} | ||
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) | ||
} | ||
|
||
log.Println("Reviews count incremented successfully!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// ApplyRoutes applies router to gin Router | ||
// func ApplyRoutes(r *gin.RouterGroup) { | ||
// g := r.Group("/reviewerdetails") | ||
// { | ||
// g.GET("", getProfile) | ||
// } | ||
// } | ||
|
||
// func update(c *gin.Context) { | ||
// db := dbconfig.GetDb() | ||
// var request GetReviewerDetailsQuery | ||
// err := c.BindQuery(&request) | ||
|
||
// payload := GetReviewerDetailsPayload{ | ||
// Name: user.Name, | ||
// WalletAddress: user.WalletAddress, | ||
// ProfilePictureUrl: user.ProfilePictureUrl, | ||
// Discord: user.Discord, | ||
// Twitter: user.Twitter, | ||
// } | ||
// httpo.NewSuccessResponseP(200, "Profile fetched successfully", payload).SendD(c) | ||
// } | ||
|
||
package leaderboard | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/NetSepio/gateway/config/dbconfig" | ||
"github.com/NetSepio/gateway/models" | ||
"github.com/NetSepio/gateway/util/pkg/logwrapper" | ||
"github.com/TheLazarusNetwork/go-helpers/httpo" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// ApplyRoutes applies router to gin Router | ||
func ApplyRoutes(r *gin.RouterGroup) { | ||
g := r.Group("/getleaderboard") | ||
{ | ||
g.GET("", getLeaderboard) | ||
} | ||
} | ||
|
||
func getLeaderboard(c *gin.Context) { | ||
db := dbconfig.GetDb() | ||
|
||
var leaderboard []models.Leaderboard | ||
|
||
if err := db.Order("reviews desc").Find(&leaderboard).Error; err != nil { | ||
httpo.NewErrorResponse(http.StatusInternalServerError, "Unexpected error occurred").SendD(c) | ||
logwrapper.Error("failed to get leaderboard", err) | ||
return | ||
} | ||
|
||
if len(leaderboard) == 0 { | ||
httpo.NewErrorResponse(404, "No leaderboard entries found").SendD(c) | ||
return | ||
} | ||
|
||
httpo.NewSuccessResponseP(200, "Leaderboard fetched successfully", leaderboard).SendD(c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package leaderboard | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Leaderboard struct { | ||
ID string `gorm:"type:uuid;primary_key"` // 9. Id (Primary Key) | ||
Reviews int `gorm:"not null"` // 1. Reviews | ||
Domain int `gorm:"not null"` // 2. Domain (all projects) | ||
UserId string `gorm:"type:uuid;not null"` // 3. User | ||
Nodes int `gorm:"not null"` // 4. Nodes | ||
DWifi int `gorm:"not null"` // 5. DWifi | ||
Discord int `gorm:"not null"` // 6. Discord | ||
Twitter int `gorm:"not null"` // 7. Twitter | ||
Telegram int `gorm:"not null"` // 8. Telegram | ||
CreatedAt time.Time `gorm:"autoCreateTime"` // 10. CreatedAt | ||
UpdatedAt time.Time `gorm:"autoUpdateTime"` // 11. UpdatedAt | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.