place.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. placeTableName = "place"
  11. )
  12. type PlaceRepository struct {
  13. db *sql.DB
  14. }
  15. func InitPlaceRepository(db *sql.DB) *PlaceRepository {
  16. return &PlaceRepository{db: db}
  17. }
  18. func (r PlaceRepository) BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error) {
  19. return r.db.BeginTx(ctx, &sql.TxOptions{Isolation: level})
  20. }
  21. func (r PlaceRepository) CommitTx(tx *sql.Tx) error {
  22. if tx == nil {
  23. return errors.New("empty transaction")
  24. }
  25. return tx.Commit()
  26. }
  27. func (r PlaceRepository) GetAll(ctx context.Context) ([]models.Place, error) {
  28. var res []models.Place
  29. query, args, err := sq.Select("id", "parent_id", "title", "created_at", "updated_at").
  30. From(placeTableName).
  31. ToSql()
  32. if err != nil {
  33. return nil, err
  34. }
  35. rows, err := r.db.QueryContext(ctx, query, args...)
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer rows.Close()
  40. for rows.Next() {
  41. resRow := models.Place{}
  42. err = rows.Scan(
  43. &resRow.ID,
  44. &resRow.ParentID,
  45. &resRow.Title,
  46. &resRow.CreatedAt,
  47. &resRow.UpdatedAt,
  48. )
  49. if err != nil {
  50. return nil, err
  51. }
  52. res = append(res, resRow)
  53. }
  54. if err = rows.Err(); err != nil {
  55. return nil, err
  56. }
  57. return res, nil
  58. }
  59. func (r PlaceRepository) GetNestedPlaces(ctx context.Context, placeID int) ([]models.Place, error) {
  60. var res []models.Place
  61. query, args, err := sq.Select("id", "parent_id", "title", "created_at", "updated_at").
  62. From(placeTableName).
  63. PlaceholderFormat(sq.Dollar).
  64. Where(sq.Eq{"parent_id": placeID}).
  65. ToSql()
  66. if err != nil {
  67. return nil, err
  68. }
  69. rows, err := r.db.QueryContext(ctx, query, args...)
  70. if err != nil {
  71. return nil, err
  72. }
  73. defer rows.Close()
  74. for rows.Next() {
  75. resRow := models.Place{}
  76. err = rows.Scan(
  77. &resRow.ID,
  78. &resRow.ParentID,
  79. &resRow.Title,
  80. &resRow.CreatedAt,
  81. &resRow.UpdatedAt,
  82. )
  83. if err != nil {
  84. return nil, err
  85. }
  86. res = append(res, resRow)
  87. }
  88. if err = rows.Err(); err != nil {
  89. return nil, err
  90. }
  91. return res, nil
  92. }
  93. func (r PlaceRepository) Get(ctx context.Context, placeID int) (*models.Place, error) {
  94. query, args, err := sq.Select("id", "parent_id", "title", "created_at", "updated_at").
  95. From(placeTableName).
  96. PlaceholderFormat(sq.Dollar).
  97. Where(sq.Eq{"id": placeID}).
  98. ToSql()
  99. if err != nil {
  100. return nil, err
  101. }
  102. var res models.Place
  103. err = r.db.QueryRowContext(ctx, query, args...).
  104. Scan(&res.ID, &res.ParentID, &res.Title, &res.CreatedAt, &res.UpdatedAt)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return &res, nil
  109. }
  110. func (r PlaceRepository) Add(ctx context.Context, req models.AddPlaceRequest, tx *sql.Tx) (int, error) {
  111. query, args, err := sq.Insert(placeTableName).
  112. PlaceholderFormat(sq.Dollar).
  113. Columns("title", "parent_id").
  114. Values(req.Title, req.ParentID).
  115. Suffix("RETURNING id").
  116. ToSql()
  117. if err != nil {
  118. return 0, err
  119. }
  120. var id int
  121. if tx == nil {
  122. err = r.db.QueryRowContext(ctx, query, args...).Scan(&id)
  123. } else {
  124. err = tx.QueryRowContext(ctx, query, args...).Scan(&id)
  125. }
  126. if err != nil {
  127. return 0, err
  128. }
  129. return id, nil
  130. }
  131. func (r PlaceRepository) Update(ctx context.Context, req models.UpdatePlaceRequest, tx *sql.Tx) error {
  132. query, args, err := sq.Update(placeTableName).
  133. PlaceholderFormat(sq.Dollar).
  134. Set("title", req.Title).
  135. Set("parent_id", req.ParentID).
  136. Set("updated_at", "NOW()").
  137. Where(sq.Eq{"id": req.ID}).
  138. ToSql()
  139. if err != nil {
  140. return err
  141. }
  142. if tx == nil {
  143. _, err = r.db.ExecContext(ctx, query, args...)
  144. } else {
  145. _, err = tx.ExecContext(ctx, query, args...)
  146. }
  147. return err
  148. }
  149. func (r PlaceRepository) Delete(ctx context.Context, placeID int, tx *sql.Tx) error {
  150. query, args, err := sq.Delete(placeTableName).
  151. PlaceholderFormat(sq.Dollar).
  152. Where(sq.Eq{"id": placeID}).
  153. ToSql()
  154. if err != nil {
  155. return err
  156. }
  157. if tx == nil {
  158. _, err = r.db.ExecContext(ctx, query, args...)
  159. } else {
  160. _, err = tx.ExecContext(ctx, query, args...)
  161. }
  162. return err
  163. }