-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package dbflow | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func ConnectHackDatabase() *gorm.DB { | ||
// Connect to the database | ||
dbHost := os.Getenv("SUPABASE_DB_HOST") | ||
dbUsername := os.Getenv("SUPABASE_DB_USERNAME") | ||
dbPassword := os.Getenv("SUPABASE_DB_PASSWORD") | ||
dbName := os.Getenv("SUPABASE_DB_NAME") | ||
dbPort := os.Getenv("SUPABASE_DB_PORT") | ||
|
||
// Construct the connection string | ||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", | ||
dbHost, dbUsername, dbPassword, dbName, dbPort) | ||
|
||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
if err != nil { | ||
panic("failed to connect database") | ||
} | ||
|
||
// // Auto migrate the schema | ||
// db.AutoMigrate(&SlnSui{}) | ||
return db | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package suiRouter | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func SuiApplyRoutes(r *gin.RouterGroup) { | ||
v1 := r.Group("snl") | ||
{ | ||
v1.GET("") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package sln_sui_dboperation | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
dbflow "github.com/MyriadFlow/storefront-gateway/hackathon/dbFlow" | ||
) | ||
|
||
var db = dbflow.ConnectHackDatabase() | ||
|
||
// Define a struct to represent your table | ||
type SlnSui struct { | ||
ID uint `gorm:"primaryKey"` | ||
GameID int `gorm:"column:game_id"` | ||
CreatedAt time.Time `gorm:"column:created_at"` | ||
ContractAddress string `gorm:"column:contract_address"` | ||
} | ||
|
||
// func ConnectHackDatabase() *gorm.DB { | ||
// // Connect to the database | ||
// dbHost := os.Getenv("SUPABASE_DB_HOST") | ||
// dbUsername := os.Getenv("SUPABASE_DB_USERNAME") | ||
// dbPassword := os.Getenv("SUPABASE_DB_PASSWORD") | ||
// dbName := os.Getenv("SUPABASE_DB_NAME") | ||
// dbPort := os.Getenv("SUPABASE_DB_PORT") | ||
|
||
// // Construct the connection string | ||
// dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", | ||
// dbHost, dbUsername, dbPassword, dbName, dbPort) | ||
|
||
// db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) | ||
// if err != nil { | ||
// panic("failed to connect database") | ||
// } | ||
|
||
// // // Auto migrate the schema | ||
// // db.AutoMigrate(&SlnSui{}) | ||
// return db | ||
|
||
// } | ||
|
||
func InsertSLN_SUI() { | ||
// Create a new record | ||
|
||
newRecord := SlnSui{ | ||
CreatedAt: time.Now(), | ||
ContractAddress: "0x1234567890abcdef", | ||
} | ||
db.Create(&newRecord) | ||
|
||
// Check for errors | ||
if db.Error != nil { | ||
panic(db.Error) | ||
} | ||
|
||
fmt.Println("Record inserted successfully!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package config | ||
|
||
var Dir string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package smartcontractdeploy | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
|
||
"github.com/MyriadFlow/storefront-gateway/hackathon/sui_snl/config" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func ApplyRoutes(r *gin.RouterGroup) { | ||
g := r.Group("/Contract") | ||
{ | ||
g.POST("", DeployContract) | ||
} | ||
} | ||
|
||
func DeployContract(c *gin.Context) { | ||
var req Contract | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error(), "message": "Failed to bind request"}) | ||
return | ||
} | ||
|
||
network := req.Network | ||
|
||
jsonByte, err := json.Marshal(req.Data) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error(), "message": "failed to encode request body"}) | ||
return | ||
} | ||
response, err := genResponse(jsonByte, network) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
filePath := "scripts/launch/launch.json" | ||
_, err = os.ReadFile(filePath) | ||
|
||
c.JSON(http.StatusOK, response) | ||
|
||
} | ||
|
||
func genResponse(jsonByte []byte, network string) ([]byte, error) { | ||
os.Chdir(config.Dir) | ||
filePath := "scripts/launch/launch.json" | ||
|
||
_, err := os.ReadFile(filePath) | ||
if err != nil { | ||
fmt.Println("error reading") | ||
return nil, err | ||
} | ||
|
||
err = os.WriteFile(filePath, []byte(jsonByte), 0644) | ||
if err != nil { | ||
fmt.Println("error writing") | ||
|
||
return nil, err | ||
} | ||
|
||
err = os.Chdir(".") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Execute the yarn launch command | ||
cmd := exec.Command("yarn", "launch", "--network", network) | ||
var outb, errb bytes.Buffer | ||
cmd.Stdout = &outb | ||
cmd.Stderr = &errb | ||
err = cmd.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = cmd.Wait() | ||
if err != nil { | ||
log.Printf("Command finished with error: %v", err.Error()) | ||
return nil, err | ||
} | ||
return outb.Bytes(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package smartcontractdeploy | ||
|
||
type Contract struct { | ||
Data struct { | ||
ContractName string `json:"contractName"` | ||
ConstructorParams map[string]any `json:"constructorParams"` | ||
} | ||
Network string `json:"network"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package dboperation | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
dbflow "github.com/MyriadFlow/storefront-gateway/hackathon/dbFlow" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type Person struct { | ||
ID uint `gorm:"primaryKey"` | ||
Name string | ||
Location string | ||
Interest string | ||
} | ||
|
||
type FriendRequest struct { | ||
ID uint `gorm:"primaryKey"` | ||
SenderID uint | ||
ReceiverID uint | ||
Status string | ||
} | ||
|
||
type Chat struct { | ||
ID uint `gorm:"primaryKey"` | ||
SenderID uint | ||
ReceiverID uint | ||
Message string | ||
CreatedAt time.Time | ||
} | ||
|
||
func GetPeople(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var people []Person | ||
db.Find(&people) | ||
c.JSON(http.StatusOK, people) | ||
} | ||
|
||
func SendFriendRequest(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var request FriendRequest | ||
if err := c.BindJSON(&request); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
db.Create(&request) | ||
c.JSON(http.StatusCreated, request) | ||
} | ||
|
||
func GetAcceptedFriendRequests(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var requests []FriendRequest | ||
db.Where("status = ?", "accepted").Find(&requests) | ||
c.JSON(http.StatusOK, requests) | ||
} | ||
|
||
func SendMessage(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var chat Chat | ||
if err := c.BindJSON(&chat); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
db.Create(&chat) | ||
c.JSON(http.StatusCreated, chat) | ||
} | ||
|
||
func GetInterests(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var interests []string | ||
db.Model(&Person{}).Pluck("DISTINCT interest", &interests) | ||
c.JSON(http.StatusOK, interests) | ||
} | ||
|
||
func GetLocations(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var locations []string | ||
db.Model(&Person{}).Pluck("DISTINCT location", &locations) | ||
c.JSON(http.StatusOK, locations) | ||
} | ||
|
||
func AddProfileInfo(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var person Person | ||
if err := c.BindJSON(&person); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
db.Create(&person) | ||
c.JSON(http.StatusCreated, person) | ||
} | ||
|
||
func jkbnds() { | ||
var notUsableTopics = []string{"Data Structures and Algorithms", | ||
"Database Management Systems (DBMS)", | ||
"Computer Networks"} | ||
addingValidation := "" | ||
|
||
if len(notUsableTopics) != 0 { | ||
addingValidation += `Do not add these under below topics in the response := | ||
` | ||
|
||
for i, _ := range notUsableTopics { | ||
addingValidation += notUsableTopics[i] | ||
} | ||
} | ||
|
||
} | ||
|
||
// CRUD operations for categories | ||
func CreateCategory(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var category Category | ||
if err := c.ShouldBindJSON(&category); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
db.Create(&category) | ||
c.JSON(http.StatusCreated, category) | ||
} | ||
|
||
func GetCategory(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var categories []Category | ||
if err := db.Where("category = ?", c.Param("categorie")).Find(&categories).Error; err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Category not found"}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, categories) | ||
} | ||
|
||
func UpdateCategory(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var category Category | ||
if err := db.First(&category, c.Param("id")).Error; err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Category not found"}) | ||
return | ||
} | ||
if err := c.ShouldBindJSON(&category); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
db.Save(&category) | ||
c.JSON(http.StatusOK, category) | ||
} | ||
|
||
func DeleteCategory(c *gin.Context) { | ||
var db = dbflow.ConnectHackDatabase() | ||
var category Category | ||
if err := db.First(&category, c.Param("id")).Error; err != nil { | ||
c.JSON(http.StatusNotFound, gin.H{"error": "Category not found"}) | ||
return | ||
} | ||
db.Delete(&category) | ||
c.JSON(http.StatusOK, gin.H{"message": "Category deleted successfully"}) | ||
} |
Oops, something went wrong.