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.

153 lines
3.8 KiB

4 years ago
4 years ago
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. "strings"
  11. "time"
  12. )
  13. func Log(g *gin.RouterGroup, db database.Database) {
  14. l := services.Loader{DB: db}
  15. g.GET("/", handler("logs", func(c *gin.Context) (interface{}, error) {
  16. filter := models.LogFilter{}
  17. if value := c.Query("minTime"); value != "" {
  18. minTime, err := time.Parse(time.RFC3339Nano, value)
  19. if err != nil {
  20. return nil, slerrors.BadRequest("Invalid minTime")
  21. }
  22. minTime = minTime.UTC()
  23. filter.MinTime = &minTime
  24. } else {
  25. lastMonth := time.Now().Add(-30 * 24 * time.Hour).UTC()
  26. filter.MinTime = &lastMonth
  27. }
  28. if value := c.Query("maxTime"); value != "" {
  29. maxTime, err := time.Parse(time.RFC3339Nano, value)
  30. if err != nil {
  31. return nil, slerrors.BadRequest("Invalid maxTime")
  32. }
  33. maxTime = maxTime.UTC()
  34. filter.MaxTime = &maxTime
  35. }
  36. if value := c.Query("groups"); value != "" {
  37. filter.ProjectGroupIDs = strings.Split(value, ",")
  38. }
  39. if value := c.Query("projects"); value != "" {
  40. filter.ProjectIDs = strings.Split(value, ",")
  41. }
  42. if value := c.Query("items"); value != "" {
  43. filter.ItemIDs = strings.Split(value, ",")
  44. }
  45. if value := c.Query("tasks"); value != "" {
  46. filter.TaskIDs = strings.Split(value, ",")
  47. }
  48. return l.ListLogs(c, filter)
  49. }))
  50. g.GET("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  51. return l.FindLog(c, c.Param("id"))
  52. }))
  53. g.POST("/", handler("log", func(c *gin.Context) (interface{}, error) {
  54. log := models.Log{}
  55. err := c.BindJSON(&log)
  56. if err != nil {
  57. return nil, slerrors.BadRequest("Invalid JSON")
  58. }
  59. task, err := l.FindTask(c.Request.Context(), log.TaskID)
  60. if err != nil {
  61. return nil, err
  62. }
  63. log.ID = generate.LogID()
  64. log.UserID = auth.UserID(c)
  65. log.TaskID = task.ID
  66. log.ItemID = task.ItemID
  67. if log.LoggedTime.IsZero() {
  68. log.LoggedTime = time.Now().UTC()
  69. } else {
  70. log.LoggedTime = log.LoggedTime.UTC()
  71. }
  72. if log.ItemAmount < 0 {
  73. return nil, slerrors.BadRequest("Invalid item amount (min: 0).")
  74. }
  75. if log.SecondaryItemAmount < 0 {
  76. return nil, slerrors.BadRequest("Invalid secondary item amount (min: 0).")
  77. }
  78. if log.SecondaryItemID != nil {
  79. _, err := l.FindItem(c.Request.Context(), *log.SecondaryItemID)
  80. if err != nil {
  81. return nil, slerrors.BadRequest("Item could not be found.")
  82. }
  83. }
  84. err = db.Logs().Insert(c.Request.Context(), log)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return l.FindLog(c.Request.Context(), log.ID)
  89. }))
  90. g.PUT("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  91. update := models.LogUpdate{}
  92. err := c.BindJSON(&update)
  93. if err != nil {
  94. return nil, slerrors.BadRequest("Invalid JSON")
  95. }
  96. log, err := l.FindLog(c.Request.Context(), c.Param("id"))
  97. if err != nil {
  98. return nil, err
  99. }
  100. log.Update(update)
  101. if log.SecondaryItemID != nil && update.SecondaryItemID != nil {
  102. _, err := l.FindItem(c.Request.Context(), *log.SecondaryItemID)
  103. if err != nil {
  104. return nil, slerrors.BadRequest("Item could not be found.")
  105. }
  106. }
  107. if log.ItemAmount < 0 {
  108. return nil, slerrors.BadRequest("Invalid item amount (min: 0).")
  109. }
  110. if log.SecondaryItemAmount < 0 {
  111. return nil, slerrors.BadRequest("Invalid secondary item amount (min: 0).")
  112. }
  113. err = db.Logs().Update(c.Request.Context(), log.Log)
  114. if err != nil {
  115. return nil, err
  116. }
  117. return log, nil
  118. }))
  119. g.DELETE("/:id", handler("log", func(c *gin.Context) (interface{}, error) {
  120. log, err := l.FindLog(c.Request.Context(), c.Param("id"))
  121. if err != nil {
  122. return nil, err
  123. }
  124. err = db.Logs().Delete(c.Request.Context(), log.Log)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return log, nil
  129. }))
  130. }