forked from crawlab-team/crawlab-core
/
plugin_service.go
45 lines (39 loc) · 1.36 KB
/
plugin_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package service
import (
"github.com/xulei324/crawlab-core/errors"
"github.com/xulei324/crawlab-core/interfaces"
models2 "github.com/xulei324/crawlab-core/models/models"
"github.com/crawlab-team/crawlab-db/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func convertTypePlugin(d interface{}, err error) (res *models2.Plugin, err2 error) {
if err != nil {
return nil, err
}
res, ok := d.(*models2.Plugin)
if !ok {
return nil, errors.ErrorModelInvalidType
}
return res, nil
}
func (svc *Service) GetPluginById(id primitive.ObjectID) (res *models2.Plugin, err error) {
d, err := svc.GetBaseService(interfaces.ModelIdPlugin).GetById(id)
return convertTypePlugin(d, err)
}
func (svc *Service) GetPlugin(query bson.M, opts *mongo.FindOptions) (res *models2.Plugin, err error) {
d, err := svc.GetBaseService(interfaces.ModelIdPlugin).Get(query, opts)
return convertTypePlugin(d, err)
}
func (svc *Service) GetPluginList(query bson.M, opts *mongo.FindOptions) (res []models2.Plugin, err error) {
l, err := svc.GetBaseService(interfaces.ModelIdPlugin).GetList(query, opts)
for _, doc := range l.GetModels() {
d := doc.(*models2.Plugin)
res = append(res, *d)
}
return res, nil
}
func (svc *Service) GetPluginByName(name string) (res *models2.Plugin, err error) {
query := bson.M{"name": name}
return svc.GetPlugin(query, nil)
}