Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
auspicious123 committed Dec 7, 2024
1 parent 2016310 commit cf802f3
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
16 changes: 16 additions & 0 deletions controllers/brand_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package phygital_controllers

import (
"encoding/json"
Expand All @@ -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
Expand Down
48 changes: 48 additions & 0 deletions controllers/phygital/type.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 10 additions & 1 deletion models/brand.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
14 changes: 12 additions & 2 deletions models/phygital.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down

0 comments on commit cf802f3

Please sign in to comment.