Skip to content

Commit

Permalink
Merge pull request #44 from NetSepio/vaibhavvvvv/dev2
Browse files Browse the repository at this point in the history
Walrus File Storage APIs added
  • Loading branch information
vaibhavvvvv authored Oct 6, 2024
2 parents c17fd07 + b91da7e commit 26ed9c2
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 3 deletions.
3 changes: 2 additions & 1 deletion api/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"github.com/NetSepio/erebrus-gateway/api/v1/client"
nodedwifi "github.com/NetSepio/erebrus-gateway/api/v1/nodeDwifi"
"github.com/NetSepio/erebrus-gateway/api/v1/nodeOperatorForm"

"github.com/NetSepio/erebrus-gateway/api/v1/nodes"
"github.com/NetSepio/erebrus-gateway/api/v1/registerDwifi"
"github.com/NetSepio/erebrus-gateway/api/v1/subscription"
"github.com/NetSepio/erebrus-gateway/api/v1/walrusFileStorage"
"github.com/gin-gonic/gin"
)

Expand All @@ -22,5 +22,6 @@ func ApplyRoutes(r *gin.RouterGroup) {
nodeOperatorForm.ApplyRoutes(v1)
registerDwifi.ApplyRoutes(v1)
nodedwifi.ApplyRoutes(v1)
walrusFileStorage.ApplyRoutes(v1)
}
}
119 changes: 119 additions & 0 deletions api/v1/walrusFileStorage/walrusFileStorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package walrusFileStorage

import (
"net/http"

"github.com/NetSepio/erebrus-gateway/config/dbconfig"
"github.com/NetSepio/erebrus-gateway/models"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

func ApplyRoutes(r *gin.RouterGroup) {
g := r.Group("/walrusFileStorage")
{
g.POST("", upsertWalrusStorage)
g.DELETE("/:address", deleteWalrusStorage)
g.DELETE("/:address/blob/:blob_id", deleteBlobID)
g.GET("/:address", getAllWalrusStorage)
}
}

func upsertWalrusStorage(c *gin.Context) {
var walrus models.WalrusStorage
if err := c.ShouldBindJSON(&walrus); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

db := dbconfig.GetDb()

var existingWalrus models.WalrusStorage
result := db.Where("wallet_address = ?", walrus.WalletAddress).First(&existingWalrus)

if result.Error == nil {
existingWalrus.FileBlobs = append(existingWalrus.FileBlobs, walrus.FileBlobs...)
if err := db.Save(&existingWalrus).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update wallet"})
return
}
c.JSON(http.StatusOK, existingWalrus)
} else if result.Error == gorm.ErrRecordNotFound {
if err := db.Create(&walrus).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create wallet"})
return
}
c.JSON(http.StatusCreated, walrus)
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
}
}

func deleteBlobID(c *gin.Context) {
walletAddress := c.Param("address")
blobID := c.Param("blob_id")
db := dbconfig.GetDb()

var walrus models.WalrusStorage
if err := db.Where("wallet_address = ?", walletAddress).First(&walrus).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Wallet not found"})
return
}

index := -1
for i, fileObj := range walrus.FileBlobs {
if fileObj.BlobID == blobID {
index = i
break
}
}

if index == -1 {
c.JSON(http.StatusNotFound, gin.H{"error": "Blob ID not found"})
return
}

walrus.FileBlobs = append(walrus.FileBlobs[:index], walrus.FileBlobs[index+1:]...)

if err := db.Save(&walrus).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update wallet"})
return
}

c.JSON(http.StatusOK, gin.H{"message": "Blob ID removed successfully"})
}

func deleteWalrusStorage(c *gin.Context) {
address := c.Param("address")
db := dbconfig.GetDb()

var walrus models.WalrusStorage
if err := db.Where("wallet_address = ?", address).First(&walrus).Error; err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Wallet not found"})
return
}

if err := db.Delete(&walrus).Error; err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete wallet"})
return
}

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

func getAllWalrusStorage(c *gin.Context) {
address := c.Param("address")
db := dbconfig.GetDb()

var walrus models.WalrusStorage
if err := db.Where("wallet_address = ?", address).First(&walrus).Error; err != nil {
if err == gorm.ErrRecordNotFound {
c.JSON(http.StatusNotFound, gin.H{"error": "Wallet not found"})
} else {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve wallet"})
}
return
}

c.JSON(http.StatusOK, gin.H{"file_blobs": walrus.FileBlobs})
}
5 changes: 3 additions & 2 deletions config/dbconfig/dbconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"fmt"
"os"

"github.com/NetSepio/erebrus-gateway/models"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"

"github.com/NetSepio/erebrus-gateway/models"

"gorm.io/driver/postgres"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func GetDb() *gorm.DB {
func DbInit() error {
db := GetDb()

if err := db.AutoMigrate(&models.User{}, &models.Erebrus{}, &models.Node{}, &models.Subscription{}, &models.FormData{}, &models.FlowId{}, &models.UserFeedback{}, &models.WifiNode{}, &models.NodeDwifi{}); err != nil {
if err := db.AutoMigrate(&models.User{}, &models.Erebrus{}, &models.Node{}, &models.Subscription{}, &models.FormData{}, &models.FlowId{}, &models.UserFeedback{}, &models.WifiNode{}, &models.NodeDwifi{}, &models.WalrusStorage{}); err != nil {
log.Fatal(err)
}
return nil
Expand Down
35 changes: 35 additions & 0 deletions models/walrusFileStorage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package models

import (
"database/sql/driver"
"encoding/json"
"fmt"

"gorm.io/gorm"
)

type WalrusStorage struct {
gorm.Model
WalletAddress string `json:"wallet_address"`
FileBlobs FileObjectArray `gorm:"type:jsonb" json:"file_blobs"`
}

type FileObject struct {
Filename string `json:"filename"`
BlobID string `json:"blob_id"`
}

type FileObjectArray []FileObject

func (a FileObjectArray) Value() (driver.Value, error) {
return json.Marshal(a)
}

func (a *FileObjectArray) Scan(value interface{}) error {
b, ok := value.([]byte)
if !ok {
return fmt.Errorf("type assertion to []byte failed")
}

return json.Unmarshal(b, &a)
}

0 comments on commit 26ed9c2

Please sign in to comment.