Skip to content

Commit

Permalink
mofying all update functionallity
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shubh committed Jun 27, 2024
1 parent 8a70883 commit 1874ae2
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 365 deletions.
102 changes: 4 additions & 98 deletions controllers/collection_controller.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package controllers

import (
"encoding/json"
"fmt"
"net/http"
"time"

"app.myriadflow.com/db"
"app.myriadflow.com/models"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)

func CreateCollection(c *gin.Context) {
Expand All @@ -29,115 +25,25 @@ func CreateCollection(c *gin.Context) {

func GetCollection(c *gin.Context) {
id := c.Param("id")
var collection Collection
var collection models.Collection
if err := db.DB.First(&collection, "id = ?", id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Collection not found"})
return
}
var categoryMap map[string]interface{}
if len(collection.Category) > 0 {
err := json.Unmarshal(collection.Category, &categoryMap)
if err != nil {
c.JSON(http.StatusInternalServerError, fmt.Errorf("error unmarshalling category for collection ID %s: %v", collection.ID, err))
// return nil, fmt.Errorf("error unmarshalling category for collection ID %s: %v", collection.ID, err)
}
}
newCollections := models.Collection{
ID: collection.ID,
Name: collection.Name,
Description: collection.Description,
LogoImage: collection.LogoImage,
CoverImage: collection.CoverImage,
Category: categoryMap,
Tags: collection.Tags,
Status: collection.Status,
BrandID: collection.BrandID,
CreatedAt: collection.CreatedAt,
UpdatedAt: collection.UpdatedAt,
}

c.JSON(http.StatusOK, newCollections)
}

type Collection struct {
ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"`
Name string `json:"name"`
Description string `json:"description"`
LogoImage string `json:"logo_image"`
CoverImage string `json:"cover_image"`
Category []byte `gorm:"type:jsonb" json:"category"`
Tags string `json:"tags"`
Status int `json:"status"`
BrandID string `json:"brand_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"`
c.JSON(http.StatusOK, collection)
}

// get all connection api
func GetAllCollections(c *gin.Context) {
var collections []Collection
var collections []models.Collection
if err := db.DB.Find(&collections).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

// // Convert Category []byte to map[string]interface{}
// for i := range collections {
// var categoryMap map[string]interface{}
// if err := json.Unmarshal(collections[i].Category, &categoryMap); err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to unmarshal category"})
// return
// }
// collections[i].Category = categoryMap
// }
data, errors := convertCollections(collections)
if errors != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": errors})
return
}

c.JSON(http.StatusOK, data)
}

func convertCollections(oldCollections []Collection) ([]models.Collection, error) {
newCollections := make([]models.Collection, len(oldCollections))

for i, oldCollection := range oldCollections {
var categoryMap map[string]interface{}
if len(oldCollection.Category) > 0 {
err := json.Unmarshal(oldCollection.Category, &categoryMap)
if err != nil {
return nil, fmt.Errorf("error unmarshalling category for collection ID %s: %v", oldCollection.ID, err)
}
}

newCollections[i] = models.Collection{
ID: oldCollection.ID,
Name: oldCollection.Name,
Description: oldCollection.Description,
LogoImage: oldCollection.LogoImage,
CoverImage: oldCollection.CoverImage,
Category: categoryMap,
Tags: oldCollection.Tags,
Status: oldCollection.Status,
BrandID: oldCollection.BrandID,
CreatedAt: oldCollection.CreatedAt,
UpdatedAt: oldCollection.UpdatedAt,
}
}

return newCollections, nil
c.JSON(http.StatusOK, collections)
}

// func GetAllCollections(c *gin.Context) {
// var collections []Collection
// if err := db.DB.Raw("SELECT * FROM collections").Scan(&collections).Error; err != nil {
// c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
// return
// }
// c.JSON(http.StatusOK, collections)
// }

func UpdateCollection(c *gin.Context) {
id := c.Param("id")
var collection models.Collection
Expand Down
145 changes: 19 additions & 126 deletions controllers/phygital_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package controllers

import (
"encoding/json"
"fmt"
"net/http"
"time"

"app.myriadflow.com/db"
"app.myriadflow.com/models"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
)

func CreatePhygital(c *gin.Context) {
Expand All @@ -31,84 +28,52 @@ func CreatePhygital(c *gin.Context) {

func GetPhygital(c *gin.Context) {
id := c.Param("id")
var oldPhygital Phygital
var oldPhygital models.Phygital
if err := db.DB.First(&oldPhygital, "id = ?", id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Phygital not found"})
return
}

var categoryMap map[string]interface{}
if len(oldPhygital.Category) > 0 {
err := json.Unmarshal(oldPhygital.Category, &categoryMap)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf("error unmarshalling category for Phygital ID %s: %v", oldPhygital.ID, err)})
}
}
newPhygitals := models.Phygital{
ID: oldPhygital.ID,
Name: oldPhygital.Name,
BrandName: oldPhygital.BrandName,
Category: categoryMap,
Description: oldPhygital.Description,
Price: oldPhygital.Price,
Quantity: oldPhygital.Quantity,
Royality: oldPhygital.Royality,
Image: oldPhygital.Image,
ProductInfo: oldPhygital.ProductInfo,
Color: oldPhygital.Color,
Size: oldPhygital.Size,
Weight: oldPhygital.Weight,
Material: oldPhygital.Material,
Usage: oldPhygital.Usage,
Quality: oldPhygital.Quality,
Manufacturer: oldPhygital.Manufacturer,
OriginCountry: oldPhygital.OriginCountry,
MetadataURI: oldPhygital.MetadataURI,
DeployerAddress: oldPhygital.DeployerAddress,
ContractAddress: oldPhygital.ContractAddress,
GraphURL: oldPhygital.GraphURL,
CollectionID: oldPhygital.CollectionID,
CreatedAt: oldPhygital.CreatedAt,
UpdatedAt: oldPhygital.UpdatedAt,
}

c.JSON(http.StatusOK, newPhygitals)
c.JSON(http.StatusOK, oldPhygital)
}

// get all phygital api
func GetAllPhygital(c *gin.Context) {
var phygitals []Phygital
var phygitals []models.Phygital
if err := db.DB.Find(&phygitals).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
data, errr := convertPhygitals(phygitals)
if errr != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": errr.Error()})
return
}
c.JSON(http.StatusOK, data)
c.JSON(http.StatusOK, phygitals)
}

