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.

141 lines
3.6 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 Goal(g *gin.RouterGroup, db database.Database) {
  13. l := services.Loader{DB: db}
  14. g.GET("/", handler("goals", func(c *gin.Context) (interface{}, error) {
  15. filter := models.GoalFilter{}
  16. if value := c.Query("minTime"); value != "" {
  17. minTime, err := time.Parse(time.RFC3339Nano, value)
  18. if err != nil {
  19. return nil, slerrors.BadRequest("Invalid minTime")
  20. }
  21. minTime = minTime.UTC()
  22. filter.MinTime = &minTime
  23. } else {
  24. lastMonth := time.Now().Add(-30 * 24 * time.Hour).UTC()
  25. filter.MinTime = &lastMonth
  26. }
  27. if value := c.Query("maxTime"); value != "" {
  28. maxTime, err := time.Parse(time.RFC3339Nano, value)
  29. if err != nil {
  30. return nil, slerrors.BadRequest("Invalid maxTime")
  31. }
  32. maxTime = maxTime.UTC()
  33. filter.MaxTime = &maxTime
  34. }
  35. if value := c.Query("includesTime"); value != "" {
  36. includesTime, err := time.Parse(time.RFC3339Nano, value)
  37. if err != nil {
  38. return nil, slerrors.BadRequest("Invalid includesTime")
  39. }
  40. includesTime = includesTime.UTC()
  41. filter.IncludesTime = &includesTime
  42. }
  43. return l.ListGoals(c.Request.Context(), filter)
  44. }))
  45. g.GET("/:id", handler("goal", func(c *gin.Context) (interface{}, error) {
  46. return l.FindGoal(c.Request.Context(), c.Param("id"))
  47. }))
  48. g.POST("/", handler("goal", func(c *gin.Context) (interface{}, error) {
  49. goal := models.Goal{}
  50. err := c.BindJSON(&goal)
  51. if err != nil {
  52. return nil, slerrors.BadRequest("Invalid JSON")
  53. }
  54. goal.ID = generate.GoalID()
  55. goal.UserID = auth.UserID(c)
  56. if goal.Amount <= 0 {
  57. return nil, slerrors.BadRequest("Goal amount must be more than 0.")
  58. }
  59. if goal.StartTime.IsZero() {
  60. return nil, slerrors.BadRequest("Start time must be set.")
  61. }
  62. if goal.EndTime.IsZero() {
  63. return nil, slerrors.BadRequest("End time must be set.")
  64. }
  65. if !goal.EndTime.After(goal.StartTime) {
  66. return nil, slerrors.BadRequest("Start time must be before end time.")
  67. }
  68. goal.StartTime = goal.StartTime.UTC()
  69. goal.EndTime = goal.EndTime.UTC()
  70. group, err := db.Groups().Find(c.Request.Context(), goal.GroupID)
  71. if err != nil {
  72. return nil, err
  73. }
  74. goal.GroupID = group.ID
  75. err = db.Goals().Insert(c.Request.Context(), goal)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return l.FindGoal(c.Request.Context(), goal.ID)
  80. }))
  81. g.PUT("/:id", handler("goal", func(c *gin.Context) (interface{}, error) {
  82. update := models.GoalUpdate{}
  83. err := c.BindJSON(&update)
  84. if err != nil {
  85. return nil, slerrors.BadRequest("Invalid JSON")
  86. }
  87. goal, err := l.FindGoal(c.Request.Context(), c.Param("id"))
  88. if err != nil {
  89. return nil, err
  90. }
  91. goal.StartTime = goal.StartTime.UTC()
  92. goal.EndTime = goal.EndTime.UTC()
  93. goal.Update(update)
  94. if goal.Amount <= 0 {
  95. return nil, slerrors.BadRequest("Goal amount must be more than 0.")
  96. }
  97. if !goal.EndTime.After(goal.StartTime) {
  98. return nil, slerrors.BadRequest("Start time must be before end time.")
  99. }
  100. err = db.Goals().Update(c.Request.Context(), goal.Goal)
  101. if err != nil {
  102. return nil, err
  103. }
  104. return goal, nil
  105. }))
  106. g.DELETE("/:id", handler("goal", func(c *gin.Context) (interface{}, error) {
  107. goal, err := l.FindGoal(c.Request.Context(), c.Param("id"))
  108. if err != nil {
  109. return nil, err
  110. }
  111. err = db.Goals().Delete(c.Request.Context(), goal.Goal)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return goal, nil
  116. }))
  117. }