forked from longtao.wu/droboticflow
96 lines
3.2 KiB
Go
96 lines
3.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func organizationPluginKey(organizationID string, pluginID string) string {
|
|
return normalizedOrganizationID(organizationID) + ":" + strings.TrimSpace(pluginID)
|
|
}
|
|
|
|
func (store *MemoryStore) ListOrganizationPlugins(_ context.Context, organizationID string) ([]domain.OrganizationPluginRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.OrganizationPluginRecord, 0)
|
|
for _, item := range store.organizationPlugins {
|
|
if normalizedOrganizationID(item.OrganizationID) == normalizedOrganizationID(organizationID) {
|
|
items = append(items, item)
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool { return items[i].PluginID < items[j].PluginID })
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetOrganizationPlugin(_ context.Context, organizationID string, pluginID string) (*domain.OrganizationPluginRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.organizationPlugins[organizationPluginKey(organizationID, pluginID)]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
copy := item
|
|
return ©, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertOrganizationPlugin(_ context.Context, item domain.OrganizationPluginRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
now := nowISO()
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
item.PluginID = strings.TrimSpace(item.PluginID)
|
|
item.ID = organizationPluginKey(item.OrganizationID, item.PluginID)
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
store.organizationPlugins[item.ID] = item
|
|
return nil
|
|
}
|
|
|
|
func (store *MongoStore) ListOrganizationPlugins(ctx context.Context, organizationID string) ([]domain.OrganizationPluginRecord, error) {
|
|
cursor, err := store.organizationPlugins.Find(ctx, bson.M{"organizationId": normalizedOrganizationID(organizationID)}, options.Find().SetSort(bson.D{{Key: "pluginId", Value: 1}}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.OrganizationPluginRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetOrganizationPlugin(ctx context.Context, organizationID string, pluginID string) (*domain.OrganizationPluginRecord, error) {
|
|
var item domain.OrganizationPluginRecord
|
|
err := store.organizationPlugins.FindOne(ctx, bson.M{
|
|
"organizationId": normalizedOrganizationID(organizationID),
|
|
"pluginId": strings.TrimSpace(pluginID),
|
|
}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertOrganizationPlugin(ctx context.Context, item domain.OrganizationPluginRecord) error {
|
|
now := nowISO()
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
item.PluginID = strings.TrimSpace(item.PluginID)
|
|
item.ID = organizationPluginKey(item.OrganizationID, item.PluginID)
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
_, err := store.organizationPlugins.UpdateOne(ctx, bson.M{
|
|
"organizationId": item.OrganizationID,
|
|
"pluginId": item.PluginID,
|
|
}, bson.M{"$set": item}, options.Update().SetUpsert(true))
|
|
return err
|
|
}
|