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.

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