You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
2.6 KiB

4 years ago
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/gissleh/stufflog/database"
  5. "github.com/gissleh/stufflog/internal/auth"
  6. "github.com/gissleh/stufflog/internal/generate"
  7. "github.com/gissleh/stufflog/internal/slerrors"
  8. "github.com/gissleh/stufflog/models"
  9. "github.com/gissleh/stufflog/services"
  10. "time"
  11. )
  12. func Log(g *gin.RouterGroup, db database.Database) {
  13. l := services.Loader{DB: db}
  14. g.GET("/", handler("logs", func(c *gin.Context) (interface{}, error) {
  15. filter := models.LogFilter{}
  16. if value := c.Query("minTime"); value != "" {
  17. minTime, err := time.Parse(time.RFC3339Nano, value)
  18. if err != nil {
  19. return nil, slerrors.BadRequest("Invalid minTime")
  20. }
  21. minTime = minTime.UTC()
  22. filter.MinTime = &minTime
  23. } else {
  24. lastMonth := time.Now().Add(-30 * 24 * time.Hour).UTC()
  25. filter.MinTime = &lastMonth
  26. }
  27. if value := c.Query("maxTime"); value != "" {
  28. maxTime, err := time.Parse(time.RFC3339Nano, value)
  29. if err != nil {
  30. return nil, slerrors.BadRequest("Invalid maxTime")
  31. }
  32. maxTime = maxTime.UTC()
  33. filter.MaxTime = &maxTime
  34. }
  35. return l.ListLogs(c, filter)
  36. }))
  37. g.GET("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  38. return l.FindLog(c, c.Param("id"))
  39. }))
  40. g.POST("/", handler("log", func(c *gin.Context) (interface{}, error) {
  41. log := models.Log{}
  42. err := c.BindJSON(&log)
  43. if err != nil {
  44. return nil, slerrors.BadRequest("Invalid JSON")
  45. }
  46. task, err := l.FindTask(c.Request.Context(), log.TaskID)
  47. if err != nil {
  48. return nil, err
  49. }
  50. log.ID = generate.LogID()
  51. log.UserID = auth.UserID(c)
  52. log.TaskID = task.ID
  53. log.ItemID = task.ItemID
  54. if log.LoggedTime.IsZero() {
  55. log.LoggedTime = time.Now().UTC()
  56. } else {
  57. log.LoggedTime = log.LoggedTime.UTC()
  58. }
  59. err = db.Logs().Insert(c.Request.Context(), log)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &models.LogResult{
  64. Log: log,
  65. Task: &task.Task,
  66. }, nil
  67. }))
  68. g.PUT("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  69. update := models.LogUpdate{}
  70. err := c.BindJSON(&update)
  71. if err != nil {
  72. return nil, slerrors.BadRequest("Invalid JSON")
  73. }
  74. log, err := l.FindLog(c.Request.Context(), c.Param("id"))
  75. if err != nil {
  76. return nil, err
  77. }
  78. log.Update(update)
  79. err = db.Logs().Update(c.Request.Context(), log.Log)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return log, nil
  84. }))
  85. g.DELETE("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  86. log, err := l.FindLog(c.Request.Context(), c.Param("id"))
  87. if err != nil {
  88. return nil, err
  89. }
  90. err = db.Logs().Delete(c.Request.Context(), log.Log)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return log, nil
  95. }))
  96. }