place_image.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.PlaceImageRepository -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. placeImageTableName = "place_image"
  15. )
  16. type placeImageRepository struct {
  17. db *sql.DB
  18. }
  19. func InitPlaceImageRepository(db *sql.DB) interfaces.PlaceImageRepository {
  20. return placeImageRepository{db: db}
  21. }
  22. func (r placeImageRepository) BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error) {
  23. return r.db.BeginTx(ctx, &sql.TxOptions{Isolation: level})
  24. }
  25. func (r placeImageRepository) CommitTx(tx *sql.Tx) error {
  26. if tx == nil {
  27. return errors.New("empty transaction")
  28. }
  29. return tx.Commit()
  30. }
  31. func (r placeImageRepository) Add(ctx context.Context, req models.AddPlaceImageRequest, tx *sql.Tx) error {
  32. query, args, err := sq.Insert(placeImageTableName).
  33. PlaceholderFormat(sq.Dollar).
  34. Columns("place_id", "image").
  35. Values(req.PlaceID, 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 placeImageRepository) Get(ctx context.Context, imageID int) (*models.Image, error) {
  48. query, args, err := sq.Select("id", "image", "place_id", "created_at").
  49. From(placeImageTableName).
  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.PlaceID, &res.CreatedAt)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return &res, nil
  63. }
  64. func (r placeImageRepository) GetByPlaceID(ctx context.Context, placeID int) ([]models.Image, error) {
  65. var res []models.Image
  66. query := "WITH RECURSIVE cte (id, parent_id) AS (" +
  67. "SELECT id, parent_id " +
  68. "FROM " + placeTableName + " " +
  69. "WHERE id = $1 " +
  70. "UNION ALL " +
  71. "SELECT p.id, p.parent_id " +
  72. "FROM " + placeTableName + " p " +
  73. "INNER JOIN cte ON p.parent_id = cte.id " +
  74. ")" +
  75. "SELECT pi.id, pi.image, pi.place_id, pi.created_at " +
  76. "FROM cte, " + placeImageTableName + " pi " +
  77. "WHERE pi.place_id = cte.id " +
  78. "ORDER BY pi.created_at DESC"
  79. rows, err := r.db.QueryContext(ctx, query, placeID)
  80. if err != nil {
  81. return nil, err
  82. }
  83. defer rows.Close()
  84. for rows.Next() {
  85. resRow := models.Image{}
  86. err = rows.Scan(
  87. &resRow.ID,
  88. &resRow.Image,
  89. &resRow.PlaceID,
  90. &resRow.CreatedAt,
  91. )
  92. if err != nil {
  93. return nil, err
  94. }
  95. res = append(res, resRow)
  96. }
  97. if err = rows.Err(); err != nil {
  98. return nil, err
  99. }
  100. return res, nil
  101. }
  102. func (r placeImageRepository) Delete(ctx context.Context, imageID int, tx *sql.Tx) error {
  103. query, args, err := sq.Delete(placeImageTableName).
  104. PlaceholderFormat(sq.Dollar).
  105. Where(sq.Eq{"id": imageID}).
  106. ToSql()
  107. if err != nil {
  108. return err
  109. }
  110. if tx == nil {
  111. _, err = r.db.ExecContext(ctx, query, args...)
  112. } else {
  113. _, err = tx.ExecContext(ctx, query, args...)
  114. }
  115. return err
  116. }