location.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package helpers
  2. import (
  3. "time"
  4. "github.com/gofiber/fiber/v2"
  5. "git.dmitriygnatenko.ru/dima/homethings/internal/middleware/timezone"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  7. )
  8. func ApplyLocation[T interface{}](fctx *fiber.Ctx, req T) T {
  9. tz, ok := fctx.Locals(timezone.CtxTimezoneKey).(string)
  10. if !ok {
  11. return req
  12. }
  13. loc, err := time.LoadLocation(tz)
  14. if err != nil {
  15. return req
  16. }
  17. switch v := any(req).(type) {
  18. case []models.Place:
  19. for i := range v {
  20. v[i].CreatedAt = v[i].CreatedAt.In(loc)
  21. v[i].UpdatedAt = v[i].UpdatedAt.In(loc)
  22. }
  23. case []models.Thing:
  24. for i := range v {
  25. v[i].CreatedAt = v[i].CreatedAt.In(loc)
  26. v[i].UpdatedAt = v[i].UpdatedAt.In(loc)
  27. }
  28. case []models.Tag:
  29. for i := range v {
  30. v[i].CreatedAt = v[i].CreatedAt.In(loc)
  31. v[i].UpdatedAt = v[i].UpdatedAt.In(loc)
  32. }
  33. case []models.Image:
  34. for i := range v {
  35. v[i].CreatedAt = v[i].CreatedAt.In(loc)
  36. }
  37. case []models.ExtThingNotification:
  38. for i := range v {
  39. v[i].CreatedAt = v[i].CreatedAt.In(loc)
  40. v[i].UpdatedAt = v[i].UpdatedAt.In(loc)
  41. v[i].NotificationDate = v[i].NotificationDate.In(loc)
  42. }
  43. case *models.Thing:
  44. v.CreatedAt = v.CreatedAt.In(loc)
  45. v.UpdatedAt = v.UpdatedAt.In(loc)
  46. case *models.Place:
  47. v.CreatedAt = v.CreatedAt.In(loc)
  48. v.UpdatedAt = v.UpdatedAt.In(loc)
  49. case *models.ThingTag:
  50. v.CreatedAt = v.CreatedAt.In(loc)
  51. v.UpdatedAt = v.UpdatedAt.In(loc)
  52. case *models.Tag:
  53. v.CreatedAt = v.CreatedAt.In(loc)
  54. v.UpdatedAt = v.UpdatedAt.In(loc)
  55. case *models.Image:
  56. v.CreatedAt = v.CreatedAt.In(loc)
  57. case *models.ThingNotification:
  58. v.CreatedAt = v.CreatedAt.In(loc)
  59. v.UpdatedAt = v.UpdatedAt.In(loc)
  60. v.NotificationDate = v.NotificationDate.In(loc)
  61. }
  62. return req
  63. }