diff --git a/controllers/brand_controller.go b/controllers/brand_controller.go index efc3d12..baa6a31 100644 --- a/controllers/brand_controller.go +++ b/controllers/brand_controller.go @@ -33,6 +33,22 @@ func GetBrand(c *gin.Context) { c.JSON(http.StatusOK, brand) } +func GetBrandsByUserID(c *gin.Context) { + userID := c.Param("userID") + var brands []models.Brand + + if err := db.DB.Where("user_id = ?", userID).Find(&brands).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + if len(brands) == 0 { + c.JSON(http.StatusNotFound, gin.H{"message": "No brands found for the user"}) + return + } + + c.JSON(http.StatusOK, brands) +} func GetBrandByName(c *gin.Context) { name := c.Param("name") diff --git a/controllers/phygital_controller.go b/controllers/phygital/phygital_controller.go similarity index 69% rename from controllers/phygital_controller.go rename to controllers/phygital/phygital_controller.go index db8f61a..2a332e8 100644 --- a/controllers/phygital_controller.go +++ b/controllers/phygital/phygital_controller.go @@ -1,4 +1,4 @@ -package controllers +package phygital_controllers import ( "encoding/json" @@ -13,11 +13,47 @@ import ( func CreatePhygital(c *gin.Context) { var phygital models.Phygital - if err := c.ShouldBindJSON(&phygital); err != nil { + var reqPhygital RequestPhygital + if err := c.ShouldBindJSON(&reqPhygital); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } + if len(phygital.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" { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid size option"}) + return + } + + if phygital.SizeOption == "one_size_with_measurements" { + + 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" { + 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"}) + // return + // } + // if sizeDetail.Quantity <= 0 { + // c.JSON(http.StatusBadRequest, gin.H{"error": "Quantity must be greater than 0"}) + // return + // } + // } + } + 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 new file mode 100644 index 0000000..f3baaf9 --- /dev/null +++ b/controllers/phygital/type.go @@ -0,0 +1,48 @@ +package phygital_controllers + +import ( + "time" + + "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"` + // 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"` + SizeDetails datatypes.JSON `gorm:"type:jsonb" json:"size_details"` + Weight float64 `json:"weight" gorm:"type:decimal(20,10)"` + Material string `json:"material"` + Usage string `json:"usage"` + Quality string `json:"quality"` + Manufacturer string `json:"manufacturer"` + OriginCountry string `json:"origin_country"` + MetadataURI string `json:"metadata_uri"` + DeployerAddress string `json:"deployer_address"` + ContractAddress string `json:"contract_address"` + GraphURL string `json:"graph_url"` + ElevateRegion string `json:"elevate_region"` + CollectionID uuid.UUID `json:"collection_id"` + 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 *Phygital) BeforeCreate(tx *gorm.DB) (err error) { + p.ID = uuid.New() + return +} diff --git a/models/brand.go b/models/brand.go index cbdb939..2880038 100644 --- a/models/brand.go +++ b/models/brand.go @@ -7,6 +7,7 @@ import ( "gorm.io/gorm" ) + type Brand struct { ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"` Name string `json:"name"` @@ -22,9 +23,17 @@ type Brand struct { Twitter string `json:"twitter"` Instagram string `json:"instagram"` Facebook string `json:"facebook"` + Telegram string `json:"telegram"` + LinkedIn string `json:"linkedin"` + Youtube string `json:"youtube"` + Discord string `json:"discord"` + Whatsapp string `json:"whatsapp"` + Google string `json:"google"` + Tiktok string `json:"tiktok"` + Snapchat string `json:"snapchat"` + Pinetrest string `json:"pinetrest"` AdditionalLink string `json:"additional_link"` Link string `json:"link"` - Discord string `json:"discord"` AdditionalInfo string `json:"additional_info"` Industry string `json:"industry"` Tags string `json:"tags"` diff --git a/models/phygital.go b/models/phygital.go index e7d385b..9b1779c 100644 --- a/models/phygital.go +++ b/models/phygital.go @@ -8,6 +8,13 @@ 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"` @@ -18,11 +25,14 @@ type Phygital struct { Price *float64 `json:"price" gorm:"type:decimal(20,10);"` Quantity int `json:"quantity"` Royality int `json:"royality"` - Image string `json:"image"` + // 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"` + // Size string `json:"size"` + 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"` Usage string `json:"usage"`