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.6 KiB

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