place.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 placeTableName = "place"
  9. var placeTableFields = []string{"id", "parent_id", "title", "created_at", "updated_at"}
  10. type PlaceRepository struct {
  11. db DB
  12. }
  13. func InitPlaceRepository(db DB) *PlaceRepository {
  14. return &PlaceRepository{db: db}
  15. }
  16. func (r PlaceRepository) GetAll(ctx context.Context) ([]models.Place, error) {
  17. var res []models.Place
  18. q, v, err := sq.Select(placeTableFields...).
  19. From(placeTableName).
  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 PlaceRepository) GetNestedPlaces(ctx context.Context, id uint64) ([]models.Place, error) {
  31. var res []models.Place
  32. q, v, err := sq.Select(placeTableFields...).
  33. From(placeTableName).
  34. PlaceholderFormat(sq.Dollar).
  35. Where(sq.Eq{"parent_id": id}).
  36. ToSql()
  37. if err != nil {
  38. return nil, fmt.Errorf("build query: %w", err)
  39. }
  40. err = r.db.SelectContext(ctx, &res, q, v...)
  41. if err != nil {
  42. return nil, fmt.Errorf("select: %w", err)
  43. }
  44. return res, nil
  45. }
  46. func (r PlaceRepository) Get(ctx context.Context, id uint64) (*models.Place, error) {
  47. q, v, err := sq.Select(placeTableFields...).
  48. From(placeTableName).
  49. PlaceholderFormat(sq.Dollar).
  50. Where(sq.Eq{"id": id}).
  51. ToSql()
  52. if err != nil {
  53. return nil, fmt.Errorf("build query: %w", err)
  54. }
  55. var res models.Place
  56. err = r.db.GetContext(ctx, &res, q, v...)
  57. if err != nil {
  58. return nil, fmt.Errorf("get: %w", err)
  59. }
  60. return &res, nil
  61. }
  62. func (r PlaceRepository) Add(ctx context.Context, req models.AddPlaceRequest) (uint64, error) {
  63. builder := sq.Insert(placeTableName).
  64. PlaceholderFormat(sq.Dollar).
  65. Columns("title", "parent_id").
  66. Values(req.Title, req.ParentID).
  67. Suffix("RETURNING id")
  68. q, v, err := builder.ToSql()
  69. if err != nil {
  70. return 0, fmt.Errorf("build query: %w", err)
  71. }
  72. var id uint64
  73. err = r.db.QueryRowContext(ctx, q, v...).Scan(&id)
  74. if err != nil {
  75. return 0, fmt.Errorf("exec: %w", err)
  76. }
  77. return id, nil
  78. }
  79. func (r PlaceRepository) Update(ctx context.Context, req models.UpdatePlaceRequest) error {
  80. q, v, err := sq.Update(placeTableName).
  81. PlaceholderFormat(sq.Dollar).
  82. Set("title", req.Title).
  83. Set("parent_id", req.ParentID).
  84. Set("updated_at", "NOW()").
  85. Where(sq.Eq{"id": req.ID}).
  86. ToSql()
  87. if err != nil {
  88. return fmt.Errorf("build query: %w", err)
  89. }
  90. _, err = r.db.ExecContext(ctx, q, v...)
  91. if err != nil {
  92. return fmt.Errorf("exec: %w", err)
  93. }
  94. return nil
  95. }
  96. func (r PlaceRepository) Delete(ctx context.Context, id uint64) error {
  97. q, v, err := sq.Delete(placeTableName).
  98. PlaceholderFormat(sq.Dollar).
  99. Where(sq.Eq{"id": id}).
  100. ToSql()
  101. if err != nil {
  102. return fmt.Errorf("build query: %w", err)
  103. }
  104. _, err = r.db.ExecContext(ctx, q, v...)
  105. if err != nil {
  106. return fmt.Errorf("exec: %w", err)
  107. }
  108. return nil
  109. }