[+] add function GetSubscriberByUser for the agenda-widget
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"ariga.io/atlas/sql/postgres"
|
||||
"ariga.io/atlas/sql/schema"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/lib/pq"
|
||||
"github.com/spf13/viper"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"ariga.io/atlas/sql/postgres"
|
||||
"ariga.io/atlas/sql/schema"
|
||||
"github.com/lib/pq"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type PostgresqlStorage struct {
|
||||
@@ -187,7 +188,7 @@ func (psql PostgresqlStorage) GetEvent(eventID string) (*Event, error) {
|
||||
return nil, err
|
||||
}
|
||||
event.Data = data
|
||||
subscriptions, err := psql.getSubscriptions(eventID)
|
||||
subscriptions, err := psql.GetSubscription(eventID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -247,7 +248,7 @@ func (psql PostgresqlStorage) GetEvents(namespaces []string) ([]Event, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
event.Subscriptions, err = psql.getSubscriptions(event.ID)
|
||||
event.Subscriptions, err = psql.GetSubscription(event.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -438,7 +439,7 @@ func (psql PostgresqlStorage) UpdateSubscription(eventid string, subscriber stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func (psql PostgresqlStorage) getSubscriptions(eventID string) ([]Subscription, error) {
|
||||
func (psql PostgresqlStorage) GetSubscription(eventID string) ([]Subscription, error) {
|
||||
var subscriptions []Subscription
|
||||
subscriptionQuery := fmt.Sprintf(`
|
||||
SELECT id, subscriber, tags, created_at, data
|
||||
@@ -476,6 +477,52 @@ func (psql PostgresqlStorage) getSubscriptions(eventID string) ([]Subscription,
|
||||
return subscriptions, nil
|
||||
}
|
||||
|
||||
func (psql PostgresqlStorage) GetSubscriber(subscriber string) ([]Subscription, error) {
|
||||
var subscriptions []Subscription
|
||||
subscriptionQuery := fmt.Sprintf("SELECT id, event_id, subscriber, tags, created_at, data FROM %s WHERE subscriber = $1", psql.Tables["subscription"])
|
||||
rows, err := psql.DbConnection.Query(subscriptionQuery, subscriber)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var subscription Subscription
|
||||
var tags pq.StringArray
|
||||
var dataSubscription []byte
|
||||
err := rows.Scan(
|
||||
&subscription.ID,
|
||||
&subscription.EventID,
|
||||
&subscription.Subscriber,
|
||||
&tags,
|
||||
&subscription.CreatedAt,
|
||||
&dataSubscription,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
subscription.Tags = []string(tags)
|
||||
data := make(map[string]any)
|
||||
err = json.Unmarshal(dataSubscription, &data)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
subscription.Data = data
|
||||
subscriptions = append(subscriptions, subscription)
|
||||
}
|
||||
return subscriptions, nil
|
||||
}
|
||||
|
||||
func (psql PostgresqlStorage) GetSubscriptionByUser(subscriber string) ([]Subscription, error) {
|
||||
events, err := psql.GetSubscriber(subscriber)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (psql PostgresqlStorage) Migrate() error {
|
||||
ctx := context.Background()
|
||||
driver, err := postgres.Open(psql.DbConnection)
|
||||
|
||||
Reference in New Issue
Block a user