thing_notification.go 3.8 KB

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