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.

142 lines
3.5 KiB

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