Skip to content

Commit

Permalink
feat: updated grpc server
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Jun 5, 2024
1 parent 96b0f0a commit 56f6581
Show file tree
Hide file tree
Showing 5 changed files with 720 additions and 45 deletions.
14 changes: 7 additions & 7 deletions grpc/payload/model_service_v2_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type ModelServiceV2Payload[T any] struct {
Type string `json:"type"`
Id primitive.ObjectID `json:"_id"`
Query bson.M `json:"query"`
FindOptions mongo.FindOptions `json:"find_options"`
Model T `json:"model"`
Update bson.M `json:"update"`
Models []T `json:"models"`
Type string `json:"type,omitempty"`
Id primitive.ObjectID `json:"_id,omitempty"`
Query bson.M `json:"query,omitempty"`
FindOptions *mongo.FindOptions `json:"find_options,omitempty"`
Model T `json:"model,omitempty"`
Update bson.M `json:"update,omitempty"`
Models []T `json:"models,omitempty"`
}
134 changes: 120 additions & 14 deletions grpc/server/model_base_service_v2_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/crawlab-team/crawlab-core/models/service"
grpc "github.com/crawlab-team/crawlab-grpc"
"go.mongodb.org/mongo-driver/bson"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"reflect"
)

Expand Down Expand Up @@ -49,51 +47,159 @@ func (svr ModelBaseServiceV2Server) GetById(ctx context.Context, req *grpc.Reque
}

func (svr ModelBaseServiceV2Server) Get(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method Get not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
data, err := svr.GetModelService(p.Type).Get(p.Query, p.FindOptions)
if err != nil {
return HandleError(err)
}
return HandleSuccessWithData(data)
}

func (svr ModelBaseServiceV2Server) GetList(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method GetList not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
data, err := svr.GetModelService(p.Type).GetList(p.Query, p.FindOptions)
if err != nil {
return HandleError(err)
}
return HandleSuccessWithData(data)
}

func (svr ModelBaseServiceV2Server) DeleteById(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteById not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).DeleteById(p.Id)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) DeleteList(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteList not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).DeleteList(p.Query)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) UpdateById(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateById not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).UpdateById(p.Id, p.Update)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) UpdateOne(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateOne not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).UpdateOne(p.Query, p.Update)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) UpdateMany(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateMany not implemented")
var p payload.ModelServiceV2Payload[any]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).UpdateMany(p.Query, p.Update)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) ReplaceById(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method ReplaceById not implemented")
var p payload.ModelServiceV2Payload[bson.M]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).ReplaceById(p.Id, p.Model)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) Replace(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method Replace not implemented")
var p payload.ModelServiceV2Payload[bson.M]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
err = svr.GetModelService(p.Type).Replace(p.Query, p.Model)
if err != nil {
return HandleError(err)
}
return HandleSuccess()
}

func (svr ModelBaseServiceV2Server) InsertOne(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method InsertOne not implemented")
var p payload.ModelServiceV2Payload[bson.M]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
id, err := svr.GetModelService(p.Type).InsertOne(p.Model)
if err != nil {
return HandleError(err)
}
return HandleSuccessWithData(id)
}

func (svr ModelBaseServiceV2Server) InsertMany(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method InsertMany not implemented")
var p payload.ModelServiceV2Payload[bson.M]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
ids, err := svr.GetModelService(p.Type).InsertMany(p.Models)
if err != nil {
return HandleError(err)
}
return HandleSuccessWithData(ids)
}

func (svr ModelBaseServiceV2Server) Count(ctx context.Context, req *grpc.Request) (res *grpc.Response, err error) {
return nil, status.Errorf(codes.Unimplemented, "method Count not implemented")
var p payload.ModelServiceV2Payload[bson.M]
err = json.Unmarshal(req.Data, &p)
if err != nil {
return nil, err
}
count, err := svr.GetModelService(p.Type).Count(p.Query)
if err != nil {
return HandleError(err)
}
return HandleSuccessWithData(count)
}

func (svr ModelBaseServiceV2Server) GetModelService(typeName string) *service.ModelServiceV2[bson.M] {
Expand Down
2 changes: 1 addition & 1 deletion grpc/server/server_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func NewGrpcServerV2() (svr *GrpcServerV2, err error) {

var _serverV2 *GrpcServerV2

func GetServerV2() (svr *GrpcServerV2, err error) {
func GetGrpcServerV2() (svr *GrpcServerV2, err error) {
if _serverV2 != nil {
return _serverV2, nil
}
Expand Down
Loading

0 comments on commit 56f6581

Please sign in to comment.