This is my new blog CMS https://jlelse.blog
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.

99 lines
2.4 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "net/http"
  5. "reflect"
  6. "strconv"
  7. "github.com/go-chi/chi/v5"
  8. "github.com/vcraescu/go-paginator"
  9. )
  10. type commentsPaginationAdapter struct {
  11. config *commentsRequestConfig
  12. nums int64
  13. db *database
  14. }
  15. func (p *commentsPaginationAdapter) Nums() (int64, error) {
  16. if p.nums == 0 {
  17. nums, _ := p.db.countComments(p.config)
  18. p.nums = int64(nums)
  19. }
  20. return p.nums, nil
  21. }
  22. func (p *commentsPaginationAdapter) Slice(offset, length int, data interface{}) error {
  23. modifiedConfig := *p.config
  24. modifiedConfig.offset = offset
  25. modifiedConfig.limit = length
  26. comments, err := p.db.getComments(&modifiedConfig)
  27. reflect.ValueOf(data).Elem().Set(reflect.ValueOf(&comments).Elem())
  28. return err
  29. }
  30. func (a *goBlog) commentsAdmin(w http.ResponseWriter, r *http.Request) {
  31. blog := r.Context().Value(blogContextKey).(string)
  32. commentsPath := r.Context().Value(pathContextKey).(string)
  33. // Adapter
  34. pageNoString := chi.URLParam(r, "page")
  35. pageNo, _ := strconv.Atoi(pageNoString)
  36. p := paginator.New(&commentsPaginationAdapter{config: &commentsRequestConfig{}, db: a.db}, 5)
  37. p.SetPage(pageNo)
  38. var comments []*comment
  39. err := p.Results(&comments)
  40. if err != nil {
  41. a.serveError(w, r, err.Error(), http.StatusInternalServerError)
  42. return
  43. }
  44. // Navigation
  45. var hasPrev, hasNext bool
  46. var prevPage, nextPage int
  47. var prevPath, nextPath string
  48. hasPrev, _ = p.HasPrev()
  49. if hasPrev {
  50. prevPage, _ = p.PrevPage()
  51. } else {
  52. prevPage, _ = p.Page()
  53. }
  54. if prevPage < 2 {
  55. prevPath = commentsPath
  56. } else {
  57. prevPath = fmt.Sprintf("%s/page/%d", commentsPath, prevPage)
  58. }
  59. hasNext, _ = p.HasNext()
  60. if hasNext {
  61. nextPage, _ = p.NextPage()
  62. } else {
  63. nextPage, _ = p.Page()
  64. }
  65. nextPath = fmt.Sprintf("%s/page/%d", commentsPath, nextPage)
  66. // Render
  67. a.render(w, r, templateCommentsAdmin, &renderData{
  68. BlogString: blog,
  69. Data: map[string]interface{}{
  70. "Comments": comments,
  71. "HasPrev": hasPrev,
  72. "HasNext": hasNext,
  73. "Prev": prevPath,
  74. "Next": nextPath,
  75. },
  76. })
  77. }
  78. func (a *goBlog) commentsAdminDelete(w http.ResponseWriter, r *http.Request) {
  79. id, err := strconv.Atoi(r.FormValue("commentid"))
  80. if err != nil {
  81. a.serveError(w, r, err.Error(), http.StatusBadRequest)
  82. return
  83. }
  84. err = a.db.deleteComment(id)
  85. if err != nil {
  86. a.serveError(w, r, err.Error(), http.StatusInternalServerError)
  87. return
  88. }
  89. a.cache.purge()
  90. http.Redirect(w, r, ".", http.StatusFound)
  91. }