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.

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