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.

111 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 Task(g *gin.RouterGroup, db database.Database) {
  13. l := services.Loader{DB: db}
  14. defaultActive := true
  15. g.GET("/", handler("tasks", func(c *gin.Context) (interface{}, error) {
  16. filter := models.TaskFilter{}
  17. if setting := c.Query("active"); setting != "" {
  18. active := setting == "true"
  19. filter.Active = &active
  20. } else {
  21. filter.Active = &defaultActive
  22. }
  23. return l.ListTasks(c, filter)
  24. }))
  25. g.GET("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
  26. return l.FindTask(c, c.Param("id"))
  27. }))
  28. g.POST("/", handler("task", func(c *gin.Context) (interface{}, error) {
  29. task := models.Task{}
  30. err := c.BindJSON(&task)
  31. if err != nil {
  32. return nil, slerrors.BadRequest("Invalid JSON")
  33. }
  34. if task.EndTime != nil && task.EndTime.Before(time.Now()) {
  35. return nil, slerrors.BadRequest("Project end time must be later than current time.")
  36. }
  37. project, err := l.FindProject(c.Request.Context(), task.ProjectID)
  38. if err != nil {
  39. return nil, err
  40. }
  41. item, err := l.FindItem(c.Request.Context(), task.ItemID)
  42. if err != nil {
  43. return nil, err
  44. }
  45. task.ID = generate.TaskID()
  46. task.UserID = auth.UserID(c)
  47. task.CreatedTime = time.Now().UTC()
  48. task.ItemID = item.ID
  49. task.ProjectID = project.ID
  50. err = db.Tasks().Insert(c.Request.Context(), task)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return &models.TaskResult{
  55. Task: task,
  56. Logs: []*models.Log{},
  57. Item: &item.Item,
  58. CompletedAmount: 0,
  59. }, nil
  60. }))
  61. g.PUT("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
  62. update := models.TaskUpdate{}
  63. err := c.BindJSON(&update)
  64. if err != nil {
  65. return nil, slerrors.BadRequest("Invalid JSON")
  66. }
  67. task, err := l.FindTask(c.Request.Context(), c.Param("id"))
  68. if err != nil {
  69. return nil, err
  70. }
  71. task.Update(update)
  72. err = db.Tasks().Update(c.Request.Context(), task.Task)
  73. if err != nil {
  74. return nil, err
  75. }
  76. return task, nil
  77. }))
  78. g.DELETE("/:id", handler("task", func(c *gin.Context) (interface{}, error) {
  79. task, err := l.FindTask(c.Request.Context(), c.Param("id"))
  80. if err != nil {
  81. return nil, err
  82. }
  83. if len(task.Logs) > 0 {
  84. return nil, slerrors.Forbidden("cannot delete tasks with logs.")
  85. }
  86. err = db.Tasks().Delete(c.Request.Context(), task.Task)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return task, nil
  91. }))
  92. }