Skip to content

Commit

Permalink
`Refactor phygital controller and model to use int instead of string …
Browse files Browse the repository at this point in the history
…for size option`
  • Loading branch information
p-shubh committed Dec 7, 2024
1 parent cf802f3 commit fe6c397
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 53 deletions.
14 changes: 7 additions & 7 deletions controllers/phygital/phygital_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@ func CreatePhygital(c *gin.Context) {
return
}

if len(phygital.Images) == 0 {
if len(reqPhygital.Images) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "At least one image is required"})
return
}
if phygital.SizeOption != "one_size_only" && phygital.SizeOption != "one_size_with_measurements" && phygital.SizeOption != "multiple_sizes" {

if reqPhygital.SizeOption != 0 && reqPhygital.SizeOption != 1 && reqPhygital.SizeOption != 2 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid size option"})
return
}

if phygital.SizeOption == "one_size_with_measurements" {
if reqPhygital.SizeOption == 1 {

var sizeDetails map[string]interface{}
if err := json.Unmarshal(phygital.SizeDetails, &sizeDetails); err != nil || len(sizeDetails) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Size details are required for 'one_size_with_measurements' option"})
return
}
} else if phygital.SizeOption == "multiple_sizes" {
} else if reqPhygital.SizeOption == 2 {
if len(phygital.SizeDetails) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "Size details are required for 'multiple_sizes' option"})
return
}


