2023-01-06 00:39:21 +01:00
|
|
|
package primary
|
2022-11-19 23:16:54 +01:00
|
|
|
|
|
|
|
import (
|
2023-07-27 17:44:06 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
2022-11-19 23:16:54 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"database/sql"
|
2023-07-30 15:58:37 +02:00
|
|
|
"errors"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) CreateSubscription(ctx db.TxContext, subscriberUID models.UserID, channel models.Channel, confirmed bool) (models.Subscription, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:27:38 +02:00
|
|
|
entity := models.SubscriptionDB{
|
|
|
|
SubscriptionID: models.NewSubscriptionID(),
|
|
|
|
SubscriberUserID: subscriberUID,
|
|
|
|
ChannelOwnerUserID: channel.OwnerUserID,
|
|
|
|
ChannelID: channel.ChannelID,
|
|
|
|
ChannelInternalName: channel.InternalName,
|
|
|
|
TimestampCreated: time2DB(time.Now()),
|
|
|
|
Confirmed: bool2DB(confirmed),
|
|
|
|
}
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2023-05-28 22:27:38 +02:00
|
|
|
_, err = sq.InsertSingle(ctx, tx, "subscriptions", entity)
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
2023-05-28 22:27:38 +02:00
|
|
|
return entity.Model(), nil
|
2022-11-19 23:16:54 +01:00
|
|
|
}
|
|
|
|
|
2023-07-30 15:58:37 +02:00
|
|
|
func (db *Database) ListSubscriptions(ctx db.TxContext, filter models.SubscriptionFilter) ([]models.Subscription, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-07-30 15:58:37 +02:00
|
|
|
filterCond, filterJoin, prepParams, err := filter.SQL()
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2023-07-30 15:58:37 +02:00
|
|
|
orderClause := " ORDER BY subscriptions.timestamp_created DESC, subscriptions.subscription_id DESC "
|
2022-12-21 18:14:13 +01:00
|
|
|
|
2023-07-30 15:58:37 +02:00
|
|
|
sqlQuery := "SELECT " + "subscriptions.*" + " FROM subscriptions " + filterJoin + " WHERE ( " + filterCond + " ) " + orderClause
|
2023-01-14 00:48:51 +01:00
|
|
|
|
2023-07-30 15:58:37 +02:00
|
|
|
rows, err := tx.Query(ctx, sqlQuery, prepParams)
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := models.DecodeSubscriptions(rows)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) GetSubscription(ctx db.TxContext, subid models.SubscriptionID) (models.Subscription, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM subscriptions WHERE subscription_id = :sid LIMIT 1", sq.PP{"sid": subid})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sub, err := models.DecodeSubscription(rows)
|
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sub, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) GetSubscriptionBySubscriber(ctx db.TxContext, subscriberId models.UserID, channelId models.ChannelID) (*models.Subscription, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM subscriptions WHERE subscriber_user_id = :suid AND channel_id = :cid LIMIT 1", sq.PP{
|
|
|
|
"suid": subscriberId,
|
|
|
|
"cid": channelId,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := models.DecodeSubscription(rows)
|
2023-07-30 15:58:37 +02:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2022-11-19 23:16:54 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) DeleteSubscription(ctx db.TxContext, subid models.SubscriptionID) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "DELETE FROM subscriptions WHERE subscription_id = :sid", sq.PP{"sid": subid})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-27 17:44:06 +02:00
|
|
|
func (db *Database) UpdateSubscriptionConfirmed(ctx db.TxContext, subscriptionID models.SubscriptionID, confirmed bool) error {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
_, err = tx.Exec(ctx, "UPDATE subscriptions SET confirmed = :conf WHERE subscription_id = :sid", sq.PP{
|
|
|
|
"conf": confirmed,
|
|
|
|
"sid": subscriptionID,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|