rankingCalendar.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package pages
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. core "codeberg.org/vnpower/pixivfe/v2/core/webapi"
  7. "github.com/gofiber/fiber/v2"
  8. )
  9. type DateWrap struct {
  10. Link string
  11. Year int
  12. Month int
  13. MonthPadded string
  14. MonthLiteral string
  15. }
  16. func parseDate(t time.Time) DateWrap {
  17. var d DateWrap
  18. year := t.Year()
  19. month := t.Month()
  20. monthPadded := fmt.Sprintf("%02d", month)
  21. d.Link = fmt.Sprintf("%d-%s-01", year, monthPadded)
  22. d.Year = year
  23. d.Month = int(month)
  24. d.MonthPadded = monthPadded
  25. d.MonthLiteral = month.String()
  26. return d
  27. }
  28. func RankingCalendarPicker(c *fiber.Ctx) error {
  29. mode := c.FormValue("mode", "daily")
  30. date := c.FormValue("date", "")
  31. return c.RedirectToRoute("/rankingCalendar", fiber.Map{
  32. "queries": map[string]string{
  33. "mode": mode,
  34. "date": date,
  35. },
  36. })
  37. }
  38. func RankingCalendarPage(c *fiber.Ctx) error {
  39. mode := c.Query("mode", "daily")
  40. date := c.Query("date", "")
  41. var year int
  42. var month int
  43. // If the user supplied a date
  44. if len(date) == 10 {
  45. var err error
  46. year, err = strconv.Atoi(date[:4])
  47. if err != nil {
  48. return err
  49. }
  50. month, err = strconv.Atoi(date[5:7])
  51. if err != nil {
  52. return err
  53. }
  54. } else {
  55. now := c.Context().Time()
  56. year = now.Year()
  57. month = int(now.Month())
  58. }
  59. realDate := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
  60. monthBefore := realDate.AddDate(0, -1, 0)
  61. monthAfter := realDate.AddDate(0, 1, 0)
  62. render, err := core.GetRankingCalendar(c, mode, year, month)
  63. if err != nil {
  64. return err
  65. }
  66. return c.Render("rankingCalendar", fiber.Map{
  67. "Title": "Ranking calendar",
  68. "Render": render,
  69. "Mode": mode,
  70. "Year": year,
  71. "MonthBefore": parseDate(monthBefore),
  72. "MonthAfter": parseDate(monthAfter),
  73. "ThisMonth": parseDate(realDate),
  74. })
  75. }