2022-11-19 15:13:47 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2022-12-07 22:11:44 +01:00
|
|
|
"github.com/jmoiron/sqlx"
|
2022-11-19 15:13:47 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
2022-12-11 03:14:42 +01:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/sq"
|
2022-11-19 15:13:47 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Subscription struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
SubscriptionID SubscriptionID
|
|
|
|
SubscriberUserID UserID
|
|
|
|
ChannelOwnerUserID UserID
|
|
|
|
ChannelID ChannelID
|
2022-11-19 15:13:47 +01:00
|
|
|
ChannelName string
|
|
|
|
TimestampCreated time.Time
|
|
|
|
Confirmed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Subscription) JSON() SubscriptionJSON {
|
|
|
|
return SubscriptionJSON{
|
|
|
|
SubscriptionID: s.SubscriptionID,
|
|
|
|
SubscriberUserID: s.SubscriberUserID,
|
|
|
|
ChannelOwnerUserID: s.ChannelOwnerUserID,
|
|
|
|
ChannelID: s.ChannelID,
|
|
|
|
ChannelName: s.ChannelName,
|
|
|
|
TimestampCreated: s.TimestampCreated.Format(time.RFC3339Nano),
|
|
|
|
Confirmed: s.Confirmed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionJSON struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
SubscriptionID SubscriptionID `json:"subscription_id"`
|
|
|
|
SubscriberUserID UserID `json:"subscriber_user_id"`
|
|
|
|
ChannelOwnerUserID UserID `json:"channel_owner_user_id"`
|
|
|
|
ChannelID ChannelID `json:"channel_id"`
|
|
|
|
ChannelName string `json:"channel_name"`
|
|
|
|
TimestampCreated string `json:"timestamp_created"`
|
|
|
|
Confirmed bool `json:"confirmed"`
|
2022-11-19 15:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type SubscriptionDB struct {
|
2022-11-20 22:18:24 +01:00
|
|
|
SubscriptionID SubscriptionID `db:"subscription_id"`
|
|
|
|
SubscriberUserID UserID `db:"subscriber_user_id"`
|
|
|
|
ChannelOwnerUserID UserID `db:"channel_owner_user_id"`
|
|
|
|
ChannelID ChannelID `db:"channel_id"`
|
|
|
|
ChannelName string `db:"channel_name"`
|
|
|
|
TimestampCreated int64 `db:"timestamp_created"`
|
|
|
|
Confirmed int `db:"confirmed"`
|
2022-11-19 15:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s SubscriptionDB) Model() Subscription {
|
|
|
|
return Subscription{
|
|
|
|
SubscriptionID: s.SubscriptionID,
|
|
|
|
SubscriberUserID: s.SubscriberUserID,
|
|
|
|
ChannelOwnerUserID: s.ChannelOwnerUserID,
|
|
|
|
ChannelID: s.ChannelID,
|
|
|
|
ChannelName: s.ChannelName,
|
|
|
|
TimestampCreated: time.UnixMilli(s.TimestampCreated),
|
|
|
|
Confirmed: s.Confirmed != 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
func DecodeSubscription(r *sqlx.Rows) (Subscription, error) {
|
2022-12-11 03:14:42 +01:00
|
|
|
data, err := sq.ScanSingle[SubscriptionDB](r, true)
|
2022-11-19 15:13:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return Subscription{}, err
|
|
|
|
}
|
|
|
|
return data.Model(), nil
|
|
|
|
}
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
func DecodeSubscriptions(r *sqlx.Rows) ([]Subscription, error) {
|
2022-12-11 03:14:42 +01:00
|
|
|
data, err := sq.ScanAll[SubscriptionDB](r, true)
|
2022-11-19 15:13:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return langext.ArrMap(data, func(v SubscriptionDB) Subscription { return v.Model() }), nil
|
|
|
|
}
|