rankingCalendar.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package routes
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "codeberg.org/vnpower/pixivfe/v2/core"
  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 Render(c, Data_rankingCalendar{Title: "Ranking calendar", Render: render, Mode: mode, Year: year, MonthBefore: parseDate(monthBefore), MonthAfter: parseDate(monthAfter), ThisMonth: parseDate(realDate)})
  67. }