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.

149 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. "time"
  11. )
  12. func Task(g *gin.RouterGroup, db database.Database) {
  13. l := services.Loader{DB: db}
  14. g.GET("/", handler("tasks", func(c *gin.Context) (interface{}, error) {
  15. filter := models.TaskFilter{}
  16. if setting := c.Query("active"); setting != "" {
  17. active := setting == "true"
  18. filter.Active = &active
  19. }
  20. if setting := c.Query("expiring"); setting != "" {
  21. expiring := setting == "true"
  22. filter.Expiring = &expiring
  23. }
  24. return l.ListTasks(c, filter)
  25. }))
  26. g.GET("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
  27. return l.FindTask(c, c.Param("id"))
  28. }))
  29. g.POST("/", handler("task", func(c *gin.Context) (interface{}, error) {
  30. createdTime := time.Now()
  31. task := models.Task{}
  32. err := c.BindJSON(&task)
  33. if err != nil {
  34. return nil, slerrors.BadRequest("Invalid JSON")
  35. }
  36. if task.EndTime != nil && task.EndTime.Before(createdTime) {
  37. return nil, slerrors.BadRequest("Task end time must be later than current time.")
  38. }
  39. if task.ItemAmount <= 0 {
  40. return nil, slerrors.BadRequest("Item amount cannot be zero or negative.")
  41. }
  42. project, err := l.FindProject(c.Request.Context(), task.ProjectID)
  43. if err != nil {
  44. return nil, err
  45. }
  46. item, err := l.FindItem(c.Request.Context(), task.ItemID)
  47. if err != nil {
  48. return nil, err
  49. }
  50. task.ID = generate.TaskID()
  51. task.UserID = auth.UserID(c)
  52. task.CreatedTime = time.Now().UTC()
  53. task.ItemID = item.ID
  54. task.ProjectID = project.ID
  55. err = db.Tasks().Insert(c.Request.Context(), task)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &models.TaskResult{
  60. Task: task,
  61. Logs: []*models.Log{},
  62. Item: &item.Item,
  63. CompletedAmount: 0,
  64. }, nil
  65. }))
  66. g.PUT("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
  67. update := models.TaskUpdate{}
  68. err := c.BindJSON(&update)
  69. if err != nil {
  70. return nil, slerrors.BadRequest("Invalid JSON")
  71. }
  72. task, err := l.FindTask(c.Request.Context(), c.Param("id"))
  73. if err != nil {
  74. return nil, err
  75. }
  76. if update.ItemID != nil && *update.ItemID != task.ItemID {
  77. _, err := l.FindItem(c.Request.Context(), *update.ItemID)
  78. if err != nil {
  79. return nil, err
  80. }
  81. logs, err := l.ListLogs(c.Request.Context(), models.LogFilter{
  82. TaskIDs: []string{task.ID},
  83. })
  84. if err != nil {
  85. return nil, err
  86. }
  87. if len(logs) > 0 {
  88. return nil, slerrors.Forbidden("You cannot change the item if the task has logs.")
  89. }
  90. }
  91. if update.ProjectID != nil && *update.ProjectID != task.ProjectID {
  92. project, err := l.FindProject(c.Request.Context(), *update.ProjectID)
  93. if err != nil {
  94. return nil, slerrors.NotFound("Destination project")
  95. }
  96. task.ProjectID = project.ID
  97. }
  98. task.Update(update)
  99. if task.EndTime != nil && task.EndTime.Before(task.CreatedTime) {
  100. return nil, slerrors.BadRequest("Task end time must be later than it was created.")
  101. }
  102. if task.ItemAmount <= 0 {
  103. return nil, slerrors.BadRequest("Item amount cannot be zero or negative.")
  104. }
  105. err = db.Tasks().Update(c.Request.Context(), task.Task)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return task, nil
  110. }))
  111. g.DELETE("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
  112. task, err := l.FindTask(c.Request.Context(), c.Param("id"))
  113. if err != nil {
  114. return nil, err
  115. }
  116. if len(task.Logs) > 0 {
  117. return nil, slerrors.Forbidden("cannot delete tasks with logs.")
  118. }
  119. err = db.Tasks().Delete(c.Request.Context(), task.Task)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return task, nil
  124. }))
  125. }