补充impl

This commit is contained in:
yumaojun03 2025-03-09 10:05:28 +08:00
parent bcd2f45575
commit 24b4e16c76
4 changed files with 91 additions and 6 deletions

View File

@ -0,0 +1 @@
package api

View File

@ -4,12 +4,38 @@ import (
"context" "context"
"gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource" "gitlab.com/go-course-project/go17/devcloud-mini/cmdb/apps/resource"
"google.golang.org/grpc/codes" "go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc/status" "go.mongodb.org/mongo-driver/mongo/options"
) )
func (s *ResourceServiceImpl) Search(context.Context, *resource.SearchRequest) (*resource.ResourceSet, error) { func (s *ResourceServiceImpl) Search(ctx context.Context, in *resource.SearchRequest) (*resource.ResourceSet, error) {
return nil, status.Errorf(codes.Unimplemented, "method Search not implemented") 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) { 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 return in, nil
} }
func (s *ResourceServiceImpl) DeleteResource(context.Context, *resource.DeleteResourceRequest) error { // $in
return nil 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
} }

View File

@ -40,3 +40,13 @@ func (s *Resource) Validate() error {
return nil return nil
} }
func (s *SearchRequest) Skip() int64 {
return (s.PageNumber - 1) * s.PageSize
}
func NewResourceSet() *ResourceSet {
return &ResourceSet{
Items: []*Resource{},
}
}

View File

@ -0,0 +1,46 @@
<mxfile host="65bd71144e">
<diagram id="GJJScDLq7OnnltmnD1WL" name="第 1 页">
<mxGraphModel dx="873" dy="339" grid="1" gridSize="10" guides="1" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="827" pageHeight="1169" math="0" shadow="0">
<root>
<mxCell id="0"/>
<mxCell id="1" parent="0"/>
<mxCell id="2" value="" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="20" y="140" width="610" height="160" as="geometry"/>
</mxCell>
<mxCell id="3" value="contailer(gin.Engine)" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="240" y="100" width="150" height="30" as="geometry"/>
</mxCell>
<mxCell id="4" value="Web Service(ws)&lt;div&gt;(gin.Group)&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="50" y="160" width="160" height="110" as="geometry"/>
</mxCell>
<mxCell id="5" value="gorestful" style="text;html=1;align=center;verticalAlign=middle;whiteSpace=wrap;rounded=0;" vertex="1" parent="1">
<mxGeometry x="50" y="50" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="6" value="Web Service(ws)&lt;div&gt;(gin.Group)&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="245" y="160" width="160" height="110" as="geometry"/>
</mxCell>
<mxCell id="7" value="Web Service(ws)&lt;div&gt;(gin.Group)&lt;/div&gt;" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="450" y="160" width="160" height="110" as="geometry"/>
</mxCell>
<mxCell id="8" value="route" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="60" y="230" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="9" value="route" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="140" y="230" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="10" value="route" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="260" y="230" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="11" value="route" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="330" y="230" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="12" value="route" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="470" y="230" width="60" height="30" as="geometry"/>
</mxCell>
<mxCell id="13" value="route" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="540" y="230" width="60" height="30" as="geometry"/>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>