thing.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package repositories
  2. import (
  3. "context"
  4. "fmt"
  5. sq "github.com/Masterminds/squirrel"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  7. )
  8. const thingTableName = "thing"
  9. type ThingRepository struct {
  10. db DB
  11. }
  12. func InitThingRepository(db DB) *ThingRepository {
  13. return &ThingRepository{db: db}
  14. }
  15. func (r ThingRepository) Get(ctx context.Context, id uint64) (*models.Thing, error) {
  16. q, v, err := sq.Select("t.id", "t.title", "t.description", "t.created_at", "t.updated_at", "p.place_id").
  17. From(thingTableName + " t").
  18. Join(placeThingTableName + " p ON p.thing_id = t.id").
  19. PlaceholderFormat(sq.Dollar).
  20. Where(sq.Eq{"id": id}).
  21. ToSql()
  22. if err != nil {
  23. return nil, fmt.Errorf("build query: %w", err)
  24. }
  25. var res models.Thing
  26. err = r.db.GetContext(ctx, &res, q, v...)
  27. if err != nil {
  28. return nil, fmt.Errorf("get: %w", err)
  29. }
  30. return &res, nil
  31. }
  32. func (r ThingRepository) Search(ctx context.Context, search string) ([]models.Thing, error) {
  33. var res []models.Thing
  34. s := fmt.Sprint("%", search, "%")
  35. q, v, err := sq.Select("t.id", "t.title", "t.description", "t.created_at", "t.updated_at", "p.place_id").
  36. From(thingTableName+" t").
  37. Join(placeThingTableName+" p ON p.thing_id = t.id").
  38. PlaceholderFormat(sq.Dollar).
  39. Where("t.title ILIKE ? OR t.description ILIKE ?", s, s).
  40. OrderBy("t.updated_at DESC").
  41. ToSql()
  42. if err != nil {
  43. return nil, fmt.Errorf("build query: %w", err)
  44. }
  45. err = r.db.SelectContext(ctx, &res, q, v...)
  46. if err != nil {
  47. return nil, fmt.Errorf("select: %w", err)
  48. }
  49. return res, nil
  50. }
  51. func (r ThingRepository) GetByPlaceID(ctx context.Context, id uint64) ([]models.Thing, error) {
  52. var res []models.Thing
  53. q, v, err := sq.Select("t.id", "t.title", "t.description", "t.created_at", "t.updated_at", "p.place_id").
  54. From(thingTableName + " t").
  55. Join(placeThingTableName + " p ON p.thing_id = t.id").
  56. PlaceholderFormat(sq.Dollar).
  57. Where(sq.Eq{"p.place_id": id}).
  58. OrderBy("t.updated_at DESC").
  59. ToSql()
  60. if err != nil {
  61. return nil, fmt.Errorf("build query: %w", err)
  62. }
  63. err = r.db.SelectContext(ctx, &res, q, v...)
  64. if err != nil {
  65. return nil, fmt.Errorf("select: %w", err)
  66. }
  67. return res, nil
  68. }
  69. // GetAllByPlaceID return things by place ID and all child places
  70. func (r ThingRepository) GetAllByPlaceID(ctx context.Context, id uint64) ([]models.Thing, error) {
  71. var res []models.Thing
  72. q := "WITH RECURSIVE cte (id, parent_id) AS (" +
  73. "SELECT id, parent_id " +
  74. "FROM " + placeTableName + " " +
  75. "WHERE id = $1 " +
  76. "UNION ALL " +
  77. "SELECT p.id, p.parent_id " +
  78. "FROM " + placeTableName + " p " +
  79. "INNER JOIN cte ON p.parent_id = cte.id " +
  80. ")" +
  81. "SELECT t.id, t.title, t.description, t.created_at, t.updated_at, pt.place_id " +
  82. "FROM cte, " + placeThingTableName + " pt, " + thingTableName + " t " +
  83. "WHERE pt.place_id = cte.id and t.id = pt.thing_id " +
  84. "ORDER BY t.updated_at DESC"
  85. err := r.db.SelectContext(ctx, &res, q, id)
  86. if err != nil {
  87. return nil, fmt.Errorf("select: %w", err)
  88. }
  89. return res, nil
  90. }
  91. func (r ThingRepository) Add(ctx context.Context, req models.AddThingRequest) (uint64, error) {
  92. q, v, err := sq.Insert(thingTableName).
  93. PlaceholderFormat(sq.Dollar).
  94. Columns("title", "description").
  95. Values(req.Title, req.Description).
  96. Suffix("RETURNING id").
  97. ToSql()
  98. if err != nil {
  99. return 0, fmt.Errorf("build query: %w", err)
  100. }
  101. var id uint64
  102. err = r.db.QueryRowContext(ctx, q, v...).Scan(&id)
  103. if err != nil {
  104. return 0, fmt.Errorf("exec: %w", err)
  105. }
  106. return id, nil
  107. }
  108. func (r ThingRepository) Update(ctx context.Context, req models.UpdateThingRequest) error {
  109. q, v, err := sq.Update(thingTableName).
  110. PlaceholderFormat(sq.Dollar).
  111. Set("title", req.Title).
  112. Set("description", req.Description).
  113. Set("updated_at", "NOW()").
  114. Where(sq.Eq{"id": req.ID}).
  115. ToSql()
  116. if err != nil {
  117. return fmt.Errorf("build query: %w", err)
  118. }
  119. _, err = r.db.ExecContext(ctx, q, v...)
  120. if err != nil {
  121. return fmt.Errorf("exec: %w", err)
  122. }
  123. return nil
  124. }
  125. func (r ThingRepository) Delete(ctx context.Context, id uint64) error {
  126. q, v, err := sq.Delete(thingTableName).
  127. PlaceholderFormat(sq.Dollar).
  128. Where(sq.Eq{"id": id}).
  129. ToSql()
  130. if err != nil {
  131. return fmt.Errorf("build query: %w", err)
  132. }
  133. _, err = r.db.ExecContext(ctx, q, v...)
  134. if err != nil {
  135. return fmt.Errorf("exec: %w", err)
  136. }
  137. return nil
  138. }