2021-02-17 15:07:42 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
|
2021-03-03 17:19:55 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2021-02-17 15:07:42 +00:00
|
|
|
"github.com/vcraescu/go-paginator"
|
|
|
|
)
|
|
|
|
|
|
|
|
type commentsPaginationAdapter struct {
|
|
|
|
config *commentsRequestConfig
|
|
|
|
nums int64
|
2021-06-06 12:39:42 +00:00
|
|
|
db *database
|
2021-02-17 15:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *commentsPaginationAdapter) Nums() (int64, error) {
|
|
|
|
if p.nums == 0 {
|
2021-06-06 12:39:42 +00:00
|
|
|
nums, _ := p.db.countComments(p.config)
|
2021-02-17 15:07:42 +00:00
|
|
|
p.nums = int64(nums)
|
|
|
|
}
|
|
|
|
return p.nums, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *commentsPaginationAdapter) Slice(offset, length int, data interface{}) error {
|
|
|
|
modifiedConfig := *p.config
|
|
|
|
modifiedConfig.offset = offset
|
|
|
|
modifiedConfig.limit = length
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
comments, err := p.db.getComments(&modifiedConfig)
|
2021-02-17 15:07:42 +00:00
|
|
|
reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&comments).Elem())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) commentsAdmin(w http.ResponseWriter, r *http.Request) {
|
2021-07-27 10:51:08 +00:00
|
|
|
blog := r.Context().Value(blogKey).(string)
|
|
|
|
commentsPath := r.Context().Value(pathKey).(string)
|
2021-03-22 07:20:56 +00:00
|
|
|
// Adapter
|
|
|
|
pageNoString := chi.URLParam(r, "page")
|
|
|
|
pageNo, _ := strconv.Atoi(pageNoString)
|
2021-06-06 12:39:42 +00:00
|
|
|
p := paginator.New(&commentsPaginationAdapter{config: &commentsRequestConfig{}, db: a.db}, 5)
|
2021-03-22 07:20:56 +00:00
|
|
|
p.SetPage(pageNo)
|
|
|
|
var comments []*comment
|
|
|
|
err := p.Results(&comments)
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-03-22 07:20:56 +00:00
|
|
|
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()
|
2021-02-17 15:07:42 +00:00
|
|
|
}
|
2021-03-22 07:20:56 +00:00
|
|
|
nextPath = fmt.Sprintf("%s/page/%d", commentsPath, nextPage)
|
|
|
|
// Render
|
2021-06-06 12:39:42 +00:00
|
|
|
a.render(w, r, templateCommentsAdmin, &renderData{
|
2021-03-22 07:20:56 +00:00
|
|
|
BlogString: blog,
|
|
|
|
Data: map[string]interface{}{
|
|
|
|
"Comments": comments,
|
|
|
|
"HasPrev": hasPrev,
|
|
|
|
"HasNext": hasNext,
|
2021-06-10 20:09:50 +00:00
|
|
|
"Prev": prevPath,
|
|
|
|
"Next": nextPath,
|
2021-03-22 07:20:56 +00:00
|
|
|
},
|
|
|
|
})
|
2021-02-17 15:07:42 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 12:39:42 +00:00
|
|
|
func (a *goBlog) commentsAdminDelete(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 15:07:42 +00:00
|
|
|
id, err := strconv.Atoi(r.FormValue("commentid"))
|
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusBadRequest)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
err = a.db.deleteComment(id)
|
2021-02-17 15:07:42 +00:00
|
|
|
if err != nil {
|
2021-06-06 12:39:42 +00:00
|
|
|
a.serveError(w, r, err.Error(), http.StatusInternalServerError)
|
2021-02-17 15:07:42 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-06 12:39:42 +00:00
|
|
|
a.cache.purge()
|
2021-02-17 15:07:42 +00:00
|
|
|
http.Redirect(w, r, ".", http.StatusFound)
|
|
|
|
}
|