delete.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package web
  2. import (
  3. "log"
  4. "net/http"
  5. "net/url"
  6. "strings"
  7. "github.com/gin-contrib/sessions"
  8. "github.com/gin-gonic/gin"
  9. csrf "github.com/utrack/gin-csrf"
  10. "code.osinet.fr/fgm/sqs_demo/back/services/redriver"
  11. )
  12. func makeDeleteHandler(rd redriver.Redriver) gin.HandlerFunc {
  13. type IDList struct {
  14. ID string `form:"id"`
  15. }
  16. const prefix = "id-"
  17. parseIDs := func(q url.Values) []string {
  18. var ids []string
  19. for k, vs := range q {
  20. // Weed out bad keys, bad value lengths, and non-matching checkbox statuses.
  21. if pos := strings.Index(k, prefix); pos == -1 || len(vs) != 1 || vs[0] != "on" {
  22. continue
  23. }
  24. b, a, found := strings.Cut(k, prefix)
  25. if b == "" && found {
  26. ids = append(ids, a)
  27. }
  28. }
  29. return ids
  30. }
  31. return func(c *gin.Context) {
  32. qName := c.Param("name")
  33. ids := parseIDs(c.Request.URL.Query())
  34. sess := sessions.Default(c)
  35. flashes := sess.Flashes()
  36. token := csrf.GetToken(c)
  37. sess.Save()
  38. log.Println(ids)
  39. c.HTML(http.StatusOK, "confirm", gin.H{
  40. "flashes": flashes,
  41. "question": "Do you confirm this deletion request ?",
  42. "description": "These messages cannot be recovered after that step",
  43. "list": ids,
  44. "cancel": "Cancel",
  45. "confirm": "Delete",
  46. "csrf": token,
  47. "redirect": "/queue/" + qName,
  48. })
  49. }
  50. return nil
  51. }
  52. func makeDeleteConfirmHandler(rd redriver.Redriver) gin.HandlerFunc {
  53. return func(c *gin.Context) {
  54. c.JSON(http.StatusServiceUnavailable, gin.H{
  55. "message": "deletion confirmation",
  56. "error": "not implemented",
  57. })
  58. }
  59. }