package handler import ( "encoding/json" "log" "net/http" "strconv" "tangled.org/moth11.net/88x31/types" "time" ) func (h *Handler) getButtons(w http.ResponseWriter, r *http.Request) { limit := r.URL.Query().Get("limit") limitI, err := strconv.Atoi(limit) if err != nil { limitI = 50 } if limitI > 100 { limitI = 100 } if limitI < 1 { limitI = 1 } cursor := r.URL.Query().Get("cursor") var cursorptr *time.Time if cursor != "" { t, err := time.Parse(time.RFC3339, cursor) if err == nil { cursorptr = &t } } btnViews, ncursor, err := h.db.GetButtons(limitI, cursorptr, r.Context()) if err != nil { log.Println(err) http.Error(w, "error getting buttons!", http.StatusInternalServerError) return } type Resp struct { BtnViews []types.ButtonView `json:"button"` Cursor *time.Time `json:"cursor,omitempty"` } myresp := Resp{} if btnViews == nil { myresp.BtnViews = make([]types.ButtonView, 0) myresp.Cursor = nil } else { myresp.BtnViews = btnViews myresp.Cursor = ncursor } w.Header().Set("Content-Type", "application/json") encoder := json.NewEncoder(w) err = encoder.Encode(myresp) if err != nil { log.Println(err) http.Error(w, "error encoding response", http.StatusInternalServerError) } } func (h *Handler) getLikedButtons(w http.ResponseWriter, r *http.Request) { did := r.URL.Query().Get("did") limita := r.URL.Query().Get("limit") limit, err := strconv.Atoi(limita) if err != nil { limit = 50 } if limit < 1 { limit = 1 } if limit > 100 { limit = 100 } cursor := r.URL.Query().Get("cursor") var crsr *string if cursor != "" { crsr = &cursor } btns, ncursor, err := h.db.GetUserLikes(did, limit, crsr, r.Context()) if err != nil { log.Println(err) http.Error(w, "error", http.StatusInternalServerError) return } type glbResp struct { Cursor *time.Time `json:"cursor,omitempty"` Buttons []types.ButtonView `json:"buttons"` } glbr := glbResp{ ncursor, btns, } w.Header().Set("Content-Type", "application/json") encoder := json.NewEncoder(w) err = encoder.Encode(glbr) if err != nil { log.Println(err) http.Error(w, "encoding error", http.StatusInternalServerError) } }