func UpdatePhygital(c *gin.Context) {
id := c.Param("id")
var phygital models.Phygital
if err := db.DB.First(&phygital, "id = ?", id).Error; err != nil {
var Phygital models.Phygital

if err := db.DB.First(&Phygital, "id = ?", id).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Phygital not found"})
return
}

if err := c.ShouldBindJSON(&phygital); err != nil {
var input models.Phygital
if err := c.ShouldBindJSON(&input); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

if err := db.DB.Save(&phygital).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
var err1 error
// Ensure the input Category is properly converted to JSON
input.Category, err1 = json.Marshal(input.Category)
if err1 != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err1.Error()})
return
}

c.JSON(http.StatusOK, phygital)
// Update fields
db.DB.Model(&Phygital).Updates(input)

c.JSON(http.StatusOK, Phygital)

}

func DeletePhygital(c *gin.Context) {
Expand All @@ -120,75 +85,3 @@ func DeletePhygital(c *gin.Context) {

c.JSON(http.StatusOK, gin.H{"message": "Phygital deleted successfully"})
}

func convertPhygitals(oldPhygitals []Phygital) ([]models.Phygital, error) {
newPhygitals := make([]models.Phygital, len(oldPhygitals))

for i, oldPhygital := range oldPhygitals {
var categoryMap map[string]interface{}
if len(oldPhygital.Category) > 0 {
err := json.Unmarshal(oldPhygital.Category, &categoryMap)
if err != nil {
return nil, fmt.Errorf("error unmarshalling category for Phygital ID %s: %v", oldPhygital.ID, err)
}
}

newPhygitals[i] = models.Phygital{
ID: oldPhygital.ID,
Name: oldPhygital.Name,
BrandName: oldPhygital.BrandName,
Category: categoryMap,
Description: oldPhygital.Description,
Price: oldPhygital.Price,
Quantity: oldPhygital.Quantity,
Royality: oldPhygital.Royality,
Image: oldPhygital.Image,
ProductInfo: oldPhygital.ProductInfo,
Color: oldPhygital.Color,
Size: oldPhygital.Size,
Weight: oldPhygital.Weight,
Material: oldPhygital.Material,
Usage: oldPhygital.Usage,
Quality: oldPhygital.Quality,
Manufacturer: oldPhygital.Manufacturer,
OriginCountry: oldPhygital.OriginCountry,
MetadataURI: oldPhygital.MetadataURI,
DeployerAddress: oldPhygital.DeployerAddress,
ContractAddress: oldPhygital.ContractAddress,
GraphURL: oldPhygital.GraphURL,
CollectionID: oldPhygital.CollectionID,
CreatedAt: oldPhygital.CreatedAt,
UpdatedAt: oldPhygital.UpdatedAt,
}
}

return newPhygitals, nil
}

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 []byte `gorm:"type:jsonb" json:"category"`
Description string `json:"description"`
Price int `json:"price"`
Quantity int `json:"quantity"`
Royality int `json:"royality"`
Image string `json:"image"`
ProductInfo string `json:"product_info"`
Color string `json:"color"`
Size string `json:"size"`
Weight int `json:"weight"`
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"`
CollectionID uuid.UUID `json:"collection_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"`
}
Loading

0 comments on commit 1874ae2

Please sign in to comment.