swirl/dao/mongo/registry.go

60 lines
1.5 KiB
Go
Raw Permalink Normal View History

2017-09-26 12:50:09 +00:00
package mongo
import (
2021-12-06 12:24:22 +00:00
"context"
2017-09-26 12:50:09 +00:00
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
2017-09-26 12:50:09 +00:00
)
2021-12-06 12:24:22 +00:00
const Registry = "registry"
2021-12-23 11:28:31 +00:00
func (d *Dao) RegistryCreate(ctx context.Context, registry *dao.Registry) (err error) {
2021-12-06 12:24:22 +00:00
return d.create(ctx, Registry, registry)
2017-09-26 12:50:09 +00:00
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RegistryUpdate(ctx context.Context, registry *dao.Registry) (err error) {
2021-12-06 12:24:22 +00:00
update := bson.M{
"name": registry.Name,
"url": registry.URL,
"username": registry.Username,
2021-12-16 08:11:16 +00:00
"updated_at": registry.UpdatedAt,
"updated_by": registry.UpdatedBy,
2021-12-06 12:24:22 +00:00
}
if registry.Password != "" {
update["password"] = registry.Password
}
return d.update(ctx, Registry, registry.ID, bson.M{"$set": update})
2017-09-26 12:50:09 +00:00
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RegistryGetAll(ctx context.Context) (registries []*dao.Registry, err error) {
registries = []*dao.Registry{}
2021-12-06 12:24:22 +00:00
err = d.fetch(ctx, Registry, bson.M{}, &registries)
2017-09-26 12:50:09 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RegistryGet(ctx context.Context, id string) (registry *dao.Registry, err error) {
registry = &dao.Registry{}
2021-12-06 12:24:22 +00:00
found, err := d.find(ctx, Registry, id, registry)
if !found {
return nil, err
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RegistryGetByURL(ctx context.Context, url string) (registry *dao.Registry, err error) {
registry = &dao.Registry{}
2021-12-06 12:24:22 +00:00
err = d.db.Collection(Registry).FindOne(ctx, bson.M{"url": url}).Decode(registry)
if err == mongo.ErrNoDocuments {
return nil, nil
} else if err != nil {
return nil, err
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (d *Dao) RegistryDelete(ctx context.Context, id string) (err error) {
return d.delete(ctx, Registry, id)
}