Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,36 @@ func (db database) GetLeaderBoardByUuidAndAlias(uuid string, alias string) Leade
return m
}

type MonthlyEarnings struct {
Month string `json:"month"`
Year int `json:"year"`
Earnings uint `json:"earnings"`
}

func (db database) GetMonthlyEarnings(uuid string) []MonthlyEarnings {
var results []MonthlyEarnings

query := `
SELECT
TO_CHAR(paid_date, 'Month') as month,
EXTRACT(YEAR FROM paid_date) as year,
COALESCE(SUM(price), 0) as earnings
FROM bounty
WHERE paid_date IS NOT NULL
AND paid = true
AND (tribe = ? OR tribe = '')
GROUP BY
EXTRACT(YEAR FROM paid_date),
EXTRACT(MONTH FROM paid_date),
TO_CHAR(paid_date, 'Month')
ORDER BY year DESC, EXTRACT(MONTH FROM paid_date) DESC
LIMIT 12
`

db.db.Raw(query, uuid).Scan(&results)
return results
}

func (db database) UpdateLeaderBoard(uuid string, alias string, u map[string]interface{}) bool {
if uuid == "" {
return false
Expand Down
9 changes: 9 additions & 0 deletions handlers/tribes.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,15 @@ func GetLeaderBoard(w http.ResponseWriter, r *http.Request) {
}
}

func GetMonthlyEarnings(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "tribe_uuid")

monthlyEarnings := db.DB.GetMonthlyEarnings(uuid)

w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(monthlyEarnings)
}

// UpdateLeaderBoard godoc
//
// @Summary Update a leaderboard
Expand Down
1 change: 1 addition & 0 deletions routes/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func NewRouter() *http.Server {
r.Group(func(r chi.Router) {
r.Get("/tribe_by_feed", tribeHandlers.GetFirstTribeByFeed)
r.Get("/leaderboard/{tribe_uuid}", handlers.GetLeaderBoard)
r.Get("/leaderboard/{tribe_uuid}/earnings", handlers.GetMonthlyEarnings)
r.Get("/tribe_by_un/{un}", tribeHandlers.GetTribeByUniqueName)
r.Get("/tribes_by_owner/{pubkey}", tribeHandlers.GetTribesByOwner)

Expand Down