Skip to content

Commit

Permalink
Merge pull request #194 from NetSepio/main
Browse files Browse the repository at this point in the history
Merging mian with prod
  • Loading branch information
Rushikeshnimkar authored Aug 13, 2024
2 parents 8b3ff56 + 2d8678a commit e67cc9a
Show file tree
Hide file tree
Showing 12 changed files with 247 additions and 16 deletions.
6 changes: 5 additions & 1 deletion api/v1/delegateReviewCreation/delegateReviewCreation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/NetSepio/gateway/api/middleware/auth/paseto"
"github.com/NetSepio/gateway/api/v1/leaderboard"
"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/models"
"github.com/NetSepio/gateway/util/pkg/aptos"
Expand All @@ -20,7 +21,7 @@ import (
func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/delegateReviewCreation")
{
g.Use(paseto.PASETO(false))
g.Use(paseto.PASETO(true))
g.POST("", deletegateReviewCreation)
}
}
Expand Down Expand Up @@ -82,6 +83,9 @@ func deletegateReviewCreation(c *gin.Context) {
if err := db.Create(newReview).Error; err != nil {
httpo.NewSuccessResponseP(httpo.TXDbFailed, "transaction is successful but failed to store tx in db", payload).Send(c, 200)
return
} else {
userID := c.GetString(paseto.CTX_USER_ID)
leaderboard.DynamicLeaderBoardUpdate(userID, "reviews")
}

httpo.NewSuccessResponseP(200, "request successfully send, review will be delegated soon", payload).SendD(c)
Expand Down
3 changes: 3 additions & 0 deletions api/v1/domain/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"

"github.com/NetSepio/gateway/api/middleware/auth/paseto"
"github.com/NetSepio/gateway/api/v1/leaderboard"
"github.com/NetSepio/gateway/config/dbconfig"
"github.com/NetSepio/gateway/models"
"github.com/NetSepio/gateway/util/pkg/logwrapper"
Expand Down Expand Up @@ -68,6 +69,8 @@ func postDomain(c *gin.Context) {
logwrapper.Errorf("failed to create domain: %s", err)
httpo.NewErrorResponse(http.StatusInternalServerError, "failed to create domain").SendD(c)
return
} else {
leaderboard.DynamicLeaderBoardUpdate(userId, "domain")
}
payload := CreateDomainResponse{
TxtValue: txtValue, DomainId: domainId,
Expand Down
65 changes: 65 additions & 0 deletions api/v1/leaderboard/db.operations.go
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!")
}
62 changes: 62 additions & 0 deletions api/v1/leaderboard/leaderboard.go
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)
}
19 changes: 19 additions & 0 deletions api/v1/leaderboard/type.go
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
}
2 changes: 2 additions & 0 deletions api/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
flowid "github.com/NetSepio/gateway/api/v1/flowid"
"github.com/NetSepio/gateway/api/v1/getreviewerdetails"
"github.com/NetSepio/gateway/api/v1/getreviews"
"github.com/NetSepio/gateway/api/v1/leaderboard"
"github.com/NetSepio/gateway/api/v1/profile"
"github.com/NetSepio/gateway/api/v1/report"
"github.com/NetSepio/gateway/api/v1/sdkauthentication"
Expand Down Expand Up @@ -49,6 +50,7 @@ func ApplyRoutes(r *gin.RouterGroup) {
subscription.ApplyRoutes(v1)
summary.ApplyRoutes(v1)
sdkauthentication.ApplyRoutes(v1)
leaderboard.ApplyRoutes(v1)

}
}
6 changes: 4 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ func Init() {

// gin.SetMode(gin.ReleaseMode)

corsM := cors.New(cors.Config{AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
corsM := cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "Authorization"},
AllowCredentials: false,
MaxAge: 12 * time.Hour,
AllowOrigins: envconfig.EnvVars.ALLOWED_ORIGIN})
AllowOrigins: []string{"*"},
})
GinApp.Use(corsM)
api.ApplyRoutes(GinApp)

Expand Down
3 changes: 3 additions & 0 deletions config/dbconfig/dbconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,13 @@ func Init() error {
&migrate.UserStripePi{},
&migrate.Sotreus{},
&migrate.Erebrus{},
&migrate.Leaderboard{},
); err != nil {
log.Fatal(err)
}

// db.Exec(`ALTER TABLE leader_boards DROP COLUMN IF EXISTS users;
// CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`)
logwrapper.Log.Info("Congrats ! Automigration completed")

return nil
Expand Down
26 changes: 13 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/chromedp/cdproto v0.0.0-20220131204822-e6abebe7b8cd
github.com/chromedp/chromedp v0.7.7
github.com/ethereum/go-ethereum v1.10.15
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/cors v1.7.2
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/jackc/pgconn v1.12.1
Expand All @@ -18,7 +18,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/stripe/stripe-go/v76 v76.11.0
github.com/vk-rv/pvx v0.0.0-20210912195928-ac00bc32f6e7
golang.org/x/crypto v0.24.0
golang.org/x/crypto v0.26.0
google.golang.org/api v0.184.0
gorm.io/driver/postgres v1.3.8
gorm.io/gorm v1.23.7
Expand All @@ -29,7 +29,7 @@ require github.com/sashabaranov/go-openai v1.24.1
require (
cloud.google.com/go/auth v0.5.1 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.2 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic/loader v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
)
Expand All @@ -38,24 +38,24 @@ require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic v1.12.1 // indirect
github.com/chromedp/sysutil v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v0.0.0-20180603214616-504e848d77ea // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.5 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/go-playground/validator/v10 v10.22.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.1.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
Expand All @@ -72,7 +72,7 @@ require (
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -93,14 +93,14 @@ require (
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/arch v0.9.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/grpc v1.64.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit e67cc9a

Please sign in to comment.