rankingCalendar.go 1.8 KB

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