place_image.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. placeImageTableName = "place_image"
  11. )
  12. type PlaceImageRepository struct {
  13. db *sql.DB
  14. }
  15. func InitPlaceImageRepository(db *sql.DB) *PlaceImageRepository {
  16. return &PlaceImageRepository{db: db}
  17. }
  18. func (r PlaceImageRepository) BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error) {
  19. return r.db.BeginTx(ctx, &sql.TxOptions{Isolation: level})
  20. }
  21. func (r PlaceImageRepository) CommitTx(tx *sql.Tx) error {
  22. if tx == nil {
  23. return errors.New("empty transaction")
  24. }
  25. return tx.Commit()
  26. }
  27. func (r PlaceImageRepository) Add(ctx context.Context, req models.AddPlaceImageRequest, tx *sql.Tx) error {
  28. query, args, err := sq.Insert(placeImageTableName).
  29. PlaceholderFormat(sq.Dollar).
  30. Columns("place_id", "image").
  31. Values(req.PlaceID, 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 PlaceImageRepository) Get(ctx context.Context, imageID int) (*models.Image, error) {
  44. query, args, err := sq.Select("id", "image", "place_id", "created_at").
  45. From(placeImageTableName).
  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.PlaceID, &res.CreatedAt)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &res, nil
  59. }
  60. func (r PlaceImageRepository) GetByPlaceID(ctx context.Context, placeID int) ([]models.Image, error) {
  61. var res []models.Image
  62. query := "WITH RECURSIVE cte (id, parent_id) AS (" +
  63. "SELECT id, parent_id " +
  64. "FROM " + placeTableName + " " +
  65. "WHERE id = $1 " +
  66. "UNION ALL " +
  67. "SELECT p.id, p.parent_id " +
  68. "FROM " + placeTableName + " p " +
  69. "INNER JOIN cte ON p.parent_id = cte.id " +
  70. ")" +
  71. "SELECT pi.id, pi.image, pi.place_id, pi.created_at " +
  72. "FROM cte, " + placeImageTableName + " pi " +
  73. "WHERE pi.place_id = cte.id " +
  74. "ORDER BY pi.created_at DESC"
  75. rows, err := r.db.QueryContext(ctx, query, placeID)
  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.PlaceID,
  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 PlaceImageRepository) Delete(ctx context.Context, imageID int, tx *sql.Tx) error {
  99. query, args, err := sq.Delete(placeImageTableName).
  100. PlaceholderFormat(sq.Dollar).
  101. Where(sq.Eq{"id": imageID}).
  102. ToSql()
  103. if err != nil {
  104. return err
  105. }
  106. if tx == nil {
  107. _, err = r.db.ExecContext(ctx, query, args...)
  108. } else {
  109. _, err = tx.ExecContext(ctx, query, args...)
  110. }
  111. return err
  112. }