thing_notification.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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("n.thing_id", "n.notification_date", "n.created_at", "n.updated_at", "t.title", "p.id", "p.title").
  34. From(thingNotificationTableName + " n").
  35. Join(thingTableName + " t ON t.id = n.thing_id").
  36. LeftJoin(placeThingTableName + " pt ON pt.thing_id = n.thing_id").
  37. LeftJoin(placeTableName + " p ON p.id = pt.place_id").
  38. PlaceholderFormat(sq.Dollar).
  39. Where(sq.Lt{"n.notification_date": "NOW()"}).
  40. OrderBy("n.notification_date DESC").
  41. ToSql()
  42. if err != nil {
  43. return nil, fmt.Errorf("build query: %w", err)
  44. }
  45. err = r.db.SelectContext(ctx, &res, q, v)
  46. if err != nil {
  47. return nil, fmt.Errorf("select: %w", err)
  48. }
  49. return res, nil
  50. }
  51. func (r ThingNotificationRepository) Add(ctx context.Context, req models.AddThingNotificationRequest) error {
  52. q, v, err := sq.Insert(thingNotificationTableName).
  53. PlaceholderFormat(sq.Dollar).
  54. Columns("thing_id", "notification_date").
  55. Values(req.ThingID, req.NotificationDate).
  56. ToSql()
  57. if err != nil {
  58. return fmt.Errorf("build query: %w", err)
  59. }
  60. _, err = r.db.ExecContext(ctx, q, v...)
  61. if err != nil {
  62. return fmt.Errorf("exec: %w", err)
  63. }
  64. return nil
  65. }
  66. func (r ThingNotificationRepository) Update(ctx context.Context, req models.UpdateThingNotificationRequest) error {
  67. q, v, err := sq.Update(thingNotificationTableName).
  68. PlaceholderFormat(sq.Dollar).
  69. Set("notification_date", req.NotificationDate).
  70. Set("updated_at", "NOW()").
  71. Where(sq.Eq{"thing_id": req.ThingID}).
  72. ToSql()
  73. if err != nil {
  74. return fmt.Errorf("build query: %w", err)
  75. }
  76. _, err = r.db.ExecContext(ctx, q, v...)
  77. if err != nil {
  78. return fmt.Errorf("exec: %w", err)
  79. }
  80. return nil
  81. }
  82. func (r ThingNotificationRepository) Delete(ctx context.Context, id uint64) error {
  83. q, v, err := sq.Delete(thingNotificationTableName).
  84. PlaceholderFormat(sq.Dollar).
  85. Where(sq.Eq{"thing_id": 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. }