thing_image.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package repositories
  2. //go:generate mkdir -p mocks
  3. //go:generate rm -rf ./mocks/*_minimock.go
  4. //go:generate minimock -i git.dmitriygnatenko.ru/dima/homethings/internal/interfaces.ThingImageRepository -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "context"
  7. "database/sql"
  8. "errors"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  10. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  11. sq "github.com/Masterminds/squirrel"
  12. )
  13. const (
  14. thingImageTableName = "thing_image"
  15. )
  16. type thingImageRepository struct {
  17. db *sql.DB
  18. }
  19. func InitThingImageRepository(db *sql.DB) interfaces.ThingImageRepository {
  20. return thingImageRepository{db: db}
  21. }
  22. func (r thingImageRepository) BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error) {
  23. return r.db.BeginTx(ctx, &sql.TxOptions{Isolation: level})
  24. }
  25. func (r thingImageRepository) CommitTx(tx *sql.Tx) error {
  26. if tx == nil {
  27. return errors.New("empty transaction")
  28. }
  29. return tx.Commit()
  30. }
  31. func (r thingImageRepository) Add(ctx context.Context, req models.AddThingImageRequest, tx *sql.Tx) error {
  32. query, args, err := sq.Insert(thingImageTableName).
  33. PlaceholderFormat(sq.Dollar).
  34. Columns("thing_id", "image").
  35. Values(req.ThingID, req.Image).
  36. ToSql()
  37. if err != nil {
  38. return err
  39. }
  40. if tx == nil {
  41. _, err = r.db.ExecContext(ctx, query, args...)
  42. } else {
  43. _, err = tx.ExecContext(ctx, query, args...)
  44. }
  45. return err
  46. }
  47. func (r thingImageRepository) Get(ctx context.Context, imageID int) (*models.Image, error) {
  48. query, args, err := sq.Select("id", "image", "thing_id", "created_at").
  49. From(thingImageTableName).
  50. PlaceholderFormat(sq.Dollar).
  51. Where(sq.Eq{"id": imageID}).
  52. ToSql()
  53. if err != nil {
  54. return nil, err
  55. }
  56. var res models.Image
  57. err = r.db.QueryRowContext(ctx, query, args...).
  58. Scan(&res.ID, &res.Image, &res.ThingID, &res.CreatedAt)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &res, nil
  63. }
  64. func (r thingImageRepository) GetByThingID(ctx context.Context, thingID int) ([]models.Image, error) {
  65. var res []models.Image
  66. query, args, err := sq.Select("id", "image", "thing_id", "created_at").
  67. From(thingImageTableName).
  68. PlaceholderFormat(sq.Dollar).
  69. Where(sq.Eq{"thing_id": thingID}).
  70. OrderBy("created_at DESC").
  71. ToSql()
  72. if err != nil {
  73. return nil, err
  74. }
  75. rows, err := r.db.QueryContext(ctx, query, args...)
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer rows.Close()
  80. for rows.Next() {
  81. resRow := models.Image{}
  82. err = rows.Scan(
  83. &resRow.ID,
  84. &resRow.Image,
  85. &resRow.ThingID,
  86. &resRow.CreatedAt,
  87. )
  88. if err != nil {
  89. return nil, err
  90. }
  91. res = append(res, resRow)
  92. }
  93. if err = rows.Err(); err != nil {
  94. return nil, err
  95. }
  96. return res, nil
  97. }
  98. func (r thingImageRepository) GetByPlaceID(ctx context.Context, placeID int) ([]models.Image, error) {
  99. var res []models.Image
  100. query := "WITH RECURSIVE cte (id, parent_id) AS (" +
  101. "SELECT id, parent_id " +
  102. "FROM " + placeTableName + " " +
  103. "WHERE id = $1 " +
  104. "UNION ALL " +
  105. "SELECT p.id, p.parent_id " +
  106. "FROM " + placeTableName + " p " +
  107. "INNER JOIN cte ON p.parent_id = cte.id " +
  108. ")" +
  109. "SELECT ti.id, ti.image, ti.thing_id, ti.created_at " +
  110. "FROM cte, " + placeThingTableName + " pt, " + thingImageTableName + " ti " +
  111. "WHERE pt.place_id = cte.id AND pt.thing_id = ti.thing_id " +
  112. "ORDER BY ti.created_at DESC"
  113. rows, err := r.db.QueryContext(ctx, query, placeID)
  114. if err != nil {
  115. return nil, err
  116. }
  117. defer rows.Close()
  118. for rows.Next() {
  119. resRow := models.Image{}
  120. err = rows.Scan(
  121. &resRow.ID,
  122. &resRow.Image,
  123. &resRow.ThingID,
  124. &resRow.CreatedAt,
  125. )
  126. if err != nil {
  127. return nil, err
  128. }
  129. res = append(res, resRow)
  130. }
  131. if err = rows.Err(); err != nil {
  132. return nil, err
  133. }
  134. return res, nil
  135. }
  136. func (r thingImageRepository) Delete(ctx context.Context, imageID int, tx *sql.Tx) error {
  137. query, args, err := sq.Delete(thingImageTableName).
  138. PlaceholderFormat(sq.Dollar).
  139. Where(sq.Eq{"id": imageID}).
  140. ToSql()
  141. if err != nil {
  142. return err
  143. }
  144. if tx == nil {
  145. _, err = r.db.ExecContext(ctx, query, args...)
  146. } else {
  147. _, err = tx.ExecContext(ctx, query, args...)
  148. }
  149. return err
  150. }