active.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package mint
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-pg/pg/v10"
  6. //"github.com/go-redis/redis/v8"
  7. "time"
  8. )
  9. type ActiveLogs struct {
  10. Id int `form:"id" json:"id" `
  11. UserId int `form:"user_id" json:"user_id" `
  12. ActiveType string `form:"active_type" json:"active_type"`
  13. Content string `form:"content" json:"content"`
  14. Timezone string `form:"timezone" json:"timezone"`
  15. CreatedAt time.Time
  16. UpdatedAt time.Time
  17. }
  18. type ActiveTimeFrames struct {
  19. Id int `form:"id" json:"id" `
  20. UserId int `form:"user_id" json:"user_id" `
  21. Hit int `form:"hit" json:"hit"`
  22. Timezone string `form:"timezone" json:"timezone"`
  23. CreatedAt time.Time
  24. UpdatedAt time.Time
  25. }
  26. type ActiveDayFrames struct {
  27. Id int `form:"id" json:"id" `
  28. UserId int `form:"user_id" json:"user_id" `
  29. Date time.Time
  30. Duration int `form:"duration" json:"duration"`
  31. hit int `form:"hit" json:"hit"`
  32. CreatedAt time.Time
  33. UpdatedAt time.Time
  34. }
  35. //display a list of all groups
  36. func ActiveIndex(db *pg.DB) gin.HandlerFunc {
  37. return func(c *gin.Context) {
  38. timerange := c.Query("range")
  39. userid := c.Query("userid")
  40. switch timerange {
  41. case "day":
  42. var dayFrames []ActiveDayFrames
  43. err := db.Model(&dayFrames).Column("id","date","Duration","hit").Where("user_id = ?",userid).Select()
  44. if err != nil {
  45. panic(err)
  46. }
  47. c.JSON(http.StatusOK, gin.H{
  48. "data": dayFrames,
  49. })
  50. case "frame":
  51. var timeFrames []ActiveTimeFrames
  52. err := db.Model(&timeFrames).Column("id","hit","timezone","created_at","updated_at").Where("user_id = ?",userid).Select()
  53. if err != nil {
  54. panic(err)
  55. }
  56. c.JSON(http.StatusOK, gin.H{
  57. "data": timeFrames,
  58. })
  59. case "event":
  60. start := c.Query("start")
  61. end := c.Query("end")
  62. var activeLogs []ActiveLogs
  63. err := db.Model(&activeLogs).Column("active_type","content","timezone","created_at").Where("user_id = ?",userid).Where("created_at > ?",start).Where("created_at < ?",end).Select()
  64. if err != nil {
  65. panic(err)
  66. }
  67. c.JSON(http.StatusOK, gin.H{
  68. "data": activeLogs,
  69. })
  70. }
  71. }
  72. }
  73. //create a new group
  74. func ActiveCreate(db *pg.DB) gin.HandlerFunc{
  75. return func(c *gin.Context){
  76. active_type := c.Query("active_type")
  77. content := c.Query("content")
  78. activeLogs := &ActiveLogs{
  79. ActiveType: active_type,
  80. Content: content,
  81. UserId:1,//TODO user_id
  82. }
  83. _, err := db.Model(activeLogs).Insert()
  84. if err != nil {
  85. panic(err)
  86. }
  87. //TODO 补充业务逻辑
  88. //建立成功
  89. c.JSON(http.StatusOK,gin.H{
  90. "message":"ok",
  91. })
  92. }
  93. }