/
accounts_list.go
74 lines (59 loc) · 2.01 KB
/
accounts_list.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package api
import (
"net/http"
"strconv"
"github.com/labstack/echo/v4"
"github.com/sparkymat/honey/auth"
"github.com/sparkymat/honey/internal/dbx"
)
type AccountsListResponse struct {
Items []presenter.Account `json:"items"`
PageSize int `json:"page_size"`
PageNumber int `json:"page_number"`
TotalCount int `json:"total_count"`
}
func AccountsList(_ ConfigService, db DatabaseService) echo.HandlerFunc {
return func(c echo.Context) error {
user, isUser := c.Get(auth.UserKey).(dbx.User)
if !isUser {
return renderError(c, http.StatusInternalServerError, "failed to load user", nil)
}
pageSizeString := c.QueryParam("page_size")
pageSize, err := strconv.ParseInt(pageSizeString, 10, 32)
if err != nil {
return renderError(c, http.StatusBadRequest, "page_size was invalid", err)
}
pageNumberString := c.QueryParam("page_number")
pageNumber, err := strconv.ParseInt(pageNumberString, 10, 32)
if err != nil {
return renderError(c, http.StatusBadRequest, "page_number was invalid", err)
}
offset := (pageNumber - 1) * pageSize
bookmarks, err := db.FetchBookmarksList(
c.Request().Context(),
dbx.FetchBookmarksListParams{
UserID: user.ID,
PageOffset: int32(offset),
PageLimit: int32(pageSize),
},
)
if err != nil {
return renderError(c, http.StatusInternalServerError, "failed to fetch bookmarks", err)
}
totalCount, err := db.CountBookmarksList(c.Request().Context(), user.ID)
if err != nil {
return renderError(c, http.StatusInternalServerError, "failed to fetch bookmarks count", err)
}
presentedBookmarks := []presenter.Bookmark{}
for _, bookmark := range bookmarks {
presentedBookmarks = append(presentedBookmarks, presenter.BookmarkFromModel(bookmark))
}
response := AccountsListResponse{
Items: presentedBookmarks,
PageSize: int(pageSize),
PageNumber: int(pageNumber),
TotalCount: int(totalCount),
}
return c.JSON(http.StatusOK, response) //nolint:wrapcheck
}
}