diff --git a/devcloud-mini/cmdb/apps/resource/api/api.go b/devcloud-mini/cmdb/apps/resource/api/api.go new file mode 100644 index 0000000..778f64e --- /dev/null +++ b/devcloud-mini/cmdb/apps/resource/api/api.go @@ -0,0 +1 @@ +package api diff --git a/devcloud-mini/cmdb/apps/resource/impl/resource.go b/devcloud-mini/cmdb/apps/resource/impl/resource.go index a50953c..594b358 100644 --- a/devcloud-mini/cmdb/apps/resource/impl/resource.go +++ b/devcloud-mini/cmdb/apps/resource/impl/resource.go @@ -4,12 +4,38 @@ import ( "context" "gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource" - "google.golang.org/grpc/codes" - "google.golang.org/grpc/status" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo/options" ) -func (s *ResourceServiceImpl) Search(context.Context, *resource.SearchRequest) (*resource.ResourceSet, error) { - return nil, status.Errorf(codes.Unimplemented, "method Search not implemented") +func (s *ResourceServiceImpl) Search(ctx context.Context, in *resource.SearchRequest) (*resource.ResourceSet, error) { + set := resource.NewResourceSet() + filter := bson.M{} + + if in.Keywords != "" { + filter["name"] = bson.M{"$regex": in.Keywords, "$options": "im"} + } + if in.Type != nil { + filter["type"] = *in.Type + } + for k, v := range in.Tag { + filter[k] = v + } + + // mongodb query + result, err := s.col.Find(ctx, filter, options.Find().SetLimit(in.PageSize).SetSkip(in.Skip())) + if err != nil { + return nil, err + } + for result.Next(ctx) { + res := &resource.Resource{} + if err := result.Decode(res); err != nil { + return nil, err + } + set.Items = append(set.Items, res) + } + + return set, nil } func (s *ResourceServiceImpl) Save(ctx context.Context, in *resource.Resource) (*resource.Resource, error) { @@ -26,6 +52,8 @@ func (s *ResourceServiceImpl) Save(ctx context.Context, in *resource.Resource) ( return in, nil } -func (s *ResourceServiceImpl) DeleteResource(context.Context, *resource.DeleteResourceRequest) error { - return nil +// $in +func (s *ResourceServiceImpl) DeleteResource(ctx context.Context, in *resource.DeleteResourceRequest) error { + _, err := s.col.DeleteMany(ctx, bson.M{"_id": bson.M{"$in": in.ResourceIds}}) + return err } diff --git a/devcloud-mini/cmdb/apps/resource/interface.go b/devcloud-mini/cmdb/apps/resource/interface.go index 881cf99..34f83ea 100644 --- a/devcloud-mini/cmdb/apps/resource/interface.go +++ b/devcloud-mini/cmdb/apps/resource/interface.go @@ -40,3 +40,13 @@ func (s *Resource) Validate() error { return nil } + +func (s *SearchRequest) Skip() int64 { + return (s.PageNumber - 1) * s.PageSize +} + +func NewResourceSet() *ResourceSet { + return &ResourceSet{ + Items: []*Resource{}, + } +} diff --git a/devcloud-mini/cmdb/docs/gorestful.drawio b/devcloud-mini/cmdb/docs/gorestful.drawio new file mode 100644 index 0000000..5558f36 --- /dev/null +++ b/devcloud-mini/cmdb/docs/gorestful.drawio @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file