diff --git a/internal/http/handlers.go b/internal/http/handlers.go index 7521503..063cc3d 100644 --- a/internal/http/handlers.go +++ b/internal/http/handlers.go @@ -1,7 +1,9 @@ package http import ( + "math" "net/http" + "strconv" "strings" "time" @@ -15,7 +17,6 @@ import ( type Handlers struct { guestbookDB *gbsqlite.SQLiteDatabase captchaExpire time.Duration - databasesPath string guestbookOwner string guestbookPageSize int64 } @@ -86,6 +87,35 @@ func (h *Handlers) Article(w http.ResponseWriter, r *http.Request) { web.Article(artcl.Title, artcl.Description, string(artcl.Body), artcl.Date, r.Host, name, r, w) } +func (h *Handlers) Guestbook(w http.ResponseWriter, r *http.Request) { + var page int64 + var pageSize int64 + + pageStr := r.URL.Query().Get("p") + if pageStr != "" { + page, _ = strconv.ParseInt(pageStr, 10, 64) + } else { + page = 1 + } + + pageSzStr := r.URL.Query().Get("ps") + if pageSzStr != "" { + pageSize, _ = strconv.ParseInt(pageSzStr, 10, 64) + } else { + pageSize = h.guestbookPageSize + } + + entries, err := h.guestbookDB.Entries(page, pageSize) + if err != nil { + return + } + + entriesCount, _ := h.guestbookDB.Count() + pageCount := int64(math.Ceil(float64(entriesCount) / float64(h.guestbookPageSize))) + + web.Guestbook("/ Guestbook", h.guestbookOwner, "", pageCount, entries, r, w) +} + func (h *Handlers) RSS(w http.ResponseWriter, r *http.Request) { posts := []mindflow.Post{ {Category: "Kek", Date: time.Now(), Title: "LMAO", Body: "Testing!"},