diff --git a/endpoints/analytics.go b/endpoints/analytics.go new file mode 100644 index 0000000..d2153ea --- /dev/null +++ b/endpoints/analytics.go @@ -0,0 +1,71 @@ +package endpoints + +import ( + "encoding/json" + "net/http" + + . "github.com/imosed/signet/data" + "github.com/rs/zerolog/log" +) + +type AnalyticsFund struct { + ID int `json:"id"` + Asset string `json:"asset"` + MinContribution float64 `json:"minContribution"` + AmountAvailable float64 `json:"amountAvailable"` + Memo string `json:"memo"` + FundWallet string `json:"fundWallet"` + Raised float64 `json:"raised"` +} + +type NearlyCompleteFundsRequest struct { + Threshold float32 `json:"threshold"` +} + +type NearlyCompleteFundsResponse struct { + Funds []AnalyticsFund `json:"funds"` +} + +func NearlyCompleteFunds(w http.ResponseWriter, r *http.Request) { + var req NearlyCompleteFundsRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + log.Error().Err(err).Msg("Could not decode body in NearlyCompleteFunds call") + return + } + + var resp NearlyCompleteFundsResponse + Db.Table("contributions"). + Select("rf.id", "asset", "min_contribution", "amount_available", "memo", "fund_wallet", "sum(amount) as raised"). + Joins("inner join reward_funds rf on rf.id = contributions.reward_fund_id"). + Group("asset, rf.id, min_contribution, amount_available, memo, fund_wallet"). + Having("sum(amount) between (rf.amount_available * ?) and rf.amount_available", req.Threshold/100). + Scan(&resp.Funds) + err = json.NewEncoder(w).Encode(resp) + if err != nil { + log.Error().Err(err).Msg("Could not deliver response in NearlyCompleteFunds call") + return + } +} + +// CompletedFundsRequest TODO: Finish out this for a future release +type CompletedFundsRequest struct { +} + +type CompletedFundsResponse struct { +} + +func CompletedFunds(w http.ResponseWriter, r *http.Request) { + var req CompletedFundsRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + panic("Could not decode body") + } + + var resp CompletedFundsResponse + + err = json.NewEncoder(w).Encode(resp) + if err != nil { + panic("Could not deliver response") + } +} diff --git a/endpoints/editqueue.go b/endpoints/editqueue.go new file mode 100644 index 0000000..7818446 --- /dev/null +++ b/endpoints/editqueue.go @@ -0,0 +1,41 @@ +package endpoints + +import ( + "encoding/json" + "net/http" + + . "github.com/imosed/signet/data" + "github.com/rs/zerolog/log" +) + +type FundOrder struct { + ID int `json:"id"` + Asset string `json:"asset"` + Order int `json:"order"` +} + +type EditQueueRequest struct { + QueueID int `json:"queueID"` + FundOrders []FundOrder `json:"fundOrders"` +} + +func EditQueue(w http.ResponseWriter, r *http.Request) { + var req EditQueueRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + log.Error().Err(err).Msg("Could not decode body in EditQueue call") + return + } + + var resp SuccessResponse + for _, qo := range req.FundOrders { + Db.Table("queue_orders"). + Where("reward_fund_id = ? and queue_id = ?", qo.ID, req.QueueID). + Update("order", qo.Order) + } + + err = json.NewEncoder(w).Encode(resp) + if err != nil { + log.Error().Err(err).Msg("Could not deliver response in EditQueue call") + } +} diff --git a/endpoints/getqueuemembers.go b/endpoints/getqueuemembers.go index a4556b0..5193023 100644 --- a/endpoints/getqueuemembers.go +++ b/endpoints/getqueuemembers.go @@ -35,6 +35,7 @@ func GetQueueMembers(w http.ResponseWriter, r *http.Request) { Db.Table("queue_orders qo").Select("rf.id, asset, title, qo.order"). Where("queue_id = ?", req.ID). Joins("inner join reward_funds rf on qo.reward_fund_id = rf.id"). + Order("qo.order"). Scan(&members) var resp GetQueueMembersResponse diff --git a/main.go b/main.go index 93c0bd9..cf6e0b8 100644 --- a/main.go +++ b/main.go @@ -37,6 +37,7 @@ func main() { router.HandleFunc("/CreateQueue", endpoints.CreateQueue) router.HandleFunc("/GetQueues", endpoints.GetQueues) router.HandleFunc("/GetQueueMembers", endpoints.GetQueueMembers) + router.HandleFunc("/EditQueue", endpoints.EditQueue) router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund) router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund) // router.HandleFunc("/SubmitFund", endpoints.SubmitFund) @@ -45,6 +46,7 @@ func main() { router.HandleFunc("/ContributorStream", endpoints.ContributorStream) router.HandleFunc("/Login", endpoints.Login) router.HandleFunc("/Register", endpoints.Register) + router.HandleFunc("/NearlyCompleteFunds", endpoints.NearlyCompleteFunds) router.HandleFunc("/EscalatePrivileges", endpoints.EscalatePrivileges) router.HandleFunc("/UsersExist", endpoints.UsersExist)