place_thing.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.PlaceThingRepository -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "context"
  7. "database/sql"
  8. "git.dmitriygnatenko.ru/dima/homethings/internal/interfaces"
  9. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  10. sq "github.com/Masterminds/squirrel"
  11. )
  12. const (
  13. placeThingTableName = "place_thing"
  14. )
  15. type placeThingRepository struct {
  16. db *sql.DB
  17. }
  18. func InitPlaceThingRepository(db *sql.DB) interfaces.PlaceThingRepository {
  19. return placeThingRepository{db: db}
  20. }
  21. func (r placeThingRepository) GetByThingID(ctx context.Context, thingID int) (*models.PlaceThing, error) {
  22. query, args, err := sq.Select("place_id", "thing_id", "created_at").
  23. From(placeThingTableName).
  24. PlaceholderFormat(sq.Dollar).
  25. Where(sq.Eq{"thing_id": thingID}).
  26. ToSql()
  27. if err != nil {
  28. return nil, err
  29. }
  30. var res models.PlaceThing
  31. err = r.db.QueryRowContext(ctx, query, args...).
  32. Scan(&res.PlaceID, &res.ThingID, &res.CreatedAt)
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &res, nil
  37. }
  38. func (r placeThingRepository) Add(ctx context.Context, req models.AddPlaceThingRequest, tx *sql.Tx) error {
  39. query, args, err := sq.Insert(placeThingTableName).
  40. PlaceholderFormat(sq.Dollar).
  41. Columns("place_id", "thing_id").
  42. Values(req.PlaceID, req.ThingID).
  43. ToSql()
  44. if err != nil {
  45. return err
  46. }
  47. if tx == nil {
  48. _, err = r.db.ExecContext(ctx, query, args...)
  49. } else {
  50. _, err = tx.ExecContext(ctx, query, args...)
  51. }
  52. return err
  53. }
  54. func (r placeThingRepository) UpdatePlace(ctx context.Context, req models.UpdatePlaceThingRequest, tx *sql.Tx) error {
  55. query, args, err := sq.Update(placeThingTableName).
  56. PlaceholderFormat(sq.Dollar).
  57. Set("place_id", req.PlaceID).
  58. Set("updated_at", "NOW()").
  59. Where(sq.Eq{"thing_id": req.ThingID}).
  60. ToSql()
  61. if err != nil {
  62. return err
  63. }
  64. if tx == nil {
  65. _, err = r.db.ExecContext(ctx, query, args...)
  66. } else {
  67. _, err = tx.ExecContext(ctx, query, args...)
  68. }
  69. return err
  70. }
  71. func (r placeThingRepository) DeleteThing(ctx context.Context, id int, tx *sql.Tx) error {
  72. query, args, err := sq.Delete(placeThingTableName).
  73. PlaceholderFormat(sq.Dollar).
  74. Where(sq.Eq{"thing_id": id}).
  75. ToSql()
  76. if err != nil {
  77. return err
  78. }
  79. if tx == nil {
  80. _, err = r.db.ExecContext(ctx, query, args...)
  81. } else {
  82. _, err = tx.ExecContext(ctx, query, args...)
  83. }
  84. return err
  85. }