diff --git a/server/handlers.go b/server/handlers.go
index f228adb..92c151d 100644
--- a/server/handlers.go
+++ b/server/handlers.go
@@ -12,6 +12,7 @@ import (
"strings"
"path/filepath"
"strconv"
+ "math"
)
func IndexHandler(rw http.ResponseWriter, req *http.Request) {
@@ -287,7 +288,12 @@ func ItemHandler(rw http.ResponseWriter, req *http.Request) {
func ItemListHandler(rw http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
+ perPage := 20
+ curPage := 1
query := req.URL.Query()
+ if page, err := strconv.ParseInt(query.Get("page"), 10, 64); err == nil {
+ curPage = int(page)
+ }
filter := storage.ItemFilter{}
if folderID, err := strconv.ParseInt(query.Get("folder_id"), 10, 64); err == nil {
filter.FolderID = &folderID
@@ -299,9 +305,16 @@ func ItemListHandler(rw http.ResponseWriter, req *http.Request) {
statusValue := storage.StatusValues[status]
filter.Status = &statusValue
}
- items := db(req).ListItems(filter)
+ items := db(req).ListItems(filter, (curPage-1)*perPage, perPage)
+ count := db(req).CountItems(filter)
rw.WriteHeader(http.StatusOK)
- writeJSON(rw, items)
+ writeJSON(rw, map[string]interface{}{
+ "page": map[string]int{
+ "cur": curPage,
+ "num": int(math.Ceil(float64(count) / float64(perPage))),
+ },
+ "list": items,
+ })
} else if req.Method == "PUT" {
query := req.URL.Query()
filter := storage.ItemFilter{}
diff --git a/storage/item.go b/storage/item.go
index 072db42..1b93841 100644
--- a/storage/item.go
+++ b/storage/item.go
@@ -96,7 +96,7 @@ func (s *Storage) CreateItems(items []Item) bool {
return true
}
-func (s *Storage) ListItems(filter ItemFilter) []Item {
+func listQueryPredicate(filter ItemFilter) (string, []interface{}) {
cond := make([]string, 0)
args := make([]interface{}, 0)
if filter.FolderID != nil {
@@ -116,7 +116,11 @@ func (s *Storage) ListItems(filter ItemFilter) []Item {
if len(cond) > 0 {
predicate = strings.Join(cond, " and ")
}
+ return predicate, args
+}
+func (s *Storage) ListItems(filter ItemFilter, offset, limit int) []Item {
+ predicate, args := listQueryPredicate(filter)
result := make([]Item, 0, 0)
query := fmt.Sprintf(`
select
@@ -126,7 +130,8 @@ func (s *Storage) ListItems(filter ItemFilter) []Item {
join feeds f on f.id = i.feed_id
where %s
order by i.date desc
- `, predicate)
+ limit %d offset %d
+ `, predicate, limit, offset)
rows, err := s.db.Query(query, args...)
if err != nil {
s.log.Print(err)
@@ -157,6 +162,22 @@ func (s *Storage) ListItems(filter ItemFilter) []Item {
return result
}
+func (s *Storage) CountItems(filter ItemFilter) int64 {
+ predicate, args := listQueryPredicate(filter)
+ query := fmt.Sprintf(`
+ select count(i.id)
+ from items i
+ join feeds f on f.id = i.feed_id
+ where %s`, predicate)
+ row := s.db.QueryRow(query, args...)
+ if row != nil {
+ var result int64
+ row.Scan(&result)
+ return result
+ }
+ return 0
+}
+
func (s *Storage) UpdateItemStatus(item_id int64, status ItemStatus) bool {
_, err := s.db.Exec(`update items set status = ? where id = ?`, status, item_id)
return err == nil
diff --git a/template/index.html b/template/index.html
index 17fb6ec..db086d5 100644
--- a/template/index.html
+++ b/template/index.html
@@ -77,9 +77,9 @@
-