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.

114 lines
2.6 KiB

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{"user_id": filter.UserID})
  27. if len(filter.TaskIDs) > 0 {
  28. sq = sq.Where(squirrel.Eq{"task_id": filter.TaskIDs})
  29. }
  30. if len(filter.ItemIDs) > 0 {
  31. sq = sq.Where(squirrel.Or{
  32. squirrel.Eq{"item_id": filter.ItemIDs},
  33. squirrel.Eq{"secondary_item_id": filter.ItemIDs},
  34. })
  35. }
  36. if filter.MinTime != nil {
  37. sq = sq.Where(squirrel.GtOrEq{
  38. "logged_time": *filter.MinTime,
  39. })
  40. }
  41. if filter.MaxTime != nil {
  42. sq = sq.Where(squirrel.LtOrEq{
  43. "logged_time": *filter.MaxTime,
  44. })
  45. }
  46. sq = sq.OrderBy("logged_time")
  47. query, args, err := sq.ToSql()
  48. if err != nil {
  49. return nil, err
  50. }
  51. res := make([]*models.Log, 0, 8)
  52. err = r.db.SelectContext(ctx, &res, query, args...)
  53. if err != nil {
  54. if err == sql.ErrNoRows {
  55. return res, nil
  56. }
  57. return nil, err
  58. }
  59. return res, nil
  60. }
  61. func (r *logRepository) Insert(ctx context.Context, log models.Log) error {
  62. _, err := r.db.NamedExecContext(ctx, `
  63. INSERT INTO log (
  64. log_id, user_id, task_id, item_id, logged_time, description, item_amount, secondary_item_id, secondary_item_amount
  65. ) VALUES (
  66. :log_id, :user_id, :task_id, :item_id, :logged_time, :description, :item_amount, :secondary_item_id, :secondary_item_amount
  67. )
  68. `, &log)
  69. if err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. func (r *logRepository) Update(ctx context.Context, log models.Log) error {
  75. _, err := r.db.NamedExecContext(ctx, `
  76. UPDATE log SET
  77. logged_time=:logged_time,
  78. description=:description,
  79. item_amount=:item_amount,
  80. secondary_item_id=:secondary_item_id,
  81. secondary_item_amount=:secondary_item_amount
  82. WHERE log_id=:log_id
  83. `, &log)
  84. if err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. func (r *logRepository) Delete(ctx context.Context, log models.Log) error {
  90. _, err := r.db.ExecContext(ctx, `DELETE FROM log WHERE log_id=$1`, log.ID)
  91. if err != nil {
  92. if err == sql.ErrNoRows {
  93. return slerrors.NotFound("Log")
  94. }
  95. return err
  96. }
  97. return nil
  98. }