apps/habbits/handlers_api.go 930 B raw
1
package main
2
3
import (
4
	"net/http"
5
	"strconv"
6
7
	"github.com/stevedylandev/andromeda/pkg/web"
8
)
9
10
func (a *App) apiListHabits(w http.ResponseWriter, r *http.Request) {
11
	habits, _, err := listHabits(a.DB)
12
	if err != nil {
13
		a.Log.Error("api list habits", "err", err)
14
		web.WriteError(w, http.StatusInternalServerError, "internal error")
15
		return
16
	}
17
	if habits == nil {
18
		habits = []Habit{}
19
	}
20
	web.WriteJSON(w, http.StatusOK, habits)
21
}
22
23
func (a *App) apiListRecords(w http.ResponseWriter, r *http.Request) {
24
	limit := 100
25
	if v := r.URL.Query().Get("limit"); v != "" {
26
		if n, err := strconv.Atoi(v); err == nil && n > 0 && n <= 500 {
27
			limit = n
28
		}
29
	}
30
	records, err := listRecords(a.DB, limit)
31
	if err != nil {
32
		a.Log.Error("api list records", "err", err)
33
		web.WriteError(w, http.StatusInternalServerError, "internal error")
34
		return
35
	}
36
	if records == nil {
37
		records = []Record{}
38
	}
39
	web.WriteJSON(w, http.StatusOK, records)
40
}