106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
|
|
commonv1 "code.icb4dc0.de/buildr/api/generated/common/v1"
|
|
|
|
"code.icb4dc0.de/buildr/buildr/modules/state/ent"
|
|
"code.icb4dc0.de/buildr/buildr/modules/state/ent/plugin"
|
|
"code.icb4dc0.de/buildr/buildr/modules/state/ent/pluginmodule"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
var _ Plugins = (*EntPluginsRepo)(nil)
|
|
|
|
func NewEntPluginsRepo(client *ent.Client) *EntPluginsRepo {
|
|
return &EntPluginsRepo{
|
|
client: client,
|
|
}
|
|
}
|
|
|
|
type EntPluginsRepo struct {
|
|
client *ent.Client
|
|
}
|
|
|
|
func (e EntPluginsRepo) Remove(ctx context.Context, id uuid.UUID) error {
|
|
return e.client.Plugin.DeleteOneID(id).Exec(ctx)
|
|
}
|
|
|
|
func (e EntPluginsRepo) ModulesForPlugin(ctx context.Context, id uuid.UUID) ([]PluginModule, error) {
|
|
modules, err := e.client.PluginModule.
|
|
Query().
|
|
Where(pluginmodule.HasPluginWith(plugin.ID(id))).
|
|
All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make([]PluginModule, 0, len(modules))
|
|
for _, module := range modules {
|
|
module := module
|
|
result = append(result, PluginModule{
|
|
ID: &module.ID,
|
|
PluginID: id,
|
|
Type: module.Type,
|
|
Category: commonv1.Category(module.Category),
|
|
DefaultSpec: module.DefaultSpec,
|
|
})
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func (e EntPluginsRepo) List(ctx context.Context) ([]Plugin, error) {
|
|
result, err := e.client.Plugin.Query().All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
plugins := make([]Plugin, 0, len(result))
|
|
for _, pluginEntity := range result {
|
|
if p, err := pluginFromEntity(pluginEntity); err != nil {
|
|
return nil, err
|
|
} else {
|
|
plugins = append(plugins, p)
|
|
}
|
|
}
|
|
|
|
return plugins, nil
|
|
}
|
|
|
|
func (e EntPluginsRepo) UpsertPlugin(ctx context.Context, p Plugin) (pluginID uuid.UUID, err error) {
|
|
return e.client.Plugin.
|
|
Create().
|
|
SetNillableID(p.ID).
|
|
SetName(p.Name).
|
|
SetLocalPath(p.LocalPath).
|
|
SetURL(p.URL.String()).
|
|
SetHash(p.Hash).
|
|
OnConflict().
|
|
UpdateNewValues().
|
|
ID(ctx)
|
|
}
|
|
|
|
func (e EntPluginsRepo) UpsertModules(ctx context.Context, modules []PluginModule) error {
|
|
for _, module := range modules {
|
|
upsertErr := e.client.PluginModule.
|
|
Create().
|
|
SetNillableID(module.ID).
|
|
SetPluginID(module.PluginID).
|
|
SetType(module.Type).
|
|
SetCategory(int(module.Category)).
|
|
SetDefaultSpec(module.DefaultSpec).
|
|
OnConflict().
|
|
UpdateNewValues().
|
|
Exec(ctx)
|
|
|
|
if upsertErr != nil {
|
|
return upsertErr
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|