-
Notifications
You must be signed in to change notification settings - Fork 16
/
statistics_handler.go
48 lines (39 loc) · 1.42 KB
/
statistics_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package httphandler
import (
"errors"
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/stellar/go/support/render/httpjson"
"github.com/stellar/stellar-disbursement-platform-backend/db"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror"
"github.com/stellar/stellar-disbursement-platform-backend/internal/statistics"
)
type StatisticsHandler struct {
DBConnectionPool db.DBConnectionPool
}
func (s StatisticsHandler) GetStatistics(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
stats, err := statistics.CalculateStatistics(ctx, s.DBConnectionPool)
if err != nil {
httperror.InternalError(ctx, "Cannot calculate statistics", err, nil).Render(w)
return
}
httpjson.RenderStatus(w, http.StatusOK, stats, httpjson.JSON)
}
func (s StatisticsHandler) GetStatisticsByDisbursement(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
disbursementID := chi.URLParam(r, "id")
stats, err := statistics.CalculateStatisticsByDisbursement(ctx, s.DBConnectionPool, disbursementID)
if err != nil {
if errors.Is(statistics.ErrResourcesNotFound, err) {
errorMsg := fmt.Sprintf("a disbursement with the id %s does not exist", disbursementID)
httperror.NotFound(errorMsg, err, nil).Render(w)
return
} else {
httperror.InternalError(ctx, "Cannot calculate statistics", err, nil).Render(w)
return
}
}
httpjson.RenderStatus(w, http.StatusOK, stats, httpjson.JSON)
}