123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package repositories
- import (
- "context"
- "database/sql"
- sq "github.com/Masterminds/squirrel"
- "git.dmitriygnatenko.ru/dima/homethings/internal/models"
- )
- const (
- thingNotificationTableName = "thing_notification"
- )
- type ThingNotificationRepository struct {
- db *sql.DB
- }
- func InitThingNotificationRepository(db *sql.DB) *ThingNotificationRepository {
- return &ThingNotificationRepository{db: db}
- }
- func (r ThingNotificationRepository) Get(ctx context.Context, thingID int) (*models.ThingNotification, error) {
- query, args, err := sq.Select("thing_id", "notification_date", "created_at", "updated_at").
- From(thingNotificationTableName).
- PlaceholderFormat(sq.Dollar).
- Where(sq.Eq{"thing_id": thingID}).
- ToSql()
- if err != nil {
- return nil, err
- }
- var res models.ThingNotification
- err = r.db.QueryRowContext(ctx, query, args...).
- Scan(&res.ThingID, &res.NotificationDate, &res.CreatedAt, &res.UpdatedAt)
- if err != nil {
- return nil, err
- }
- return &res, nil
- }
- func (r ThingNotificationRepository) GetExpired(ctx context.Context) ([]models.ExtThingNotification, error) {
- var res []models.ExtThingNotification
- query, args, err := sq.Select("n.thing_id", "n.notification_date", "n.created_at", "n.updated_at", "t.title", "p.id", "p.title").
- From(thingNotificationTableName + " n").
- Join(thingTableName + " t ON t.id = n.thing_id").
- LeftJoin(placeThingTableName + " pt ON pt.thing_id = n.thing_id").
- LeftJoin(placeTableName + " p ON p.id = pt.place_id").
- PlaceholderFormat(sq.Dollar).
- Where(sq.Lt{"n.notification_date": "NOW()"}).
- OrderBy("n.notification_date DESC").
- ToSql()
- if err != nil {
- return nil, err
- }
- rows, err := r.db.QueryContext(ctx, query, args...)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- for rows.Next() {
- resRow := models.ExtThingNotification{}
- err = rows.Scan(
- &resRow.ThingID,
- &resRow.NotificationDate,
- &resRow.CreatedAt,
- &resRow.UpdatedAt,
- &resRow.ThingTitle,
- &resRow.PlaceID,
- &resRow.PlaceTitle,
- )
- if err != nil {
- return nil, err
- }
- res = append(res, resRow)
- }
- if err = rows.Err(); err != nil {
- return nil, err
- }
- return res, nil
- }
- func (r ThingNotificationRepository) Add(ctx context.Context, req models.AddThingNotificationRequest, tx *sql.Tx) error {
- query, args, err := sq.Insert(thingNotificationTableName).
- PlaceholderFormat(sq.Dollar).
- Columns("thing_id", "notification_date").
- Values(req.ThingID, req.NotificationDate).
- ToSql()
- if err != nil {
- return err
- }
- if tx == nil {
- _, err = r.db.ExecContext(ctx, query, args...)
- } else {
- _, err = tx.ExecContext(ctx, query, args...)
- }
- return err
- }
- func (r ThingNotificationRepository) Update(ctx context.Context, req models.UpdateThingNotificationRequest, tx *sql.Tx) error {
- query, args, err := sq.Update(thingNotificationTableName).
- PlaceholderFormat(sq.Dollar).
- Set("notification_date", req.NotificationDate).
- Set("updated_at", "NOW()").
- Where(sq.Eq{"thing_id": req.ThingID}).
- ToSql()
- if err != nil {
- return err
- }
- if tx == nil {
- _, err = r.db.ExecContext(ctx, query, args...)
- } else {
- _, err = tx.ExecContext(ctx, query, args...)
- }
- return err
- }
- func (r ThingNotificationRepository) Delete(ctx context.Context, thingID int, tx *sql.Tx) error {
- query, args, err := sq.Delete(thingNotificationTableName).
- PlaceholderFormat(sq.Dollar).
- Where(sq.Eq{"thing_id": thingID}).
- ToSql()
- if err != nil {
- return err
- }
- if tx == nil {
- _, err = r.db.ExecContext(ctx, query, args...)
- } else {
- _, err = tx.ExecContext(ctx, query, args...)
- }
- return err
- }
|