tag.go 3.9 KB

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