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.

109 lines
2.7 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 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.IDs != nil {
  31. sq = sq.Where(squirrel.Eq{"task.task_id": filter.IDs})
  32. }
  33. if filter.ItemIDs != nil {
  34. sq = sq.Where(squirrel.Eq{"task.item_id": filter.ItemIDs})
  35. }
  36. if filter.ProjectIDs != nil {
  37. sq = sq.Where(squirrel.Eq{"task.project_id": filter.ProjectIDs})
  38. }
  39. sq = sq.InnerJoin("project AS p ON task.project_id = p.project_id")
  40. sq = sq.OrderBy("created_time")
  41. query, args, err := sq.ToSql()
  42. if err != nil {
  43. return nil, err
  44. }
  45. res := make([]*models.Task, 0, 8)
  46. err = r.db.SelectContext(ctx, &res, query, args...)
  47. if err != nil {
  48. if err == sql.ErrNoRows {
  49. return res, nil
  50. }
  51. return nil, err
  52. }
  53. return res, nil
  54. }
  55. func (r *taskRepository) Insert(ctx context.Context, task models.Task) error {
  56. _, err := r.db.NamedExecContext(ctx, `
  57. INSERT INTO task (
  58. task_id, user_id, item_id, project_id, item_amount, name, description, active, created_time, end_time
  59. ) VALUES (
  60. :task_id, :user_id, :item_id, :project_id, :item_amount, :name, :description, :active, :created_time, :end_time
  61. )
  62. `, &task)
  63. if err != nil {
  64. return err
  65. }
  66. return nil
  67. }
  68. func (r *taskRepository) Update(ctx context.Context, task models.Task) error {
  69. _, err := r.db.NamedExecContext(ctx, `
  70. UPDATE task SET
  71. item_id = :item_id,
  72. item_amount = :item_amount,
  73. name = :name,
  74. description = :description,
  75. active = :active,
  76. end_time = :end_time
  77. WHERE task_id=:task_id
  78. `, &task)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }
  84. func (r *taskRepository) Delete(ctx context.Context, task models.Task) error {
  85. _, err := r.db.ExecContext(ctx, `DELETE FROM task WHERE task_id=$1`, task.ID)
  86. if err != nil {
  87. if err == sql.ErrNoRows {
  88. return slerrors.NotFound("Task")
  89. }
  90. return err
  91. }
  92. return nil
  93. }