From b8456da9f24f6e6feaea82d63792554963c19453 Mon Sep 17 00:00:00 2001 From: auspicious123 Date: Sat, 7 Dec 2024 20:41:38 +0530 Subject: [PATCH] create phygital multiple images and sizes add in api --- controllers/phygital/phygital_controller.go | 59 +++++++++++++-------- controllers/phygital/type.go | 7 +-- models/phygital.go | 10 +--- server/server.go | 29 ++++++++++ 4 files changed, 68 insertions(+), 37 deletions(-) diff --git a/controllers/phygital/phygital_controller.go b/controllers/phygital/phygital_controller.go index 2ec5baa..0fb06a9 100644 --- a/controllers/phygital/phygital_controller.go +++ b/controllers/phygital/phygital_controller.go @@ -6,6 +6,7 @@ import ( "app.myriadflow.com/db" "app.myriadflow.com/models" + "gorm.io/datatypes" "github.com/gin-gonic/gin" ) @@ -14,6 +15,7 @@ func CreatePhygital(c *gin.Context) { var phygital models.Phygital var reqPhygital RequestPhygital + if err := c.ShouldBindJSON(&reqPhygital); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return @@ -25,35 +27,48 @@ func CreatePhygital(c *gin.Context) { } if reqPhygital.SizeOption != 0 && reqPhygital.SizeOption != 1 && reqPhygital.SizeOption != 2 { - c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid size option"}) + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid size option. Must be 0, 1, or 2."}) return } - 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 reqPhygital.SizeOption == 2 { - if len(phygital.SizeDetails) == 0 { - c.JSON(http.StatusBadRequest, gin.H{"error": "Size details are required for 'multiple_sizes' option"}) + if reqPhygital.SizeOption == 0 { + phygital.SizeDetails = datatypes.JSON([]byte("{}")) // Store empty JSON for `SizeOption == 0` + } else { + if len(reqPhygital.SizeDetails) == 0 { + c.JSON(http.StatusBadRequest, gin.H{"error": "SizeDetails is required for SizeOption 1 or 2."}) 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"}) - // return - // } - // if sizeDetail.Quantity <= 0 { - // c.JSON(http.StatusBadRequest, gin.H{"error": "Quantity must be greater than 0"}) - // return - // } - // } + phygital.SizeDetails = reqPhygital.SizeDetails // Store incoming JSON object } + + phygital.Name = reqPhygital.Name + phygital.BrandName = reqPhygital.BrandName + phygital.Category = reqPhygital.Category + phygital.Tags = reqPhygital.Tags + phygital.Description = reqPhygital.Description + phygital.Price = &reqPhygital.Price + phygital.Quantity = reqPhygital.Quantity + phygital.Royality = reqPhygital.Royality + phygital.Images = reqPhygital.Images + phygital.ProductInfo = reqPhygital.ProductInfo + phygital.ProductUrl = reqPhygital.ProductUrl + phygital.Color = reqPhygital.Color + phygital.SizeOption = reqPhygital.SizeOption + phygital.Weight = reqPhygital.Weight + phygital.Material = reqPhygital.Material + phygital.Usage = reqPhygital.Usage + phygital.Quality = reqPhygital.Quality + phygital.Manufacturer = reqPhygital.Manufacturer + phygital.OriginCountry = reqPhygital.OriginCountry + phygital.MetadataURI = reqPhygital.MetadataURI + phygital.DeployerAddress = reqPhygital.DeployerAddress + phygital.ContractAddress = reqPhygital.ContractAddress + phygital.GraphURL = reqPhygital.GraphURL + phygital.ElevateRegion = reqPhygital.ElevateRegion + phygital.CollectionID = reqPhygital.CollectionID + phygital.ChaintypeID = reqPhygital.ChaintypeID + if err := db.DB.Create(&phygital).Error; err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return diff --git a/controllers/phygital/type.go b/controllers/phygital/type.go index 633c5dd..74eed97 100644 --- a/controllers/phygital/type.go +++ b/controllers/phygital/type.go @@ -39,9 +39,4 @@ type RequestPhygital struct { ChaintypeID uuid.UUID `gorm:"type:uuid" json:"chaintype_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"` -} - -// func (p *RequestPhygital) BeforeCreate(tx *gorm.DB) (err error) { -// p.ID = uuid.New() -// return -// } +} \ No newline at end of file diff --git a/models/phygital.go b/models/phygital.go index e30722d..5ec93b0 100644 --- a/models/phygital.go +++ b/models/phygital.go @@ -8,12 +8,6 @@ import ( "gorm.io/gorm" ) -// 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"` @@ -24,13 +18,11 @@ type Phygital struct { 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"` - // 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"` diff --git a/server/server.go b/server/server.go index 4bc6d54..d3d74fd 100644 --- a/server/server.go +++ b/server/server.go @@ -1,10 +1,12 @@ package server import ( + "net/http" "os" "app.myriadflow.com/controllers" phygital_controllers "app.myriadflow.com/controllers/phygital" + "app.myriadflow.com/db" "app.myriadflow.com/middleware" "github.com/gin-gonic/gin" ) @@ -26,6 +28,33 @@ func Router() { "message": "pong", }) }) + + router.GET("/ping_database", func(c *gin.Context) { + DB := db.Connect() + + // Retrieve the underlying SQL database object from GORM + sqlDB, err := DB.DB() + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Failed to retrieve database object: " + err.Error(), + }) + return + } + + // Ping the database to check if the connection is active + if err := sqlDB.Ping(); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{ + "error": "Database connection error: " + err.Error(), + }) + return + } + + // If the ping is successful + c.JSON(http.StatusOK, gin.H{ + "message": "Database connection successful!", + }) + }) + Routes(router) router.Run(":" + os.Getenv("APP_PORT")) // listen and serve on 0.0.0.0:808 }