From 77f75df24924f57bb93acf8798d7f18dc7fead83 Mon Sep 17 00:00:00 2001 From: Kamil Samigullin Date: Wed, 17 Oct 2018 11:01:55 +0300 Subject: [PATCH] refactor grpc layer --- env/client/grpc/schema.create.yml | 5 +- env/client/grpc/template.create.yml | 26 +---- pkg/server/grpc/server_schema.go | 171 ++++++++++------------------ pkg/server/grpc/server_template.go | 147 ++++++++---------------- 4 files changed, 109 insertions(+), 240 deletions(-) diff --git a/env/client/grpc/schema.create.yml b/env/client/grpc/schema.create.yml index d9de528..bd2f697 100644 --- a/env/client/grpc/schema.create.yml +++ b/env/client/grpc/schema.create.yml @@ -2,7 +2,4 @@ kind: Schema payload: id: 10000000-2000-4000-8000-160000000004 title: Email subscription - definition: | -
- -
+ definition: ~ diff --git a/env/client/grpc/template.create.yml b/env/client/grpc/template.create.yml index b52126c..b8ddae6 100644 --- a/env/client/grpc/template.create.yml +++ b/env/client/grpc/template.create.yml @@ -2,28 +2,4 @@ kind: Template payload: id: 10000000-2000-4000-8000-160000000005 title: Subscription template - definition: | - {{- define "forma.body" -}} -
- {{- with .Schema.Input "email" -}} -
- -
-
- - mail - -
- {{- template "forma.input" . -}} -
-
-
- {{- end -}} -
- {{- end -}} - {{- define "forma.submit" -}} - {{- template "forma.powered_by" . -}} -
- -
- {{- end -}} + definition: ~ diff --git a/pkg/server/grpc/server_schema.go b/pkg/server/grpc/server_schema.go index 051fc08..18414b4 100644 --- a/pkg/server/grpc/server_schema.go +++ b/pkg/server/grpc/server_schema.go @@ -6,9 +6,7 @@ import ( "encoding/xml" "strings" - "github.com/golang/protobuf/ptypes/timestamp" "github.com/kamilsk/form-api/pkg/domain" - "github.com/kamilsk/form-api/pkg/errors" "github.com/kamilsk/form-api/pkg/server/grpc/middleware" "github.com/kamilsk/form-api/pkg/storage/query" "google.golang.org/grpc/codes" @@ -26,141 +24,90 @@ type schemaServer struct { // Create TODO issue#173 func (server *schemaServer) Create(ctx context.Context, req *CreateSchemaRequest) (*CreateSchemaResponse, error) { - data := query.CreateSchema{Title: req.Title} - if err := xml.NewDecoder(strings.NewReader(req.Definition)).Decode(&data.Definition); err != nil { - return nil, status.Errorf(codes.InvalidArgument, - "trying to unmarshal XML `%s` of the schema definition", - req.Definition) - } - if req.Id != "" { - id := domain.ID(req.Id) - data.ID = &id - } - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err - } - schema, err := server.storage.CreateSchema(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to create the schema %q", req.Definition) + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr + } + data := query.CreateSchema{ID: ptrToID(req.Id), Title: req.Title} + if len(req.Definition) > 0 { + if decodeErr := xml.NewDecoder(strings.NewReader(req.Definition)).Decode(&data.Definition); decodeErr != nil { + return nil, status.Errorf(codes.InvalidArgument, + "trying to unmarshal XML `%s` of the schema definition: %+v", + req.Definition, decodeErr) } - return nil, status.Errorf(codes.Unknown, "trying to create the schema %q", req.Definition) + } + schema, createErr := server.storage.CreateSchema(ctx, tokenID, data) + if createErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", createErr) } return &CreateSchemaResponse{ - Id: string(schema.ID), - CreatedAt: ×tamp.Timestamp{ - Seconds: int64(schema.CreatedAt.Second()), - Nanos: int32(schema.CreatedAt.Nanosecond()), - }, + Id: schema.ID.String(), + CreatedAt: Timestamp(&schema.CreatedAt), }, nil } // Read TODO issue#173 func (server *schemaServer) Read(ctx context.Context, req *ReadSchemaRequest) (*ReadSchemaResponse, error) { - data := query.ReadSchema{ID: domain.ID(req.Id)} - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err - } - schema, err := server.storage.ReadSchema(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to read the schema %q", req.Id) - } - return nil, status.Errorf(codes.Unknown, "trying to read the schema %q", req.Id) + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - resp := ReadSchemaResponse{ - Id: string(schema.ID), - Title: schema.Title, - CreatedAt: ×tamp.Timestamp{ - Seconds: int64(schema.CreatedAt.Second()), - Nanos: int32(schema.CreatedAt.Nanosecond()), - }, + schema, readErr := server.storage.ReadSchema(ctx, tokenID, query.ReadSchema{ID: domain.ID(req.Id)}) + if readErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", readErr) } buf := bytes.NewBuffer(make([]byte, 0, 1024)) if encodeErr := xml.NewEncoder(buf).Encode(schema.Definition); encodeErr != nil { return nil, status.Errorf(codes.Internal, - "trying to marshal definition `%#v` of the schema %q into XML", - schema.Definition, schema.ID) - } - resp.Definition = buf.String() - if schema.UpdatedAt != nil { - resp.UpdatedAt = ×tamp.Timestamp{ - Seconds: int64(schema.UpdatedAt.Second()), - Nanos: int32(schema.UpdatedAt.Nanosecond()), - } - } - if schema.DeletedAt != nil { - resp.DeletedAt = ×tamp.Timestamp{ - Seconds: int64(schema.DeletedAt.Second()), - Nanos: int32(schema.DeletedAt.Nanosecond()), - } - } - return &resp, nil + "trying to marshal definition `%#v` of the schema %q into XML: %+v", + schema.Definition, schema.ID, encodeErr) + } + return &ReadSchemaResponse{ + Id: schema.ID.String(), + Title: schema.Title, + Definition: buf.String(), + CreatedAt: Timestamp(&schema.CreatedAt), + UpdatedAt: Timestamp(schema.UpdatedAt), + DeletedAt: Timestamp(schema.DeletedAt), + }, nil } // Update TODO issue#173 func (server *schemaServer) Update(ctx context.Context, req *UpdateSchemaRequest) (*UpdateSchemaResponse, error) { - data := query.UpdateSchema{ID: domain.ID(req.Id), Title: req.Title} - if err := xml.NewDecoder(strings.NewReader(req.Definition)).Decode(&data.Definition); err != nil { - return nil, status.Errorf(codes.InvalidArgument, - "trying to unmarshal XML `%s` of the schema definition", - req.Definition) - } - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - schema, err := server.storage.UpdateSchema(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to update the schema %q", req.Id) + data := query.UpdateSchema{ID: domain.ID(req.Id), Title: req.Title} + if len(req.Definition) > 0 { + if decodeErr := xml.NewDecoder(strings.NewReader(req.Definition)).Decode(&data.Definition); decodeErr != nil { + return nil, status.Errorf(codes.InvalidArgument, + "trying to unmarshal XML `%s` of the schema definition: %+v", + req.Definition, decodeErr) } - return nil, status.Errorf(codes.Unknown, "trying to update the schema %q", req.Id) } - resp := UpdateSchemaResponse{} - if schema.UpdatedAt != nil { - resp.UpdatedAt = ×tamp.Timestamp{ - Seconds: int64(schema.UpdatedAt.Second()), - Nanos: int32(schema.UpdatedAt.Nanosecond()), - } + schema, updateErr := server.storage.UpdateSchema(ctx, tokenID, data) + if updateErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", updateErr) } - return &resp, nil + return &UpdateSchemaResponse{ + Id: schema.ID.String(), + UpdatedAt: Timestamp(schema.UpdatedAt), + }, nil } // Delete TODO issue#173 func (server *schemaServer) Delete(ctx context.Context, req *DeleteSchemaRequest) (*DeleteSchemaResponse, error) { - data := query.DeleteSchema{ID: domain.ID(req.Id)} - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - schema, err := server.storage.DeleteSchema(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to delete the schema %q", req.Id) - } - return nil, status.Errorf(codes.Unknown, "trying to delete the schema %q", req.Id) + schema, deleteErr := server.storage.DeleteSchema(ctx, tokenID, query.DeleteSchema{ID: domain.ID(req.Id)}) + if deleteErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", deleteErr) } - resp := DeleteSchemaResponse{} - if schema.DeletedAt != nil { - resp.DeletedAt = ×tamp.Timestamp{ - Seconds: int64(schema.DeletedAt.Second()), - Nanos: int32(schema.DeletedAt.Nanosecond()), - } - } - return &resp, nil + return &DeleteSchemaResponse{ + Id: schema.ID.String(), + DeletedAt: Timestamp(schema.DeletedAt), + }, nil } diff --git a/pkg/server/grpc/server_template.go b/pkg/server/grpc/server_template.go index 0cf92b4..8f2e234 100644 --- a/pkg/server/grpc/server_template.go +++ b/pkg/server/grpc/server_template.go @@ -3,9 +3,7 @@ package grpc import ( "context" - "github.com/golang/protobuf/ptypes/timestamp" "github.com/kamilsk/form-api/pkg/domain" - "github.com/kamilsk/form-api/pkg/errors" "github.com/kamilsk/form-api/pkg/server/grpc/middleware" "github.com/kamilsk/form-api/pkg/storage/query" "google.golang.org/grpc/codes" @@ -23,125 +21,76 @@ type templateServer struct { // Create TODO issue#173 func (server *templateServer) Create(ctx context.Context, req *CreateTemplateRequest) (*CreateTemplateResponse, error) { - data := query.CreateTemplate{Title: req.Title, Definition: domain.Template(req.Definition)} - if req.Id != "" { - id := domain.ID(req.Id) - data.ID = &id + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err - } - template, err := server.storage.CreateTemplate(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to create the template %q", req.Definition) - } - return nil, status.Errorf(codes.Unknown, "trying to create the template %q", req.Definition) + template, createErr := server.storage.CreateTemplate(ctx, tokenID, query.CreateTemplate{ + ID: ptrToID(req.Id), + Title: req.Title, + Definition: domain.Template(req.Definition), + }) + if createErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", createErr) } return &CreateTemplateResponse{ - Id: string(template.ID), - CreatedAt: ×tamp.Timestamp{ - Seconds: int64(template.CreatedAt.Second()), - Nanos: int32(template.CreatedAt.Nanosecond()), - }, + Id: template.ID.String(), + CreatedAt: Timestamp(&template.CreatedAt), }, nil } // Read TODO issue#173 func (server *templateServer) Read(ctx context.Context, req *ReadTemplateRequest) (*ReadTemplateResponse, error) { - data := query.ReadTemplate{ID: domain.ID(req.Id)} - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - template, err := server.storage.ReadTemplate(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to read the template %q", req.Id) - } - return nil, status.Errorf(codes.Unknown, "trying to read the template %q", req.Id) + template, readErr := server.storage.ReadTemplate(ctx, tokenID, query.ReadTemplate{ID: domain.ID(req.Id)}) + if readErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", readErr) } - resp := ReadTemplateResponse{ - Id: string(template.ID), + return &ReadTemplateResponse{ + Id: template.ID.String(), Title: template.Title, Definition: string(template.Definition), - CreatedAt: ×tamp.Timestamp{ - Seconds: int64(template.CreatedAt.Second()), - Nanos: int32(template.CreatedAt.Nanosecond()), - }, - } - if template.UpdatedAt != nil { - resp.UpdatedAt = ×tamp.Timestamp{ - Seconds: int64(template.UpdatedAt.Second()), - Nanos: int32(template.UpdatedAt.Nanosecond()), - } - } - if template.DeletedAt != nil { - resp.DeletedAt = ×tamp.Timestamp{ - Seconds: int64(template.DeletedAt.Second()), - Nanos: int32(template.DeletedAt.Nanosecond()), - } - } - return &resp, nil + CreatedAt: Timestamp(&template.CreatedAt), + UpdatedAt: Timestamp(template.UpdatedAt), + DeletedAt: Timestamp(template.DeletedAt), + }, nil } // Update TODO issue#173 func (server *templateServer) Update(ctx context.Context, req *UpdateTemplateRequest) (*UpdateTemplateResponse, error) { - data := query.UpdateTemplate{ID: domain.ID(req.Id), Title: req.Title, Definition: domain.Template(req.Definition)} - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - template, err := server.storage.UpdateTemplate(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to update the template %q", req.Id) - } - return nil, status.Errorf(codes.Unknown, "trying to update the template %q", req.Id) + template, updateErr := server.storage.UpdateTemplate(ctx, tokenID, query.UpdateTemplate{ + ID: domain.ID(req.Id), + Title: req.Title, + Definition: domain.Template(req.Definition), + }) + if updateErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", updateErr) } - resp := UpdateTemplateResponse{} - if template.UpdatedAt != nil { - resp.UpdatedAt = ×tamp.Timestamp{ - Seconds: int64(template.UpdatedAt.Second()), - Nanos: int32(template.UpdatedAt.Nanosecond()), - } - } - return &resp, nil + return &UpdateTemplateResponse{ + Id: template.ID.String(), + UpdatedAt: Timestamp(template.UpdatedAt), + }, nil } // Delete TODO issue#173 func (server *templateServer) Delete(ctx context.Context, req *DeleteTemplateRequest) (*DeleteTemplateResponse, error) { - data := query.DeleteTemplate{ID: domain.ID(req.Id)} - tokenID, err := middleware.TokenExtractor(ctx) - if err != nil { - return nil, err + tokenID, authErr := middleware.TokenExtractor(ctx) + if authErr != nil { + return nil, authErr } - template, err := server.storage.DeleteTemplate(ctx, tokenID, data) - if err != nil { - if appErr, is := err.(errors.ApplicationError); is { - if _, is = appErr.IsClientError(); is { - return nil, status.Error(codes.InvalidArgument, appErr.Message()) - } - return nil, status.Errorf(codes.Internal, "trying to delete the template %q", req.Id) - } - return nil, status.Errorf(codes.Unknown, "trying to delete the template %q", req.Id) + template, deleteErr := server.storage.DeleteTemplate(ctx, tokenID, query.DeleteTemplate{ID: domain.ID(req.Id)}) + if deleteErr != nil { + return nil, status.Errorf(codes.Internal, "error happened: %+v", deleteErr) } - resp := DeleteTemplateResponse{} - if template.DeletedAt != nil { - resp.DeletedAt = ×tamp.Timestamp{ - Seconds: int64(template.DeletedAt.Second()), - Nanos: int32(template.DeletedAt.Nanosecond()), - } - } - return &resp, nil + return &DeleteTemplateResponse{ + Id: template.ID.String(), + DeletedAt: Timestamp(template.DeletedAt), + }, nil }