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.

105 lines
2.6 KiB

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