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.

123 lines
3.1 KiB

4 years ago
4 years ago
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 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("active DESC", "status_tag ASC", "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, status_tag
  66. ) VALUES (
  67. :task_id, :user_id, :item_id, :project_id, :item_amount, :name, :description, :active, :created_time, :end_time, :status_tag
  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. status_tag = :status_tag,
  85. project_id = :project_id
  86. WHERE task_id=:task_id
  87. `, &task)
  88. if err != nil {
  89. return err
  90. }
  91. _, err = r.db.NamedExecContext(ctx, `UPDATE log SET item_id = :item_id WHERE task_id=:task_id`, &task)
  92. if err != nil {
  93. return err
  94. }
  95. return nil
  96. }
  97. func (r *taskRepository) Delete(ctx context.Context, task models.Task) error {
  98. _, err := r.db.ExecContext(ctx, `DELETE FROM task WHERE task_id=$1`, task.ID)
  99. if err != nil {
  100. if err == sql.ErrNoRows {
  101. return slerrors.NotFound("Task")
  102. }
  103. return err
  104. }
  105. return nil
  106. }