thing_notification.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 thingNotificationTableName = "thing_notification"
  9. type ThingNotificationRepository struct {
  10. db DB
  11. }
  12. func InitThingNotificationRepository(db DB) *ThingNotificationRepository {
  13. return &ThingNotificationRepository{db: db}
  14. }
  15. func (r ThingNotificationRepository) Get(ctx context.Context, id uint64) (*models.ThingNotification, error) {
  16. q, v, err := sq.Select("thing_id", "notification_date", "created_at", "updated_at").
  17. From(thingNotificationTableName).
  18. PlaceholderFormat(sq.Dollar).
  19. Where(sq.Eq{"thing_id": id}).
  20. ToSql()
  21. if err != nil {
  22. return nil, fmt.Errorf("build query: %w", err)
  23. }
  24. var res models.ThingNotification
  25. err = r.db.GetContext(ctx, &res, q, v...)
  26. if err != nil {
  27. return nil, fmt.Errorf("get: %w", err)
  28. }
  29. return &res, nil
  30. }
  31. func (r ThingNotificationRepository) GetExpired(ctx context.Context) ([]models.ExtThingNotification, error) {
  32. var res []models.ExtThingNotification
  33. q, v, err := sq.Select(
  34. "n.thing_id AS thing_id",
  35. "n.notification_date",
  36. "n.created_at",
  37. "n.updated_at",
  38. "t.title AS thing_title",
  39. "p.id AS place_id",
  40. "p.title AS place_title",
  41. ).
  42. From(thingNotificationTableName + " n").
  43. Join(thingTableName + " t ON t.id = n.thing_id").
  44. LeftJoin(placeThingTableName + " pt ON pt.thing_id = n.thing_id").
  45. LeftJoin(placeTableName + " p ON p.id = pt.place_id").
  46. PlaceholderFormat(sq.Dollar).
  47. Where(sq.Lt{"n.notification_date": "NOW()"}).
  48. OrderBy("n.notification_date DESC").
  49. ToSql()
  50. if err != nil {
  51. return nil, fmt.Errorf("build query: %w", err)
  52. }
  53. err = r.db.SelectContext(ctx, &res, q, v...)
  54. if err != nil {
  55. return nil, fmt.Errorf("select: %w", err)
  56. }
  57. return res, nil
  58. }
  59. func (r ThingNotificationRepository) Add(ctx context.Context, req models.AddThingNotificationRequest) error {
  60. q, v, err := sq.Insert(thingNotificationTableName).
  61. PlaceholderFormat(sq.Dollar).
  62. Columns("thing_id", "notification_date").
  63. Values(req.ThingID, req.NotificationDate).
  64. ToSql()
  65. if err != nil {
  66. return fmt.Errorf("build query: %w", err)
  67. }
  68. _, err = r.db.ExecContext(ctx, q, v...)
  69. if err != nil {
  70. return fmt.Errorf("exec: %w", err)
  71. }
  72. return nil
  73. }
  74. func (r ThingNotificationRepository) Update(ctx context.Context, req models.UpdateThingNotificationRequest) error {
  75. q, v, err := sq.Update(thingNotificationTableName).
  76. PlaceholderFormat(sq.Dollar).
  77. Set("notification_date", req.NotificationDate).
  78. Set("updated_at", "NOW()").
  79. Where(sq.Eq{"thing_id": req.ThingID}).
  80. ToSql()
  81. if err != nil {
  82. return fmt.Errorf("build query: %w", err)
  83. }
  84. _, err = r.db.ExecContext(ctx, q, v...)
  85. if err != nil {
  86. return fmt.Errorf("exec: %w", err)
  87. }
  88. return nil
  89. }
  90. func (r ThingNotificationRepository) Delete(ctx context.Context, id uint64) error {
  91. q, v, err := sq.Delete(thingNotificationTableName).
  92. PlaceholderFormat(sq.Dollar).
  93. Where(sq.Eq{"thing_id": id}).
  94. ToSql()
  95. if err != nil {
  96. return fmt.Errorf("build query: %w", err)
  97. }
  98. _, err = r.db.ExecContext(ctx, q, v...)
  99. if err != nil {
  100. return fmt.Errorf("exec: %w", err)
  101. }
  102. return nil
  103. }