tag.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.TagRepository -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. tagTableName = "tag"
  15. )
  16. type tagRepository struct {
  17. db *sql.DB
  18. }
  19. func InitTagRepository(db *sql.DB) interfaces.TagRepository {
  20. return tagRepository{db: db}
  21. }
  22. func (r tagRepository) BeginTx(ctx context.Context, level sql.IsolationLevel) (*sql.Tx, error) {
  23. return r.db.BeginTx(ctx, &sql.TxOptions{Isolation: level})
  24. }
  25. func (r tagRepository) CommitTx(tx *sql.Tx) error {
  26. if tx == nil {
  27. return errors.New("empty transaction")
  28. }
  29. return tx.Commit()
  30. }
  31. func (r tagRepository) GetAll(ctx context.Context) ([]models.Tag, error) {
  32. var res []models.Tag
  33. query, args, err := sq.Select("id", "title", "style", "created_at", "updated_at").
  34. From(tagTableName).
  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.Tag{}
  46. err = rows.Scan(
  47. &resRow.ID,
  48. &resRow.Title,
  49. &resRow.Style,
  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 tagRepository) GetByThingID(ctx context.Context, thingID int) ([]models.Tag, error) {
  64. var res []models.Tag
  65. query, args, err := sq.Select("t.id", "t.title", "t.style", "t.created_at", "t.updated_at").
  66. From(tagTableName + " t").
  67. Join(thingTagTableName + " tt ON tt.tag_id = t.id").
  68. PlaceholderFormat(sq.Dollar).
  69. Where(sq.Eq{"tt.thing_id": thingID}).
  70. ToSql()
  71. if err != nil {
  72. return nil, err
  73. }
  74. rows, err := r.db.QueryContext(ctx, query, args...)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer rows.Close()
  79. for rows.Next() {
  80. resRow := models.Tag{}
  81. err = rows.Scan(
  82. &resRow.ID,
  83. &resRow.Title,
  84. &resRow.Style,
  85. &resRow.CreatedAt,
  86. &resRow.UpdatedAt,
  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 tagRepository) Get(ctx context.Context, tagID int) (*models.Tag, error) {
  99. query, args, err := sq.Select("id", "title", "style", "created_at", "updated_at").
  100. From(tagTableName).
  101. PlaceholderFormat(sq.Dollar).
  102. Where(sq.Eq{"id": tagID}).
  103. ToSql()
  104. if err != nil {
  105. return nil, err
  106. }
  107. var res models.Tag
  108. err = r.db.QueryRowContext(ctx, query, args...).
  109. Scan(&res.ID, &res.Title, &res.Style, &res.CreatedAt, &res.UpdatedAt)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &res, nil
  114. }
  115. func (r tagRepository) Add(ctx context.Context, req models.AddTagRequest, tx *sql.Tx) (int, error) {
  116. query, args, err := sq.Insert(tagTableName).
  117. PlaceholderFormat(sq.Dollar).
  118. Columns("title", "style").
  119. Values(req.Title, req.Style).
  120. Suffix("RETURNING id").
  121. ToSql()
  122. if err != nil {
  123. return 0, err
  124. }
  125. var id int
  126. if tx == nil {
  127. err = r.db.QueryRowContext(ctx, query, args...).Scan(&id)
  128. } else {
  129. err = tx.QueryRowContext(ctx, query, args...).Scan(&id)
  130. }
  131. if err != nil {
  132. return 0, err
  133. }
  134. return id, nil
  135. }
  136. func (r tagRepository) Update(ctx context.Context, req models.UpdateTagRequest, tx *sql.Tx) error {
  137. query, args, err := sq.Update(tagTableName).
  138. PlaceholderFormat(sq.Dollar).
  139. Set("title", req.Title).
  140. Set("style", req.Style).
  141. Set("updated_at", "NOW()").
  142. Where(sq.Eq{"id": req.ID}).
  143. ToSql()
  144. if err != nil {
  145. return err
  146. }
  147. if tx == nil {
  148. _, err = r.db.ExecContext(ctx, query, args...)
  149. } else {
  150. _, err = tx.ExecContext(ctx, query, args...)
  151. }
  152. return err
  153. }
  154. func (r tagRepository) Delete(ctx context.Context, tagID int, tx *sql.Tx) error {
  155. query, args, err := sq.Delete(tagTableName).
  156. PlaceholderFormat(sq.Dollar).
  157. Where(sq.Eq{"id": tagID}).
  158. ToSql()
  159. if err != nil {
  160. return err
  161. }
  162. if tx == nil {
  163. _, err = r.db.ExecContext(ctx, query, args...)
  164. } else {
  165. _, err = tx.ExecContext(ctx, query, args...)
  166. }
  167. return err
  168. }