diff --git a/controllers/profile_controller.go b/controllers/profile_controller.go index 7eb9872..e8122d7 100644 --- a/controllers/profile_controller.go +++ b/controllers/profile_controller.go @@ -1,11 +1,13 @@ package controllers import ( + "net/http" + "app.myriadflow.com/db" "app.myriadflow.com/models" - "net/http" "github.com/gin-gonic/gin" + "github.com/google/uuid" ) // CreateProfile creates a new profile @@ -141,3 +143,70 @@ func GetProfileByUsername(c *gin.Context) { c.JSON(http.StatusOK, profile) } + +func SaveAddresses(c *gin.Context) { + var addresses []models.Address + if err := c.ShouldBindJSON(&addresses); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + profileID := c.Param("profile_id") // Get profile ID from URL parameter + for i := range addresses { + addresses[i].ProfileID, _ = uuid.Parse(profileID) + } + + if err := db.DB.Create(&addresses).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, addresses) +} + +func GetAddresses(c *gin.Context) { + profileID := c.Param("profile_id") + var addresses []models.Address + if err := db.DB.Where("profile_id = ?", profileID).Find(&addresses).Error; err != nil { + c.JSON(http.StatusNotFound, gin.H{"error": "Addresses not found"}) + return + } + + c.JSON(http.StatusOK, addresses) +} + +func UpdateAddress(c *gin.Context) { + profileID := c.Param("profile_id") + var addresses []models.Address + + if err := c.ShouldBindJSON(&addresses); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + for _, address := range addresses { + profileUUID, err := uuid.Parse(profileID) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid profile ID format"}) + return + } + address.ProfileID = profileUUID + + if err := db.DB.Save(&address).Error; err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + } + + c.JSON(http.StatusOK, addresses) +} + +func DeleteAddress(c *gin.Context) { + ID := c.Param("id") + if err := db.DB.Delete(&models.Address{}, "id = ?", ID).Error; err != nil { + c.JSON(http.StatusNotFound, gin.H{"error": "Address not found"}) + return + } + + c.JSON(http.StatusOK, gin.H{"message": "Address deleted"}) +} diff --git a/db/db.connection.go b/db/db.connection.go index f1dd35d..fd51917 100644 --- a/db/db.connection.go +++ b/db/db.connection.go @@ -34,7 +34,7 @@ func Init() { log.Println(err) } - err = DB.AutoMigrate(&models.User{}, &models.Brand{}, &models.Collection{}, &models.Phygital{}, &models.WebXR{}, &models.Avatar{}, &models.Variant{}, &models.FanToken{}, &models.ChainType{}, &models.NftEntries{}, &models.Profile{}, &models.CartItem{}, &models.OTPStore{}, &models.OTPData{}, &models.MainnetFanToken{}, &models.DelegateMintFanTokenRequest{} , &models.Elevate{}) + err = DB.AutoMigrate(&models.User{}, &models.Brand{}, &models.Collection{}, &models.Phygital{}, &models.WebXR{}, &models.Avatar{}, &models.Variant{}, &models.FanToken{}, &models.ChainType{}, &models.NftEntries{}, &models.Profile{}, &models.CartItem{}, &models.OTPStore{}, &models.OTPData{}, &models.MainnetFanToken{}, &models.DelegateMintFanTokenRequest{} , &models.Elevate{} , &models.Address{}) if err != nil { log.Fatalf("failed to migrate database: %v", err) } diff --git a/models/profile.go b/models/profile.go index 6f9a3d2..e1c75e2 100644 --- a/models/profile.go +++ b/models/profile.go @@ -7,22 +7,36 @@ import ( "gorm.io/gorm" ) +type Address struct { + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"` + FullName string `json:"full_name"` + StreetAddress string `json:"street_address"` + StreetAddress2 string `json:"street_address_2"` + City string `json:"city"` + Pincode string `json:"pincode"` + Country string `json:"country"` + ProfileID uuid.UUID `gorm:"type:uuid" json:"profile_id"` // Foreign key +} + type Profile struct { - ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"` - Name string `json:"name"` - Email string `json:"email"` - WalletAddress string `json:"wallet_address"` - CoverImage string `json:"cover_image"` - ProfileImage string `json:"profile_image"` - Username string `json:"username"` - Bio string `json:"bio"` - Website string `json:"website"` - X string `json:"x"` - Instagram string `json:"instagram"` - Basename string `json:"basename"` - 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"` + ID uuid.UUID `gorm:"type:uuid;default:uuid_generate_v4();primary_key" json:"id"` + Name string `json:"name"` + Email string `json:"email"` + WalletAddress string `json:"wallet_address"` + CoverImage string `json:"cover_image"` + ProfileImage string `json:"profile_image"` + Username string `json:"username"` + Bio string `json:"bio"` + Website string `json:"website"` + X string `json:"x"` + Instagram string `json:"instagram"` + Basename string `json:"basename"` + 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"` + Addresses []Address `gorm:"foreignKey:ProfileID"` + SelectedSocialLink string `json:"selected_social_link"` + Link string `json:"link"` } func (p *Profile) BeforeCreate(tx *gorm.DB) (err error) { diff --git a/server/server.go b/server/server.go index 84cf901..83e5978 100644 --- a/server/server.go +++ b/server/server.go @@ -125,6 +125,8 @@ func Routes(r *gin.Engine) { //Profile routes r.POST("/profiles", controllers.CreateProfile) + r.POST("/profiles/addresses/:profile_id" , controllers.SaveAddresses) + r.GET("/profiles/addresses/:profile_id" , controllers.GetAddresses) r.GET("/profiles/:id", controllers.GetProfile) r.GET("/profiles/all", controllers.GetAllProfiles) r.GET("/profiles/all/:chaintype_id", controllers.GetAllProfilesByChainType) @@ -132,8 +134,10 @@ func Routes(r *gin.Engine) { r.GET("/profiles/wallet/:walletAddress", controllers.GetProfileByWalletAddress) r.GET("/profiles/username/:username", controllers.GetProfileByUsername) r.PUT("/profiles/:walletAddress", controllers.UpdateProfile) + r.PUT("/profiles/addresses/:profile_id" , controllers.UpdateAddress) r.DELETE("/profiles/:id", controllers.DeleteProfile) r.DELETE("/profiles/walletandemail/:walletAddress/:email", controllers.DeleteProfileByWalletAndEmail) + r.DELETE("/profiles/addresses/:id" , controllers.DeleteAddress) // Cart routes r.POST("/cart", controllers.AddToCart)