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.

116 lines
2.8 KiB

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 taskRepository struct {
  11. db *sqlx.DB
  12. }
  13. func (r *taskRepository) Find(ctx context.Context, id string) (*models.Task, error) {
  14. res := models.Task{}
  15. err := r.db.GetContext(ctx, &res, "SELECT task.*, p.icon FROM task INNER JOIN project AS p ON task.project_id = p.project_id WHERE task_id=$1", id)
  16. if err != nil {
  17. if err == sql.ErrNoRows {
  18. return nil, slerrors.NotFound("Task")
  19. }
  20. return nil, err
  21. }
  22. return &res, nil
  23. }
  24. func (r *taskRepository) List(ctx context.Context, filter models.TaskFilter) ([]*models.Task, error) {
  25. sq := squirrel.Select("task.*", "p.icon").From("task").PlaceholderFormat(squirrel.Dollar)
  26. sq = sq.Where(squirrel.Eq{"task.user_id": filter.UserID})
  27. if filter.Active != nil {
  28. sq = sq.Where(squirrel.Eq{"task.active": *filter.Active})
  29. }
  30. if filter.Expiring != nil {
  31. if *filter.Expiring {
  32. sq = sq.Where("task.end_time IS NOT NULL")
  33. } else {
  34. sq = sq.Where("task.end_time IS NULL")
  35. }
  36. }
  37. if filter.IDs != nil {
  38. sq = sq.Where(squirrel.Eq{"task.task_id": filter.IDs})
  39. }
  40. if filter.ItemIDs != nil {
  41. sq = sq.Where(squirrel.Eq{"task.item_id": filter.ItemIDs})
  42. }
  43. if filter.ProjectIDs != nil {
  44. sq = sq.Where(squirrel.Eq{"task.project_id": filter.ProjectIDs})
  45. }
  46. sq = sq.InnerJoin("project AS p ON task.project_id = p.project_id")
  47. sq = sq.OrderBy("created_time")
  48. query, args, err := sq.ToSql()
  49. if err != nil {
  50. return nil, err
  51. }
  52. res := make([]*models.Task, 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 *taskRepository) Insert(ctx context.Context, task models.Task) error {
  63. _, err := r.db.NamedExecContext(ctx, `
  64. INSERT INTO task (
  65. task_id, user_id, item_id, project_id, item_amount, name, description, active, created_time, end_time
  66. ) VALUES (
  67. :task_id, :user_id, :item_id, :project_id, :item_amount, :name, :description, :active, :created_time, :end_time
  68. )
  69. `, &task)
  70. if err != nil {
  71. return err
  72. }
  73. return nil
  74. }
  75. func (r *taskRepository) Update(ctx context.Context, task models.Task) error {
  76. _, err := r.db.NamedExecContext(ctx, `
  77. UPDATE task SET
  78. item_id = :item_id,
  79. item_amount = :item_amount,
  80. name = :name,
  81. description = :description,
  82. active = :active,
  83. end_time = :end_time
  84. WHERE task_id=:task_id
  85. `, &task)
  86. if err != nil {
  87. return err
  88. }
  89. return nil
  90. }
  91. func (r *taskRepository) Delete(ctx context.Context, task models.Task) error {
  92. _, err := r.db.ExecContext(ctx, `DELETE FROM task WHERE task_id=$1`, task.ID)
  93. if err != nil {
  94. if err == sql.ErrNoRows {
  95. return slerrors.NotFound("Task")
  96. }
  97. return err
  98. }
  99. return nil
  100. }