Skip to content

Commit

Permalink
feat : added new table fantoken
Browse files Browse the repository at this point in the history
  • Loading branch information
surajhub255 committed Jul 17, 2024
1 parent 742288b commit 3113077
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
75 changes: 75 additions & 0 deletions controllers/fantoken_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package controllers

import(
"net/http"

"app.myriadflow.com/db"
"app.myriadflow.com/models"
"github.com/gin-gonic/gin"
)

func CreateFanToken(c *gin.Context) {
var fantoken models.FanToken
if err := c.ShouldBindJSON(&fantoken); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if err := db.DB.Create(&fantoken).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, fantoken)
}

func GetFanToken(c *gin.Context) {
id := c.Param("id")
var fantoken models.FanToken
if err := db.DB.First(&fantoken, "id = ?", id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "FanToken not found"})
return
}

c.JSON(http.StatusOK, fantoken)
}

func GetAllFanToken(c *gin.Context) {
var fantoken []models.FanToken
if err := db.DB.Find(&fantoken).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, fantoken)
}

func UpdateFanToken(c *gin.Context) {
id := c.Param("id")
var fantoken models.FanToken
if err := db.DB.First(&fantoken, "id = ?", id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "FanToken not found"})
return
}

if err := c.ShouldBindJSON(&fantoken); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if err := db.DB.Save(&fantoken).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, fantoken)
}

func DeleteFanToken(c *gin.Context) {
id := c.Param("id")
if err := db.DB.Delete(&models.FanToken{}, "id = ?", id).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

c.JSON(http.StatusOK, gin.H{"message": "FanToken deleted successfully"})
}
2 changes: 1 addition & 1 deletion db/db.connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Init() {

InitMigration(DB)

err = DB.Debug().AutoMigrate(&models.User{}, &models.Brand{}, &models.Collection{}, &models.Phygital{}, &models.WebXR{}, &models.Avatar{}, &models.Variant{})
err = DB.Debug().AutoMigrate(&models.User{}, &models.Brand{}, &models.Collection{}, &models.Phygital{}, &models.WebXR{}, &models.Avatar{}, &models.Variant{} , &models.FanToken{})
if err != nil {
log.Fatalf("failed to migrate database: %v", err)
}
Expand Down
17 changes: 17 additions & 0 deletions models/fantoken.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

import(
"time"
"github.com/google/uuid"
)

type FanToken struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"`
BrandID string `json:"brand_id"`
CollectionID uuid.UUID `json:"collection_id"`
PhygitalID string `json:"phygital_id"`
TokenFestID string `json:"tokenFest_id"`
CreatedAt time.Time `gorm:"type:timestamp;default:current_timestamp" json:"created_at"`
UpdatedAt time.Time `gorm:"type:timestamp;default:current_timestamp" json:"updated_at"`
}

7 changes: 7 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,11 @@ func Routes(r *gin.Engine) {
r.GET("/variants/all", controllers.GetAllVariant)
r.PUT("/variants/:id", controllers.UpdateVariant)
r.DELETE("/variants/:id", controllers.DeleteVariant)

//TokenFest routes
r.POST("/fantoken", controllers.CreateFanToken)
r.GET("/fantoken/:id" , controllers.GetFanToken)
r.GET("fantoken/all" , controllers.GetAllFanToken)
r.PUT("fantoken/:id" , controllers.UpdateFanToken)
r.DELETE("fantoken/:id" , controllers.DeleteFanToken)
}

0 comments on commit 3113077

Please sign in to comment.