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.

122 lines
2.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
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 DESC", "end_time ASC", "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. composition_mode, unweighted, item_id, task_filter, item_filter
  67. ) VALUES (
  68. :goal_id, :user_id, :group_id, :amount, :start_time, :end_time, :name, :description,
  69. :composition_mode, :unweighted, :item_id, :task_filter, :item_filter
  70. )
  71. `, &goal)
  72. if err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. func (r *goalRepository) Update(ctx context.Context, goal models.Goal) error {
  78. _, err := r.db.NamedExecContext(ctx, `
  79. UPDATE goal SET
  80. amount=:amount,
  81. start_time=:start_time,
  82. end_time=:end_time,
  83. name=:name,
  84. description=:description,
  85. composition_mode=:composition_mode,
  86. unweighted=:unweighted,
  87. item_id=:item_id,
  88. task_filter=:task_filter,
  89. item_filter=:item_filter
  90. WHERE goal_id=:goal_id
  91. `, &goal)
  92. if err != nil {
  93. return err
  94. }
  95. return nil
  96. }
  97. func (r *goalRepository) Delete(ctx context.Context, goal models.Goal) error {
  98. _, err := r.db.ExecContext(ctx, `DELETE FROM goal WHERE goal_id=$1`, goal.ID)
  99. if err != nil {
  100. if err == sql.ErrNoRows {
  101. return slerrors.NotFound("Goal")
  102. }
  103. return err
  104. }
  105. return nil
  106. }