swirl/dao/mongo/registry.go
cuigh ca0789ac4e Change mgo pkg
Since original pkg is unmaintained
2018-01-19 16:21:46 +08:00

61 lines
1.3 KiB
Go

package mongo
import (
"time"
"github.com/cuigh/swirl/model"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
func (d *Dao) RegistryCreate(registry *model.Registry) (err error) {
d.do(func(db *database) {
err = db.C("registry").Insert(registry)
})
return
}
func (d *Dao) RegistryUpdate(registry *model.Registry) (err error) {
d.do(func(db *database) {
update := bson.M{
"name": registry.Name,
"url": registry.URL,
"username": registry.Username,
"updated_at": time.Now(),
}
if registry.Password != "" {
update["password"] = registry.Password
}
err = db.C("registry").UpdateId(registry.ID, bson.M{"$set": update})
})
return
}
func (d *Dao) RegistryList() (registries []*model.Registry, err error) {
d.do(func(db *database) {
registries = []*model.Registry{}
err = db.C("registry").Find(nil).All(&registries)
})
return
}
func (d *Dao) RegistryGet(id string) (registry *model.Registry, err error) {
d.do(func(db *database) {
registry = &model.Registry{}
err = db.C("registry").FindId(id).One(registry)
if err == mgo.ErrNotFound {
err = nil
} else if err != nil {
registry = nil
}
})
return
}
func (d *Dao) RegistryDelete(id string) (err error) {
d.do(func(db *database) {
err = db.C("registry").RemoveId(id)
})
return
}