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.

132 lines
3.6 KiB

4 years ago
4 years ago
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 Project(g *gin.RouterGroup, db database.Database) {
  14. l := services.Loader{DB: db}
  15. g.GET("/", handler("projects", func(c *gin.Context) (interface{}, error) {
  16. filter := models.ProjectFilter{}
  17. if setting := c.Query("active"); setting != "" {
  18. active := setting == "true"
  19. filter.Active = &active
  20. }
  21. if setting := c.Query("include-semi-active"); setting != "" {
  22. filter.IncludeSemiActive = setting == "true"
  23. }
  24. if setting := c.Query("expiring"); setting != "" {
  25. filter.Expiring = setting == "true"
  26. }
  27. if setting := c.Query("favorite"); setting != "" {
  28. favorite := setting == "true"
  29. filter.Favorite = &favorite
  30. }
  31. if setting := c.Query("ungrouped"); setting != "" {
  32. filter.Ungrouped = setting == "true"
  33. }
  34. if setting := c.Query("groups"); setting != "" {
  35. filter.ProjectGroupIDs = strings.Split(setting, ",")
  36. }
  37. return l.ListProjects(c, filter)
  38. }))
  39. g.GET("/:id", handler("project", func(c *gin.Context) (interface{}, error) {
  40. return l.FindProject(c, c.Param("id"))
  41. }))
  42. g.POST("/", handler("project", func(c *gin.Context) (interface{}, error) {
  43. project := models.Project{}
  44. err := c.BindJSON(&project)
  45. if err != nil {
  46. return nil, slerrors.BadRequest("Invalid JSON")
  47. }
  48. if project.EndTime != nil && project.EndTime.Before(time.Now()) {
  49. return nil, slerrors.BadRequest("Project end time must be later than current time.")
  50. }
  51. if project.StartTime != nil && (project.EndTime == nil || !project.StartTime.Before(*project.EndTime)) {
  52. return nil, slerrors.BadRequest("Project start time must be before end time.")
  53. }
  54. project.ID = generate.ProjectID()
  55. project.UserID = auth.UserID(c)
  56. project.CreatedTime = time.Now().UTC()
  57. if project.GroupID != nil && *project.GroupID == "" {
  58. project.GroupID = nil
  59. }
  60. err = db.Projects().Insert(c.Request.Context(), project)
  61. if err != nil {
  62. return nil, err
  63. }
  64. if project.Active && project.StatusTag != nil {
  65. project.StatusTag = nil
  66. }
  67. if project.SubtractAmount < 0 {
  68. project.SubtractAmount = 0
  69. }
  70. return &models.ProjectResult{
  71. Project: project,
  72. Tasks: []*models.TaskResult{},
  73. }, nil
  74. }))
  75. g.PUT("/:id", handler("project", func(c *gin.Context) (interface{}, error) {
  76. update := models.ProjectUpdate{}
  77. err := c.BindJSON(&update)
  78. if err != nil {
  79. return nil, slerrors.BadRequest("Invalid JSON")
  80. }
  81. project, err := l.FindProject(c.Request.Context(), c.Param("id"))
  82. if err != nil {
  83. return nil, err
  84. }
  85. project.Update(update)
  86. if project.EndTime != nil && project.StartTime != nil && (project.EndTime == nil || !project.StartTime.Before(*project.EndTime)) {
  87. return nil, slerrors.BadRequest("Project start time must be before end time.")
  88. } else {
  89. project.StartTime = nil
  90. }
  91. err = db.Projects().Update(c.Request.Context(), project.Project)
  92. if err != nil {
  93. return nil, err
  94. }
  95. return project, nil
  96. }))
  97. g.DELETE("/:id", handler("project", func(c *gin.Context) (interface{}, error) {
  98. project, err := l.FindProject(c.Request.Context(), c.Param("id"))
  99. if err != nil {
  100. return nil, err
  101. }
  102. if len(project.Tasks) > 0 {
  103. return nil, slerrors.Forbidden("cannot delete non-empty projects.")
  104. }
  105. err = db.Projects().Delete(c.Request.Context(), project.Project)
  106. if err != nil {
  107. return nil, err
  108. }
  109. return project, nil
  110. }))
  111. }