Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
refactor domain logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsk committed Oct 17, 2018
1 parent 3eb6088 commit 7c25381
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
6 changes: 3 additions & 3 deletions pkg/domain/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
//go:generate easyjson -all
type Schema struct {
ID string `json:"id,omitempty" yaml:"id,omitempty" xml:"id,attr,omitempty"`
Language string `json:"lang" yaml:"lang" xml:"lang,attr"`
Title string `json:"title" yaml:"title" xml:"title,attr"`
Action string `json:"action" yaml:"action" xml:"action,attr"`
Language string `json:"lang,omitempty" yaml:"lang,omitempty" xml:"lang,attr,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty" xml:"title,attr,omitempty"`
Action string `json:"action,omitempty" yaml:"action,omitempty" xml:"action,attr,omitempty"`
Method string `json:"method,omitempty" yaml:"method,omitempty" xml:"method,attr,omitempty"`
EncodingType string `json:"enctype,omitempty" yaml:"enctype,omitempty" xml:"enctype,attr,omitempty"`
Inputs []Input `json:"input" yaml:"input" xml:"input"`
Expand Down
9 changes: 1 addition & 8 deletions pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ func (service *Forma) HandleGetV1(ctx context.Context, req v1.GetRequest) (resp
if resp.Error != nil {
return
}

// add form namespace to make its elements unique
enrich(service.baseURL, &resp.Schema)

return
}

Expand All @@ -72,7 +69,6 @@ func (service *Forma) HandleInput(ctx context.Context, req v1.PostRequest) (resp
return
}

// add form namespace to make its elements unique
enrich(service.baseURL, &schema)
resp.URL = req.InputData.Redirect(req.Context.Referer(), schema.Action)

Expand Down Expand Up @@ -129,11 +125,8 @@ func (service *Forma) HandleInput(ctx context.Context, req v1.PostRequest) (resp
return resp
}

// configure form action
func enrich(base *url.URL, schema *domain.Schema) {
for i := range schema.Inputs {
schema.Inputs[i].ID = schema.ID + "_" + schema.Inputs[i].Name
}
// replace fallback by current API call
schema.Action = extend(*base, "api/v1", schema.ID)
schema.Method = http.MethodPost
schema.EncodingType = "application/x-www-form-urlencoded"
Expand Down
18 changes: 17 additions & 1 deletion pkg/storage/protected.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,23 @@ func (storage *Storage) ReadSchema(ctx context.Context, tokenID domain.ID, data
if authErr != nil {
return entity, authErr
}
return storage.exec.SchemaEditor(ctx, conn).Read(token, data)

entity, readErr := storage.exec.SchemaEditor(ctx, conn).Read(token, data)
if readErr != nil {
return entity, readErr
}

// TODO issue#logic duplicated
{
ptr := &entity.Definition
ptr.ID = entity.ID.String()
ptr.Title = entity.Title
for i, input := range ptr.Inputs {
ptr.Inputs[i].ID = ptr.ID + "_" + input.Name
}
}

return entity, nil
}

// UpdateSchema TODO issue#173
Expand Down
12 changes: 10 additions & 2 deletions pkg/storage/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ func (storage *Storage) Schema(ctx context.Context, id domain.ID) (domain.Schema
if readErr != nil {
return schema, readErr
}
entity.Definition.ID = entity.ID.String()
entity.Definition.Title = entity.Title

// TODO issue#logic duplicated
{
ptr := &entity.Definition
ptr.ID = entity.ID.String()
ptr.Title = entity.Title
for i, input := range ptr.Inputs {
ptr.Inputs[i].ID = ptr.ID + "_" + input.Name
}
}

return entity.Definition, nil
}
Expand Down

0 comments on commit 7c25381

Please sign in to comment.