thing_image.go 3.8 KB

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