thing.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. res, err := r.db.ExecContext(ctx, q, v...)
  102. if err != nil {
  103. return 0, fmt.Errorf("exec: %w", err)
  104. }
  105. id, err := res.LastInsertId()
  106. if err != nil {
  107. return 0, fmt.Errorf("last insert ID: %w", err)
  108. }
  109. return uint64(id), nil
  110. }
  111. func (r ThingRepository) Update(ctx context.Context, req models.UpdateThingRequest) error {
  112. q, v, err := sq.Update(thingTableName).
  113. PlaceholderFormat(sq.Dollar).
  114. Set("title", req.Title).
  115. Set("description", req.Description).
  116. Set("updated_at", "NOW()").
  117. Where(sq.Eq{"id": req.ID}).
  118. ToSql()
  119. if err != nil {
  120. return fmt.Errorf("build query: %w", err)
  121. }
  122. _, err = r.db.ExecContext(ctx, q, v...)
  123. if err != nil {
  124. return fmt.Errorf("exec: %w", err)
  125. }
  126. return nil
  127. }
  128. func (r ThingRepository) Delete(ctx context.Context, id uint64) error {
  129. q, v, err := sq.Delete(thingTableName).
  130. PlaceholderFormat(sq.Dollar).
  131. Where(sq.Eq{"id": id}).
  132. ToSql()
  133. if err != nil {
  134. return fmt.Errorf("build query: %w", err)
  135. }
  136. _, err = r.db.ExecContext(ctx, q, v...)
  137. if err != nil {
  138. return fmt.Errorf("exec: %w", err)
  139. }
  140. return nil
  141. }