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.

108 lines
2.8 KiB

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. project.ID = generate.ProjectID()
  45. project.UserID = auth.UserID(c)
  46. project.CreatedTime = time.Now().UTC()
  47. err = db.Projects().Insert(c.Request.Context(), project)
  48. if err != nil {
  49. return nil, err
  50. }
  51. if project.Active && project.StatusTag != nil {
  52. project.StatusTag = nil
  53. }
  54. return &models.ProjectResult{
  55. Project: project,
  56. Tasks: []*models.TaskResult{},
  57. }, nil
  58. }))
  59. g.PUT("/:id", handler("project", func(c *gin.Context) (interface{}, error) {
  60. update := models.ProjectUpdate{}
  61. err := c.BindJSON(&update)
  62. if err != nil {
  63. return nil, slerrors.BadRequest("Invalid JSON")
  64. }
  65. project, err := l.FindProject(c.Request.Context(), c.Param("id"))
  66. if err != nil {
  67. return nil, err
  68. }
  69. project.Update(update)
  70. err = db.Projects().Update(c.Request.Context(), project.Project)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return project, nil
  75. }))
  76. g.DELETE("/:id", handler("project", func(c *gin.Context) (interface{}, error) {
  77. project, err := l.FindProject(c.Request.Context(), c.Param("id"))
  78. if err != nil {
  79. return nil, err
  80. }
  81. if len(project.Tasks) > 0 {
  82. return nil, slerrors.Forbidden("cannot delete non-empty projects.")
  83. }
  84. err = db.Projects().Delete(c.Request.Context(), project.Project)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return project, nil
  89. }))
  90. }