[+] add function GetSubscriberByUser for the agenda-widget

This commit is contained in:
2023-09-06 13:34:43 +02:00
parent f06ed52547
commit be0835273e
16 changed files with 551 additions and 1556 deletions

View File

@@ -24,6 +24,7 @@ type Event struct {
type Subscription struct {
ID string `json:"id" bson:"_id"`
EventID string `json:"event_id"`
Subscriber string `json:"subscriber"`
Tags []string `json:"tags"`
CreatedAt time.Time `json:"created_at,omitempty"`

View File

@@ -153,3 +153,11 @@ func (s MongoDBStorage) UpdateEvent(event Event) error {
return nil
}
func (psql MongoDBStorage) GetSubscriber(subscriber string) ([]Subscription, error) {
return nil, nil
}
func (psql MongoDBStorage) GetSubscriptionByUser(subscriber string) ([]Subscription, error) {
return nil, nil
}

View File

@@ -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)

View File

@@ -3,11 +3,12 @@ package storage
import (
"context"
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/spf13/viper"
"testing"
"time"
)
var cfg *viper.Viper

View File

@@ -13,6 +13,7 @@ type Storage interface {
AddSubscription(eventid string, subscription Subscription) error
UpdateSubscription(eventid string, subscriber string, deletesubscription Subscription) error
UpdateEvent(Event) error
GetSubscriptionByUser(subscriber string) ([]Subscription, error)
}
type StorageImpl struct {
}