tag.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package repositories
  2. import (
  3. "context"
  4. "fmt"
  5. sq "github.com/Masterminds/squirrel"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  7. )
  8. const tagTableName = "tag"
  9. var tagTableFields = []string{"id", "title", "style", "created_at", "updated_at"}
  10. type TagRepository struct {
  11. db DB
  12. }
  13. func InitTagRepository(db DB) *TagRepository {
  14. return &TagRepository{db: db}
  15. }
  16. func (r TagRepository) GetAll(ctx context.Context) ([]models.Tag, error) {
  17. var res []models.Tag
  18. q, v, err := sq.Select(tagTableFields...).
  19. From(tagTableName).
  20. ToSql()
  21. if err != nil {
  22. return nil, fmt.Errorf("build query: %w", err)
  23. }
  24. err = r.db.SelectContext(ctx, &res, q, v...)
  25. if err != nil {
  26. return nil, fmt.Errorf("select: %w", err)
  27. }
  28. return res, nil
  29. }
  30. func (r TagRepository) GetByThingID(ctx context.Context, id uint64) ([]models.Tag, error) {
  31. var res []models.Tag
  32. q, v, err := sq.Select("t.id", "t.title", "t.style", "t.created_at", "t.updated_at").
  33. From(tagTableName + " t").
  34. Join(thingTagTableName + " tt ON tt.tag_id = t.id").
  35. PlaceholderFormat(sq.Dollar).
  36. Where(sq.Eq{"tt.thing_id": id}).
  37. ToSql()
  38. if err != nil {
  39. return nil, fmt.Errorf("build query: %w", err)
  40. }
  41. err = r.db.SelectContext(ctx, &res, q, v...)
  42. if err != nil {
  43. return nil, fmt.Errorf("select: %w", err)
  44. }
  45. return res, nil
  46. }
  47. func (r TagRepository) Get(ctx context.Context, id uint64) (*models.Tag, error) {
  48. q, v, err := sq.Select(tagTableFields...).
  49. From(tagTableName).
  50. PlaceholderFormat(sq.Dollar).
  51. Where(sq.Eq{"id": id}).
  52. ToSql()
  53. if err != nil {
  54. return nil, fmt.Errorf("build query: %w", err)
  55. }
  56. var res models.Tag
  57. err = r.db.GetContext(ctx, &res, q, v...)
  58. if err != nil {
  59. return nil, fmt.Errorf("get: %w", err)
  60. }
  61. return &res, nil
  62. }
  63. func (r TagRepository) Add(ctx context.Context, req models.AddTagRequest) (uint64, error) {
  64. q, v, err := sq.Insert(tagTableName).
  65. PlaceholderFormat(sq.Dollar).
  66. Columns("title", "style").
  67. Values(req.Title, req.Style).
  68. Suffix("RETURNING id").
  69. ToSql()
  70. if err != nil {
  71. return 0, fmt.Errorf("build query: %w", err)
  72. }
  73. res, err := r.db.ExecContext(ctx, q, v...)
  74. if err != nil {
  75. return 0, fmt.Errorf("exec: %w", err)
  76. }
  77. id, err := res.LastInsertId()
  78. if err != nil {
  79. return 0, fmt.Errorf("last insert ID: %w", err)
  80. }
  81. return uint64(id), nil
  82. }
  83. func (r TagRepository) Update(ctx context.Context, req models.UpdateTagRequest) error {
  84. q, v, err := sq.Update(tagTableName).
  85. PlaceholderFormat(sq.Dollar).
  86. Set("title", req.Title).
  87. Set("style", req.Style).
  88. Set("updated_at", "NOW()").
  89. Where(sq.Eq{"id": req.ID}).
  90. ToSql()
  91. if err != nil {
  92. return fmt.Errorf("build query: %w", err)
  93. }
  94. _, err = r.db.ExecContext(ctx, q, v...)
  95. if err != nil {
  96. return fmt.Errorf("exec: %w", err)
  97. }
  98. return nil
  99. }
  100. func (r TagRepository) Delete(ctx context.Context, id uint64) error {
  101. q, v, err := sq.Delete(tagTableName).
  102. PlaceholderFormat(sq.Dollar).
  103. Where(sq.Eq{"id": id}).
  104. ToSql()
  105. if err != nil {
  106. return fmt.Errorf("build query: %w", err)
  107. }
  108. _, err = r.db.ExecContext(ctx, q, v...)
  109. if err != nil {
  110. return fmt.Errorf("exec: %w", err)
  111. }
  112. return nil
  113. }