Simple blogging system written in Go https://goblog.app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

97 lines
2.2 KiB

package main
import (
"fmt"
"net/http"
"reflect"
"strconv"
"sync"
"github.com/go-chi/chi/v5"
"github.com/vcraescu/go-paginator"
)
type commentsPaginationAdapter struct {
config *commentsRequestConfig
nums int64
getNums sync.Once
db *database
}
func (p *commentsPaginationAdapter) Nums() (int64, error) {
p.getNums.Do(func() {
nums, _ := p.db.countComments(p.config)
p.nums = int64(nums)
})
return p.nums, nil
}
func (p *commentsPaginationAdapter) Slice(offset, length int, data any) error {
modifiedConfig := *p.config
modifiedConfig.offset = offset
modifiedConfig.limit = length
comments, err := p.db.getComments(&modifiedConfig)
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&comments).Elem())
return err
}
func (a *goBlog) commentsAdmin(w http.ResponseWriter, r *http.Request) {
commentsPath := r.Context().Value(pathKey).(string)
// Adapter
p := paginator.New(&commentsPaginationAdapter{config: &commentsRequestConfig{}, db: a.db}, 5)
p.SetPage(stringToInt(chi.URLParam(r, "page")))
var comments []*comment
err := p.Results(&comments)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
// Navigation
var hasPrev, hasNext bool
var prevPage, nextPage int
var prevPath, nextPath string
hasPrev, _ = p.HasPrev()
if hasPrev {
prevPage, _ = p.PrevPage()
} else {
prevPage, _ = p.Page()
}
if prevPage < 2 {
prevPath = commentsPath
} else {
prevPath = fmt.Sprintf("%s/page/%d", commentsPath, prevPage)
}
hasNext, _ = p.HasNext()
if hasNext {
nextPage, _ = p.NextPage()
} else {
nextPage, _ = p.Page()
}
nextPath = fmt.Sprintf("%s/page/%d", commentsPath, nextPage)
// Render
a.render(w, r, a.renderCommentsAdmin, &renderData{
Data: &commentsRenderData{
comments: comments,
hasPrev: hasPrev,
hasNext: hasNext,
prev: prevPath,
next: nextPath,
},
})
}
func (a *goBlog) commentsAdminDelete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(r.FormValue("commentid"))
if err != nil {
a.serveError(w, r, err.Error(), http.StatusBadRequest)
return
}
err = a.db.deleteComment(id)
if err != nil {
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return
}
a.cache.purge()
http.Redirect(w, r, ".", http.StatusFound)
}