Skip to content

Commit

Permalink
Adding Hackathon backend apis
Browse files Browse the repository at this point in the history
  • Loading branch information
p-shubh committed May 13, 2024
1 parent 61e17c3 commit cafe1e3
Show file tree
Hide file tree
Showing 11 changed files with 455 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/v1/v1.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package apiv1

import (
"fmt"

authenticate "github.com/MyriadFlow/storefront-gateway/api/v1/authenticate"
creatorrole "github.com/MyriadFlow/storefront-gateway/api/v1/creatorrole"
delegateassetcreation "github.com/MyriadFlow/storefront-gateway/api/v1/delegateassetcreation"
Expand All @@ -9,6 +11,8 @@ import (
"github.com/MyriadFlow/storefront-gateway/api/v1/subgraph"
"github.com/MyriadFlow/storefront-gateway/api/v1/webapp"
"github.com/MyriadFlow/storefront-gateway/api/v1/website"
suiRouter "github.com/MyriadFlow/storefront-gateway/hackathon/sui_snl/ApiRouting"
voyagerrouting "github.com/MyriadFlow/storefront-gateway/hackathon/voyager/voyagerRouting"

"github.com/MyriadFlow/storefront-gateway/api/v1/healthcheck"
"github.com/MyriadFlow/storefront-gateway/api/v1/highlights"
Expand Down Expand Up @@ -40,5 +44,8 @@ func ApplyRoutes(r *gin.RouterGroup) {
subgraph.ApplyRoutes(v1)
webapp.ApplyRoutes(v1)
website.ApplyRoutes(v1)
fmt.Println("************* HACKATHONE APIS ***************")
voyagerrouting.VoyagerApplyRoutes(v1)
suiRouter.SuiApplyRoutes(v1)
}
}
32 changes: 32 additions & 0 deletions hackathon/dbFlow/db.circulation.go
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

}
12 changes: 12 additions & 0 deletions hackathon/sui_snl/ApiRouting/snl.routing.go
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("")
}
}
58 changes: 58 additions & 0 deletions hackathon/sui_snl/DBoperation/sln.sui.db.operation.go
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!")
}
3 changes: 3 additions & 0 deletions hackathon/sui_snl/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package config

var Dir string
86 changes: 86 additions & 0 deletions hackathon/sui_snl/smartContractDeploy/smartContract.go
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
}
9 changes: 9 additions & 0 deletions hackathon/sui_snl/smartContractDeploy/type.go
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"`
}
158 changes: 158 additions & 0 deletions hackathon/voyager/dbOperation/db.operation.go
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"})
}
Loading

0 comments on commit cafe1e3

Please sign in to comment.