place.go 4.2 KB

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