2023-01-06 00:39:21 +01:00
|
|
|
package primary
|
2022-11-19 23:16:54 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"database/sql"
|
2022-12-07 23:32:58 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 23:16:54 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) CreateSubscription(ctx 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
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
|
2022-12-22 11:22:36 +01:00
|
|
|
res, err := tx.Exec(ctx, "INSERT INTO subscriptions (subscriber_user_id, channel_owner_user_id, channel_internal_name, channel_id, timestamp_created, confirmed) VALUES (:suid, :ouid, :cnam, :cid, :ts, :conf)", sq.PP{
|
2022-12-07 22:11:44 +01:00
|
|
|
"suid": subscriberUID,
|
|
|
|
"ouid": channel.OwnerUserID,
|
2022-12-22 11:22:36 +01:00
|
|
|
"cnam": channel.InternalName,
|
2022-12-07 22:11:44 +01:00
|
|
|
"cid": channel.ChannelID,
|
|
|
|
"ts": time2DB(now),
|
|
|
|
"conf": confirmed,
|
|
|
|
})
|
2022-11-19 23:16:54 +01:00
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
liid, err := res.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return models.Subscription{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.Subscription{
|
2022-12-22 11:22:36 +01:00
|
|
|
SubscriptionID: models.SubscriptionID(liid),
|
|
|
|
SubscriberUserID: subscriberUID,
|
|
|
|
ChannelOwnerUserID: channel.OwnerUserID,
|
|
|
|
ChannelID: channel.ChannelID,
|
|
|
|
ChannelInternalName: channel.InternalName,
|
|
|
|
TimestampCreated: now,
|
|
|
|
Confirmed: confirmed,
|
2022-11-19 23:16:54 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) ListSubscriptionsByChannel(ctx TxContext, 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 channel_id = :cid", sq.PP{"cid": channelID})
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-22 12:43:40 +01:00
|
|
|
func (db *Database) ListSubscriptionsByChannelOwner(ctx TxContext, ownerUserID models.UserID, confirmed *bool) ([]models.Subscription, error) {
|
2022-11-19 23:16:54 +01:00
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-12-21 18:14:13 +01:00
|
|
|
cond := ""
|
|
|
|
if confirmed != nil && *confirmed {
|
|
|
|
cond = " AND confirmed = 1"
|
|
|
|
} else if confirmed != nil && !*confirmed {
|
|
|
|
cond = " AND confirmed = 0"
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM subscriptions WHERE channel_owner_user_id = :ouid"+cond, sq.PP{"ouid": ownerUserID})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := models.DecodeSubscriptions(rows)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) ListSubscriptionsBySubscriber(ctx TxContext, subscriberUserID models.UserID, confirmed *bool) ([]models.Subscription, error) {
|
|
|
|
tx, err := ctx.GetOrCreateTransaction(db)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cond := ""
|
|
|
|
if confirmed != nil && *confirmed {
|
|
|
|
cond = " AND confirmed = 1"
|
|
|
|
} else if confirmed != nil && !*confirmed {
|
|
|
|
cond = " AND confirmed = 0"
|
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := tx.Query(ctx, "SELECT * FROM subscriptions WHERE subscriber_user_id = :suid"+cond, sq.PP{"suid": subscriberUserID})
|
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
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) GetSubscription(ctx 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
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) GetSubscriptionBySubscriber(ctx 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)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &user, nil
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) DeleteSubscription(ctx 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
|
|
|
|
}
|
|
|
|
|
2022-11-20 22:18:24 +01:00
|
|
|
func (db *Database) UpdateSubscriptionConfirmed(ctx 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
|
|
|
|
}
|