Skip to content

Commit cacaf70

Browse files
committed
Removed gin.SetMode(gin.DebugMode) and dbconfig.DbInit() from main function
1 parent ddcd899 commit cacaf70

4 files changed

Lines changed: 169 additions & 2 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package token
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/NetSepio/erebrus-gateway/config/dbconfig"
7+
"github.com/NetSepio/erebrus-gateway/models"
8+
"github.com/gin-gonic/gin"
9+
"github.com/google/uuid"
10+
)
11+
12+
func ApplyRoutesSubscriptionToken(r *gin.RouterGroup) {
13+
g := r.Group("/token")
14+
{
15+
g.POST("", CreateSubscriptionToken)
16+
g.GET("/all", GetSubscriptionTokens)
17+
g.GET("/:id", GetSubscriptionToken)
18+
g.PATCH("/:id", UpdateSubscriptionToken)
19+
g.DELETE("/:id", DeleteSubscriptionToken)
20+
21+
}
22+
}
23+
24+
// Create Subscription Token
25+
func CreateSubscriptionToken(c *gin.Context) {
26+
var token models.SubscriptionToken
27+
if err := c.ShouldBindJSON(&token); err != nil {
28+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
29+
return
30+
}
31+
32+
DB := dbconfig.GetDb()
33+
34+
tx := DB.Create(&token)
35+
if tx.Error != nil {
36+
c.JSON(http.StatusInternalServerError, gin.H{"error": tx.Error.Error()})
37+
return
38+
}
39+
c.JSON(http.StatusCreated, token)
40+
}
41+
42+
// Get All Subscription Tokens
43+
func GetSubscriptionTokens(c *gin.Context) {
44+
var tokens []models.SubscriptionToken
45+
DB := dbconfig.GetDb()
46+
47+
tx := DB.Find(&tokens)
48+
if tx.Error != nil {
49+
c.JSON(http.StatusInternalServerError, gin.H{"error": tx.Error.Error()})
50+
return
51+
}
52+
c.JSON(http.StatusOK, tokens)
53+
}
54+
55+
// Get Subscription Token by ID
56+
func GetSubscriptionToken(c *gin.Context) {
57+
idStr := c.Param("id")
58+
59+
// Convert string to UUID
60+
id, err := uuid.Parse(idStr)
61+
if err != nil {
62+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid UUID format", "info": err.Error()})
63+
return
64+
}
65+
66+
var token models.SubscriptionToken
67+
DB := dbconfig.GetDb()
68+
69+
// Use UUID in the query
70+
if err := DB.First(&token, "id = ?", id).Error; err != nil {
71+
c.JSON(http.StatusNotFound, gin.H{"error": "Token not found", "info": err.Error()})
72+
return
73+
}
74+
75+
c.JSON(http.StatusOK, token)
76+
}
77+
78+
// Update Subscription Token
79+
func UpdateSubscriptionToken(c *gin.Context) {
80+
idStr := c.Param("id")
81+
var token models.SubscriptionToken
82+
83+
// Convert string to UUID
84+
id, err := uuid.Parse(idStr)
85+
if err != nil {
86+
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid UUID format", "info": err.Error()})
87+
return
88+
}
89+
90+
DB := dbconfig.GetDb()
91+
92+
if err := DB.First(&token, "id = ?", id).Error; err != nil {
93+
c.JSON(http.StatusNotFound, gin.H{"error": "Token not found", "info": err.Error()})
94+
return
95+
}
96+
97+
if err := c.ShouldBindJSON(&token); err != nil {
98+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "info": err.Error()})
99+
return
100+
}
101+
102+
tx := DB.Save(&token)
103+
if tx.Error != nil {
104+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update token", "info": tx.Error.Error()})
105+
return
106+
}
107+
c.JSON(http.StatusOK, token)
108+
}
109+
110+
// Delete Subscription Token
111+
func DeleteSubscriptionToken(c *gin.Context) {
112+
id := c.Param("id")
113+
var token models.SubscriptionToken
114+
115+
DB := dbconfig.GetDb()
116+
117+
if err := DB.First(&token, "id = ?", id).Error; err != nil {
118+
c.JSON(http.StatusNotFound, gin.H{"error": "Token not found", "info": err.Error()})
119+
return
120+
}
121+
122+
tx := DB.Delete(&token)
123+
if tx.Error != nil {
124+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete token", "info": tx.Error.Error()})
125+
return
126+
}
127+
c.JSON(http.StatusOK, gin.H{"message": "Token deleted successfully"})
128+
}

main.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ func main() {
3737
gin.SetMode(gin.ReleaseMode)
3838

3939
}
40-
gin.SetMode(gin.DebugMode)
41-
dbconfig.DbInit()
4240

4341
// cors middleware
4442
config := cors.DefaultConfig()

models/subscription.nft.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
"gorm.io/gorm"
8+
)
9+
10+
type SubscriptionNFT struct {
11+
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey" json:"id"`
12+
Blockchain string `gorm:"type:varchar(50);not null" json:"blockchain"`
13+
Name string `gorm:"type:varchar(100);not null" json:"name"`
14+
Symbol string `gorm:"type:varchar(50);not null" json:"symbol"`
15+
ImageURI string `gorm:"type:text;not null" json:"imageUri"`
16+
ContractAddress string `gorm:"type:varchar(100);not null" json:"contractAddress"`
17+
CreatedAt time.Time `json:"createdAt"`
18+
UpdatedAt time.Time `json:"updatedAt"`
19+
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
20+
}

models/subscription.token.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package models
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
"gorm.io/gorm"
8+
)
9+
10+
type SubscriptionToken struct {
11+
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primaryKey" json:"id"`
12+
Blockchain string `gorm:"type:varchar(50);not null" json:"blockchain"`
13+
Name string `gorm:"type:varchar(100);not null" json:"name"`
14+
Symbol string `gorm:"type:varchar(50);not null" json:"symbol"`
15+
ImageURI string `gorm:"type:text;not null" json:"imageUri"`
16+
ContractAddress string `gorm:"type:varchar(100);not null" json:"contractAddress"`
17+
Quantity int `gorm:"not null" json:"quantity"`
18+
CreatedAt time.Time `json:"createdAt"`
19+
UpdatedAt time.Time `json:"updatedAt"`
20+
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
21+
}

0 commit comments

Comments
 (0)