// for _, sizeDetail := range phygital.SizeDetails {
// if sizeDetail.Size == "" {
// c.JSON(http.StatusBadRequest, gin.H{"error": "Size is required for each entry in size_details"})
Expand Down Expand Up @@ -97,7 +97,7 @@ func GetPhygitalByWalletAddress(c *gin.Context) {
func GetAllPhygitalByChainType(c *gin.Context) {
chaintypeId := c.Param("chaintype_id")
var phygitals []models.Phygital
if err := db.DB.Where("chaintype_id = ? " , chaintypeId).Find(&phygitals).Error; err != nil {
if err := db.DB.Where("chaintype_id = ? ", chaintypeId).Find(&phygitals).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
Expand Down
35 changes: 17 additions & 18 deletions controllers/phygital/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ import (

"github.com/google/uuid"
"gorm.io/datatypes"
"gorm.io/gorm"
)

type RequestPhygital struct {
Name string `json:"name"`
BrandName string `json:"brand_name"`
Category datatypes.JSON ` json:"category"`
Tags datatypes.JSON ` json:"tags"`
Description string `json:"description"`
Price float64 `json:"price" gorm:"type:decimal(20,10);"`
Quantity int `json:"quantity"`
Royality int `json:"royality"`
Name string `json:"name"`
BrandName string `json:"brand_name"`
Category datatypes.JSON ` json:"category"`
Tags datatypes.JSON ` json:"tags"`
Description string `json:"description"`
Price float64 `json:"price" gorm:"type:decimal(20,10);"`
Quantity int `json:"quantity"`
Royality int `json:"royality"`
// Image string `json:"image"`
Images datatypes.JSON `gorm:"type:jsonb" json:"images"`
ProductInfo string `json:"product_info"`
ProductUrl string `json:"product_url"`
Color string `json:"color"`
Images datatypes.JSON `gorm:"type:jsonb" json:"images"`
ProductInfo string `json:"product_info"`
ProductUrl string `json:"product_url"`
Color string `json:"color"`
// Size string `json:"size"`
SizeOption string `json:"size_option"`
SizeOption int `json:"size_option"`
SizeDetails datatypes.JSON `gorm:"type:jsonb" json:"size_details"`
Weight float64 `json:"weight" gorm:"type:decimal(20,10)"`
Material string `json:"material"`
Expand All @@ -42,7 +41,7 @@ type RequestPhygital struct {
UpdatedAt time.Time `gorm:"type:timestamp;default:current_timestamp" json:"updated_at"`
}

func (p *Phygital) BeforeCreate(tx *gorm.DB) (err error) {
p.ID = uuid.New()
return
}
// func (p *RequestPhygital) BeforeCreate(tx *gorm.DB) (err error) {
// p.ID = uuid.New()
// return
// }
39 changes: 19 additions & 20 deletions models/phygital.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@ import (
"gorm.io/gorm"
)


type SizeDetail struct {
Size string `json:"size"`
Quantity int `json:"quantity"`
AdditionalDetails string `json:"additional_details"`
}
// type SizeDetail struct {
// Size string `json:"size"`
// Quantity int `json:"quantity"`
// AdditionalDetails string `json:"additional_details"`
// }

type Phygital struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"`
Name string `json:"name"`
BrandName string `json:"brand_name"`
Category datatypes.JSON `gorm:"type:jsonb" json:"category"`
Tags datatypes.JSON `gorm:"type:jsonb" json:"tags"`
Description string `json:"description"`
Price *float64 `json:"price" gorm:"type:decimal(20,10);"`
Quantity int `json:"quantity"`
Royality int `json:"royality"`
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"`
Name string `json:"name"`
BrandName string `json:"brand_name"`
Category datatypes.JSON `gorm:"type:jsonb" json:"category"`
Tags datatypes.JSON `gorm:"type:jsonb" json:"tags"`
Description string `json:"description"`
Price *float64 `json:"price" gorm:"type:decimal(20,10);"`
Quantity int `json:"quantity"`
Royality int `json:"royality"`
// Image string `json:"image"`
Images datatypes.JSON `gorm:"type:jsonb" json:"images"`
ProductInfo string `json:"product_info"`
ProductUrl string `json:"product_url"`
Color string `json:"color"`
Images datatypes.JSON `gorm:"type:jsonb" json:"images"`
ProductInfo string `json:"product_info"`
ProductUrl string `json:"product_url"`
Color string `json:"color"`
// Size string `json:"size"`
SizeOption string `json:"size_option"`
SizeOption string `json:"size_option"`
SizeDetails datatypes.JSON `gorm:"type:jsonb" json:"size_details"`
Weight float64 `json:"weight" gorm:"type:decimal(20,10)"`
Material string `json:"material"`
Expand Down
17 changes: 9 additions & 8 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"os"

"app.myriadflow.com/controllers"
phygital_controllers "app.myriadflow.com/controllers/phygital"
"app.myriadflow.com/middleware"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -61,14 +62,14 @@ func Routes(r *gin.Engine) {
r.GET("/collections/region", controllers.GetAllCollectionByRegion)

// Phygital routes
r.POST("/phygitals", controllers.CreatePhygital)
r.GET("/phygitals/:id", controllers.GetPhygital)
r.GET("/phygitals/deployer_address/:deployer_address", controllers.GetPhygitalByWalletAddress)
r.GET("/phygitals/all/:chaintype_id", controllers.GetAllPhygitalByChainType)
r.GET("/phygitals/all", controllers.GetAllPhygital)
r.PUT("/phygitals/:id", controllers.UpdatePhygital)
r.DELETE("/phygitals/:id", controllers.DeletePhygital)
r.GET("/phygitals/region", controllers.GetAllPhygitalByRegion)
r.POST("/phygitals", phygital_controllers.CreatePhygital)
r.GET("/phygitals/:id", phygital_controllers.GetPhygital)
r.GET("/phygitals/deployer_address/:deployer_address", phygital_controllers.GetPhygitalByWalletAddress)
r.GET("/phygitals/all/:chaintype_id", phygital_controllers.GetAllPhygitalByChainType)
r.GET("/phygitals/all", phygital_controllers.GetAllPhygital)
r.PUT("/phygitals/:id", phygital_controllers.UpdatePhygital)
r.DELETE("/phygitals/:id", phygital_controllers.DeletePhygital)
r.GET("/phygitals/region", phygital_controllers.GetAllPhygitalByRegion)

// WebXR routes
r.POST("/webxr", controllers.CreateWebXR)
Expand Down

0 comments on commit fe6c397

Please sign in to comment.