package endpoints import ( "encoding/json" "net/http" . "github.com/imosed/signet/data" ) func getQualifiedRewardFunds() []RewardFund { var standalone []RewardFund var currentFromQueues []RewardFund var allQueues []RewardFund Db.Table("reward_funds"). Select("reward_funds.id", "reward_funds.created_at", "reward_funds.updated_at", "reward_funds.deleted_at", "reward_funds.asset", "reward_funds.fund_wallet", "reward_funds.selling_wallet", "reward_funds.issuer_wallet", "reward_funds.memo", "reward_funds.price", "reward_funds.amount_available", "reward_funds.min_contribution", "reward_funds.title", "reward_funds.description"). Joins("left outer join queue_orders qo on reward_funds.id = qo.reward_fund_id"). Where("qo.reward_fund_id is null"). Scan(&standalone) tempTable := Db.Table("contributions c"). Select("c.reward_fund_id", "queue_id", "sum(amount) amt"). Joins("inner join queue_orders qo on c.reward_fund_id = qo.reward_fund_id"). Group("c.reward_fund_id"). Group("qo.queue_id") Db.Table("reward_funds"). Select("distinct on (qo.queue_id) reward_funds.id", "reward_funds.created_at", "reward_funds.updated_at", "reward_funds.deleted_at", "reward_funds.asset", "reward_funds.fund_wallet", "reward_funds.selling_wallet", "reward_funds.issuer_wallet", "reward_funds.memo", "reward_funds.price", "reward_funds.amount_available", "reward_funds.min_contribution", "reward_funds.title", "reward_funds.description"). Joins("inner join queue_orders qo on reward_funds.id = qo.reward_fund_id"). Joins("left join contributions c on reward_funds.id = c.reward_fund_id"). Joins("inner join (?) tt on reward_funds.id = tt.reward_fund_id", tempTable, Db.Where("tt.amount < reward_funds.amount_available or tt.amount is null")). Order("qo.queue_id"). Order("qo.order"). Scan(¤tFromQueues) allQueues = append(standalone, currentFromQueues...) return allQueues } func GetRewardFunds(w http.ResponseWriter, r *http.Request) { var req GetRewardFundsRequest err := json.NewDecoder(r.Body).Decode(&req) if err != nil { panic("Could not decode body") } var resp GetRewardFundsResponse var rewardFunds []RewardFund rewardFunds = getQualifiedRewardFunds() for _, f := range rewardFunds { resp.RewardFunds = append(resp.RewardFunds, FundInfo{ ID: f.ID, CreatedAt: f.CreatedAt, Asset: f.Asset, FundWallet: f.FundWallet, IssuerWallet: f.IssuerWallet, Memo: f.Memo, AmountAvailable: f.AmountAvailable, MinContribution: f.MinContribution, Title: f.Title, Description: f.Description, Bonuses: f.Bonuses, }) } err = json.NewEncoder(w).Encode(resp) if err != nil { panic("Could not deliver response") } } type GetRewardFundsRequest struct { Offset int `json:"offset"` } type GetRewardFundsResponse struct { RewardFunds []FundInfo `json:"rewardFunds"` Total int64 `json:"total"` }