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.

127 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
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 logRepository struct {
  11. db *sqlx.DB
  12. }
  13. func (r *logRepository) Find(ctx context.Context, id string) (*models.Log, error) {
  14. res := models.Log{}
  15. err := r.db.GetContext(ctx, &res, "SELECT * FROM log WHERE log_id=$1", id)
  16. if err != nil {
  17. if err == sql.ErrNoRows {
  18. return nil, slerrors.NotFound("Log")
  19. }
  20. return nil, err
  21. }
  22. return &res, nil
  23. }
  24. func (r *logRepository) List(ctx context.Context, filter models.LogFilter) ([]*models.Log, error) {
  25. sq := squirrel.Select("log.*").From("log").PlaceholderFormat(squirrel.Dollar)
  26. sq = sq.Where(squirrel.Eq{"log.user_id": filter.UserID})
  27. if len(filter.TaskIDs) > 0 {
  28. sq = sq.Where(squirrel.Eq{"log.task_id": filter.TaskIDs})
  29. }
  30. if len(filter.ItemIDs) > 0 {
  31. sq = sq.Where(squirrel.Or{
  32. squirrel.Eq{"log.item_id": filter.ItemIDs},
  33. squirrel.Eq{"log.secondary_item_id": filter.ItemIDs},
  34. })
  35. }
  36. if filter.MinTime != nil {
  37. sq = sq.Where(squirrel.GtOrEq{
  38. "log.logged_time": *filter.MinTime,
  39. })
  40. }
  41. if filter.MaxTime != nil {
  42. sq = sq.Where(squirrel.LtOrEq{
  43. "log.logged_time": *filter.MaxTime,
  44. })
  45. }
  46. if len(filter.ProjectIDs) > 0 || len(filter.ProjectGroupIDs) > 0 {
  47. sq = sq.InnerJoin("task AS t ON t.task_id = log.task_id")
  48. sq = sq.InnerJoin("project AS p ON p.project_id = t.project_id")
  49. if len(filter.ProjectGroupIDs) > 0 {
  50. sq = sq.InnerJoin("project_group AS pg ON pg.project_group_id = p.project_group_id")
  51. sq = sq.Where(squirrel.Eq{"pg.project_group_id": filter.ProjectGroupIDs})
  52. }
  53. if len(filter.ProjectIDs) > 0 {
  54. sq = sq.Where(squirrel.Eq{"p.project_id": filter.ProjectIDs})
  55. }
  56. }
  57. sq = sq.OrderBy("logged_time")
  58. query, args, err := sq.ToSql()
  59. if err != nil {
  60. return nil, err
  61. }
  62. res := make([]*models.Log, 0, 8)
  63. err = r.db.SelectContext(ctx, &res, query, args...)
  64. if err != nil {
  65. if err == sql.ErrNoRows {
  66. return res, nil
  67. }
  68. return nil, err
  69. }
  70. return res, nil
  71. }
  72. func (r *logRepository) Insert(ctx context.Context, log models.Log) error {
  73. _, err := r.db.NamedExecContext(ctx, `
  74. INSERT INTO log (
  75. log_id, user_id, task_id, item_id, logged_time, description, item_amount, secondary_item_id, secondary_item_amount
  76. ) VALUES (
  77. :log_id, :user_id, :task_id, :item_id, :logged_time, :description, :item_amount, :secondary_item_id, :secondary_item_amount
  78. )
  79. `, &log)
  80. if err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. func (r *logRepository) Update(ctx context.Context, log models.Log) error {
  86. _, err := r.db.NamedExecContext(ctx, `
  87. UPDATE log SET
  88. logged_time=:logged_time,
  89. description=:description,
  90. item_amount=:item_amount,
  91. secondary_item_id=:secondary_item_id,
  92. secondary_item_amount=:secondary_item_amount
  93. WHERE log_id=:log_id
  94. `, &log)
  95. if err != nil {
  96. return err
  97. }
  98. return nil
  99. }
  100. func (r *logRepository) Delete(ctx context.Context, log models.Log) error {
  101. _, err := r.db.ExecContext(ctx, `DELETE FROM log WHERE log_id=$1`, log.ID)
  102. if err != nil {
  103. if err == sql.ErrNoRows {
  104. return slerrors.NotFound("Log")
  105. }
  106. return err
  107. }
  108. return nil
  109. }