thing_notification.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package repositories
  2. import (
  3. "context"
  4. "database/sql"
  5. sq "github.com/Masterminds/squirrel"
  6. "git.dmitriygnatenko.ru/dima/homethings/internal/models"
  7. )
  8. const (
  9. thingNotificationTableName = "thing_notification"
  10. )
  11. type ThingNotificationRepository struct {
  12. db *sql.DB
  13. }
  14. func InitThingNotificationRepository(db *sql.DB) *ThingNotificationRepository {
  15. return &ThingNotificationRepository{db: db}
  16. }
  17. func (r ThingNotificationRepository) Get(ctx context.Context, thingID int) (*models.ThingNotification, error) {
  18. query, args, err := sq.Select("thing_id", "notification_date", "created_at", "updated_at").
  19. From(thingNotificationTableName).
  20. PlaceholderFormat(sq.Dollar).
  21. Where(sq.Eq{"thing_id": thingID}).
  22. ToSql()
  23. if err != nil {
  24. return nil, err
  25. }
  26. var res models.ThingNotification
  27. err = r.db.QueryRowContext(ctx, query, args...).
  28. Scan(&res.ThingID, &res.NotificationDate, &res.CreatedAt, &res.UpdatedAt)
  29. if err != nil {
  30. return nil, err
  31. }
  32. return &res, nil
  33. }
  34. func (r ThingNotificationRepository) GetExpired(ctx context.Context) ([]models.ExtThingNotification, error) {
  35. var res []models.ExtThingNotification
  36. query, args, err := sq.Select("n.thing_id", "n.notification_date", "n.created_at", "n.updated_at", "t.title", "p.id", "p.title").
  37. From(thingNotificationTableName + " n").
  38. Join(thingTableName + " t ON t.id = n.thing_id").
  39. LeftJoin(placeThingTableName + " pt ON pt.thing_id = n.thing_id").
  40. LeftJoin(placeTableName + " p ON p.id = pt.place_id").
  41. PlaceholderFormat(sq.Dollar).
  42. Where(sq.Lt{"n.notification_date": "NOW()"}).
  43. OrderBy("n.notification_date DESC").
  44. ToSql()
  45. if err != nil {
  46. return nil, err
  47. }
  48. rows, err := r.db.QueryContext(ctx, query, args...)
  49. if err != nil {
  50. return nil, err
  51. }
  52. defer rows.Close()
  53. for rows.Next() {
  54. resRow := models.ExtThingNotification{}
  55. err = rows.Scan(
  56. &resRow.ThingID,
  57. &resRow.NotificationDate,
  58. &resRow.CreatedAt,
  59. &resRow.UpdatedAt,
  60. &resRow.ThingTitle,
  61. &resRow.PlaceID,
  62. &resRow.PlaceTitle,
  63. )
  64. if err != nil {
  65. return nil, err
  66. }
  67. res = append(res, resRow)
  68. }
  69. if err = rows.Err(); err != nil {
  70. return nil, err
  71. }
  72. return res, nil
  73. }
  74. func (r ThingNotificationRepository) Add(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) error {
  75. query, args, err := sq.Insert(thingNotificationTableName).
  76. PlaceholderFormat(sq.Dollar).
  77. Columns("thing_id", "notification_date").
  78. Values(req.ThingID, req.NotificationDate).
  79. ToSql()
  80. if err != nil {
  81. return err
  82. }
  83. if tx == nil {
  84. _, err = r.db.ExecContext(ctx, query, args...)
  85. } else {
  86. _, err = tx.ExecContext(ctx, query, args...)
  87. }
  88. return err
  89. }
  90. func (r ThingNotificationRepository) Update(ctx context.Context, req models.UpdateThingNotificationRequest, tx *sql.Tx) error {
  91. query, args, err := sq.Update(thingNotificationTableName).
  92. PlaceholderFormat(sq.Dollar).
  93. Set("notification_date", req.NotificationDate).
  94. Set("updated_at", "NOW()").
  95. Where(sq.Eq{"thing_id": req.ThingID}).
  96. ToSql()
  97. if err != nil {
  98. return err
  99. }
  100. if tx == nil {
  101. _, err = r.db.ExecContext(ctx, query, args...)
  102. } else {
  103. _, err = tx.ExecContext(ctx, query, args...)
  104. }
  105. return err
  106. }
  107. func (r ThingNotificationRepository) Delete(ctx context.Context, thingID int, tx *sql.Tx) error {
  108. query, args, err := sq.Delete(thingNotificationTableName).
  109. PlaceholderFormat(sq.Dollar).
  110. Where(sq.Eq{"thing_id": thingID}).
  111. ToSql()
  112. if err != nil {
  113. return err
  114. }
  115. if tx == nil {
  116. _, err = r.db.ExecContext(ctx, query, args...)
  117. } else {
  118. _, err = tx.ExecContext(ctx, query, args...)
  119. }
  120. return err
  121. }