From 8fb6e59b91756d518259f341a450be05bd749412 Mon Sep 17 00:00:00 2001 From: Jared Date: Thu, 5 Jan 2023 21:25:21 -0500 Subject: [PATCH] Fix up queue stuff, not ready yet --- data/context.go | 56 ++++++++++++++++----------------- endpoints/contributionstream.go | 5 +-- endpoints/createrewardfund.go | 23 ++++++++++++-- endpoints/getqueuemembers.go | 41 ++++++++++++++++++++++++ main.go | 1 + 5 files changed, 91 insertions(+), 35 deletions(-) create mode 100644 endpoints/getqueuemembers.go diff --git a/data/context.go b/data/context.go index 9ca8eec..46b0c60 100644 --- a/data/context.go +++ b/data/context.go @@ -5,65 +5,63 @@ import ( "github.com/spf13/viper" "gorm.io/driver/postgres" "gorm.io/gorm" + "time" ) +type ModelBase struct { + ID uint `gorm:"primarykey" json:"id"` + CreatedAt time.Time `json:"createdAt"` + UpdatedAt time.Time `json:"updatedAt"` + DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` +} + type Bonus struct { - gorm.Model + ModelBase Goal float64 `json:"goal"` Percent float64 `json:"percent"` RewardFundID uint `json:"rewardFundID"` } type Queue struct { - gorm.Model - Name string `json:"name"` - Funds []RewardFund `json:"funds"` + ModelBase + Name string `gorm:"type:varchar(50)" json:"name"` + Orders []QueueOrder `json:"order"` } type RewardFund struct { - gorm.Model + ModelBase Asset string `json:"asset"` FundWallet string `json:"fundWallet"` SellingWallet string `json:"sellingWallet"` IssuerWallet string `json:"issuerWallet"` Memo string `json:"memo"` - Price float64 `json:"price"` + Price float64 `gorm:"type:decimal(19,7)" json:"price"` AmountAvailable float64 `gorm:"type:decimal(19,7)" json:"amountAvailable"` MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"` Contributions []Contribution `json:"contributions"` Title string `gorm:"type:varchar(50)" json:"title"` Description string `gorm:"type:text" json:"description"` - QueueID uint `json:"queueID"` Bonuses []Bonus `json:"bonuses"` + QueueOrder QueueOrder `json:"queueOrder"` } -type QueueRewardFund struct { +type QueueOrder struct { + ModelBase + QueueID uint `json:"queueID"` + RewardFundID uint `json:"rewardFundID"` + Order uint16 `gorm:"type:smallint" json:"order"` } type Contribution struct { - gorm.Model - Wallet string `json:"wallet"` - Amount float64 `gorm:"type:decimal(19,7)" json:"amount"` - TransactionID string `json:"transactionID"` - RewardFundID uint `json:"rewardFundID"` - Tags []AppliedTag `json:"tags"` -} - -type Tag struct { - gorm.Model - Description string `json:"description"` - Active bool `json:"active"` - Contribution AppliedTag `json:"contribution"` -} - -type AppliedTag struct { - gorm.Model - TagID uint `json:"tagID"` - ContributionID uint `json:"contributionID"` + ModelBase + Wallet string `json:"wallet"` + Amount float64 `gorm:"type:decimal(19,7)" json:"amount"` + TransactionID string `json:"transactionID"` + RewardFundID uint `json:"rewardFundID"` } type User struct { - gorm.Model + ModelBase Username string `json:"username"` Password string `json:"password"` Privileges uint `json:"admin"` @@ -84,7 +82,7 @@ func InitializeDatabase() { if err != nil { panic("Could not open database") } - err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, Contribution{}, Bonus{}) + err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, QueueOrder{}, Contribution{}, Bonus{}) if err != nil { panic("Could not migrate database") } diff --git a/endpoints/contributionstream.go b/endpoints/contributionstream.go index 4c8e185..a13585f 100644 --- a/endpoints/contributionstream.go +++ b/endpoints/contributionstream.go @@ -9,7 +9,6 @@ import ( "github.com/stellar/go/protocols/horizon" "github.com/stellar/go/protocols/horizon/operations" "golang.org/x/net/context" - "gorm.io/gorm" "net/http" "strconv" "strings" @@ -74,9 +73,7 @@ func InitializeContributionStreams() { Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt) contribution := Contribution{ - Model: gorm.Model{ - CreatedAt: tx.LedgerCloseTime, - }, + ModelBase: ModelBase{CreatedAt: tx.LedgerCloseTime}, Wallet: payment.From, Amount: amt, TransactionID: payment.GetTransactionHash(), diff --git a/endpoints/createrewardfund.go b/endpoints/createrewardfund.go index 4868a5a..16261b5 100644 --- a/endpoints/createrewardfund.go +++ b/endpoints/createrewardfund.go @@ -11,12 +11,25 @@ import ( "strconv" ) +type CreateRewardFundRequest struct { + Asset string `json:"asset"` + FundWallet string `json:"fundWallet"` + SellingWallet string `json:"sellingWallet"` + IssuerWallet string `json:"issuerWallet"` + Memo string `json:"memo"` + MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"` + Title string `gorm:"type:varchar(50)" json:"title"` + Description string `gorm:"type:text" json:"description"` + QueueID uint `json:"queueID"` + Bonuses []Bonus `json:"bonuses"` +} + type SuccessResponse struct { Success bool `json:"success"` } func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { - var fund RewardFund + var fund CreateRewardFundRequest dec := json.NewDecoder(req.Body) err := dec.Decode(&fund) if err != nil { @@ -37,9 +50,14 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { Title: fund.Title, Description: fund.Description, Contributions: nil, - QueueID: fund.QueueID, } + var fundsInQueue []RewardFund + Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue) + next := uint16(len(fundsInQueue)) + + joinTable := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next} + offerReq := horizonclient.OfferRequest{ Seller: rewardFund.SellingWallet, Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet), @@ -104,6 +122,7 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { if claims.Privileges <= Admin { Db.Create(&rewardFund) + Db.Create(&joinTable) for _, cancel := range cancellations { cancel() diff --git a/endpoints/getqueuemembers.go b/endpoints/getqueuemembers.go new file mode 100644 index 0000000..741124d --- /dev/null +++ b/endpoints/getqueuemembers.go @@ -0,0 +1,41 @@ +package endpoints + +import ( + "encoding/json" + . "github.com/imosed/signet/data" + "net/http" +) + +type QueueMember struct { + Asset string `json:"asset"` + Title string `json:"title"` +} + +type GetQueueMembersRequest struct { + ID uint `json:"id"` +} + +type GetQueueMembersResponse struct { + Members []QueueMember `json:"members"` +} + +func GetQueueMembers(w http.ResponseWriter, r *http.Request) { + var req GetQueueMembersRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + panic("Could not decode body") + } + + var members []QueueMember + Db.Table("queue_orders qo").Select("reward_fund_id, asset, title").Where("queue_id = ?", req.ID). + Joins("inner join reward_funds rf on qo.reward_fund_id = rf.id"). + Scan(&members) + + var resp GetQueueMembersResponse + resp.Members = members + + err = json.NewEncoder(w).Encode(resp) + if err != nil { + panic("Could not deliver response") + } +} diff --git a/main.go b/main.go index be6ce40..718b506 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,7 @@ func main() { router.HandleFunc("/GetContributions", endpoints.GetContributions) router.HandleFunc("/CreateQueue", endpoints.CreateQueue) router.HandleFunc("/GetQueues", endpoints.GetQueues) + router.HandleFunc("/GetQueueMembers", endpoints.GetQueueMembers) router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund) router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund) //router.HandleFunc("/SubmitFund", endpoints.SubmitFund)