Skip to content

Commit

Permalink
create phygital multiple images and sizes add in api
Browse files Browse the repository at this point in the history
  • Loading branch information
auspicious123 committed Dec 7, 2024
1 parent fe6c397 commit b8456da
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 37 deletions.
59 changes: 37 additions & 22 deletions controllers/phygital/phygital_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"app.myriadflow.com/db"
"app.myriadflow.com/models"
"gorm.io/datatypes"

"github.com/gin-gonic/gin"
)
Expand All @@ -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
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions controllers/phygital/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// }
}
10 changes: 1 addition & 9 deletions models/phygital.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
29 changes: 29 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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
}
Expand Down

0 comments on commit b8456da

Please sign in to comment.