place.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Columns("title", "parent_id").
  65. Values(req.Title, req.ParentID).
  66. Suffix("RETURNING id")
  67. q, v, err := builder.ToSql()
  68. if err != nil {
  69. return 0, fmt.Errorf("build query: %w", err)
  70. }
  71. res, err := r.db.ExecContext(ctx, q, v...)
  72. if err != nil {
  73. return 0, fmt.Errorf("exec: %w", err)
  74. }
  75. id, err := res.LastInsertId()
  76. if err != nil {
  77. return 0, fmt.Errorf("last insert ID: %w", err)
  78. }
  79. return uint64(id), nil
  80. }
  81. func (r PlaceRepository) Update(ctx context.Context, req models.UpdatePlaceRequest) error {
  82. q, v, err := sq.Update(placeTableName).
  83. PlaceholderFormat(sq.Dollar).
  84. Set("title", req.Title).
  85. Set("parent_id", req.ParentID).
  86. Set("updated_at", "NOW()").
  87. Where(sq.Eq{"id": req.ID}).
  88. ToSql()
  89. if err != nil {
  90. return fmt.Errorf("build query: %w", err)
  91. }
  92. _, err = r.db.ExecContext(ctx, q, v...)
  93. if err != nil {
  94. return fmt.Errorf("exec: %w", err)
  95. }
  96. return nil
  97. }
  98. func (r PlaceRepository) Delete(ctx context.Context, id uint64) error {
  99. q, v, err := sq.Delete(placeTableName).
  100. PlaceholderFormat(sq.Dollar).
  101. Where(sq.Eq{"id": id}).
  102. ToSql()
  103. if err != nil {
  104. return fmt.Errorf("build query: %w", err)
  105. }
  106. _, err = r.db.ExecContext(ctx, q, v...)
  107. if err != nil {
  108. return fmt.Errorf("exec: %w", err)
  109. }
  110. return nil
  111. }