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.

115 lines
2.5 KiB

4 years ago
  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/Masterminds/squirrel"
  6. "github.com/gissleh/stufflog/internal/slerrors"
  7. "github.com/gissleh/stufflog/models"
  8. "github.com/jmoiron/sqlx"
  9. )
  10. type goalRepository struct {
  11. db *sqlx.DB
  12. }
  13. func (r *goalRepository) Find(ctx context.Context, id string) (*models.Goal, error) {
  14. res := models.Goal{}
  15. err := r.db.GetContext(ctx, &res, "SELECT * FROM goal WHERE goal_id=$1", id)
  16. if err != nil {
  17. if err == sql.ErrNoRows {
  18. return nil, slerrors.NotFound("Goal")
  19. }
  20. return nil, err
  21. }
  22. return &res, nil
  23. }
  24. func (r *goalRepository) List(ctx context.Context, filter models.GoalFilter) ([]*models.Goal, error) {
  25. sq := squirrel.Select("*").From("goal").PlaceholderFormat(squirrel.Dollar)
  26. sq = sq.Where(squirrel.Eq{"user_id": filter.UserID})
  27. if len(filter.IDs) > 0 {
  28. sq = sq.Where(squirrel.Eq{"goal_id": filter.IDs})
  29. }
  30. if filter.MinTime != nil {
  31. sq = sq.Where(squirrel.GtOrEq{
  32. "end_time": *filter.MinTime,
  33. })
  34. }
  35. if filter.MaxTime != nil {
  36. sq = sq.Where(squirrel.LtOrEq{
  37. "start_time": *filter.MaxTime,
  38. })
  39. }
  40. if filter.IncludesTime != nil {
  41. sq = sq.Where(squirrel.LtOrEq{
  42. "start_time": *filter.IncludesTime,
  43. }).Where(squirrel.GtOrEq{
  44. "end_time": *filter.IncludesTime,
  45. })
  46. }
  47. sq = sq.OrderBy("start_time", "end_time", "name")
  48. query, args, err := sq.ToSql()
  49. if err != nil {
  50. return nil, err
  51. }
  52. res := make([]*models.Goal, 0, 8)
  53. err = r.db.SelectContext(ctx, &res, query, args...)
  54. if err != nil {
  55. if err == sql.ErrNoRows {
  56. return res, nil
  57. }
  58. return nil, err
  59. }
  60. return res, nil
  61. }
  62. func (r *goalRepository) Insert(ctx context.Context, goal models.Goal) error {
  63. _, err := r.db.NamedExecContext(ctx, `
  64. INSERT INTO goal (
  65. goal_id, user_id, group_id, amount, start_time, end_time, name, description
  66. ) VALUES (
  67. :goal_id, :user_id, :group_id, :amount, :start_time, :end_time, :name, :description
  68. )
  69. `, &goal)
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. func (r *goalRepository) Update(ctx context.Context, goal models.Goal) error {
  76. _, err := r.db.NamedExecContext(ctx, `
  77. UPDATE goal SET
  78. amount=:amount,
  79. start_time=:start_time,
  80. end_time=:end_time,
  81. name=:name,
  82. description=:description
  83. WHERE goal_id=:goal_id
  84. `, &goal)
  85. if err != nil {
  86. return err
  87. }
  88. return nil
  89. }
  90. func (r *goalRepository) Delete(ctx context.Context, goal models.Goal) error {
  91. _, err := r.db.ExecContext(ctx, `DELETE FROM goal WHERE goal_id=$1`, goal.ID)
  92. if err != nil {
  93. if err == sql.ErrNoRows {
  94. return slerrors.NotFound("Goal")
  95. }
  96. return err
  97. }
  98. return nil
  99. }