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.

139 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
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. if log.ItemAmount < 0 {
  60. return nil, slerrors.BadRequest("Invalid item amount (min: 0).")
  61. }
  62. if log.SecondaryItemAmount < 0 {
  63. return nil, slerrors.BadRequest("Invalid secondary item amount (min: 0).")
  64. }
  65. if log.SecondaryItemID != nil {
  66. _, err := l.FindItem(c.Request.Context(), *log.SecondaryItemID)
  67. if err != nil {
  68. return nil, slerrors.BadRequest("Item could not be found.")
  69. }
  70. }
  71. err = db.Logs().Insert(c.Request.Context(), log)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return l.FindLog(c.Request.Context(), log.ID)
  76. }))
  77. g.PUT("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  78. update := models.LogUpdate{}
  79. err := c.BindJSON(&update)
  80. if err != nil {
  81. return nil, slerrors.BadRequest("Invalid JSON")
  82. }
  83. log, err := l.FindLog(c.Request.Context(), c.Param("id"))
  84. if err != nil {
  85. return nil, err
  86. }
  87. log.Update(update)
  88. if log.SecondaryItemID != nil && update.SecondaryItemID != nil {
  89. _, err := l.FindItem(c.Request.Context(), *log.SecondaryItemID)
  90. if err != nil {
  91. return nil, slerrors.BadRequest("Item could not be found.")
  92. }
  93. }
  94. if log.ItemAmount < 0 {
  95. return nil, slerrors.BadRequest("Invalid item amount (min: 0).")
  96. }
  97. if log.SecondaryItemAmount < 0 {
  98. return nil, slerrors.BadRequest("Invalid secondary item amount (min: 0).")
  99. }
  100. err = db.Logs().Update(c.Request.Context(), log.Log)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return log, nil
  105. }))
  106. g.DELETE("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  107. log, err := l.FindLog(c.Request.Context(), c.Param("id"))
  108. if err != nil {
  109. return nil, err
  110. }
  111. err = db.Logs().Delete(c.Request.Context(), log.Log)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return log, nil
  116. }))
  117. }