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

Commit 7c25381

Browse files
committed
refactor domain logic
1 parent 3eb6088 commit 7c25381

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

pkg/domain/schema.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import (
99
//go:generate easyjson -all
1010
type Schema struct {
1111
ID string `json:"id,omitempty" yaml:"id,omitempty" xml:"id,attr,omitempty"`
12-
Language string `json:"lang" yaml:"lang" xml:"lang,attr"`
13-
Title string `json:"title" yaml:"title" xml:"title,attr"`
14-
Action string `json:"action" yaml:"action" xml:"action,attr"`
12+
Language string `json:"lang,omitempty" yaml:"lang,omitempty" xml:"lang,attr,omitempty"`
13+
Title string `json:"title,omitempty" yaml:"title,omitempty" xml:"title,attr,omitempty"`
14+
Action string `json:"action,omitempty" yaml:"action,omitempty" xml:"action,attr,omitempty"`
1515
Method string `json:"method,omitempty" yaml:"method,omitempty" xml:"method,attr,omitempty"`
1616
EncodingType string `json:"enctype,omitempty" yaml:"enctype,omitempty" xml:"enctype,attr,omitempty"`
1717
Inputs []Input `json:"input" yaml:"input" xml:"input"`

pkg/service/service.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,7 @@ func (service *Forma) HandleGetV1(ctx context.Context, req v1.GetRequest) (resp
5757
if resp.Error != nil {
5858
return
5959
}
60-
61-
// add form namespace to make its elements unique
6260
enrich(service.baseURL, &resp.Schema)
63-
6461
return
6562
}
6663

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

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

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

128+
// configure form action
132129
func enrich(base *url.URL, schema *domain.Schema) {
133-
for i := range schema.Inputs {
134-
schema.Inputs[i].ID = schema.ID + "_" + schema.Inputs[i].Name
135-
}
136-
// replace fallback by current API call
137130
schema.Action = extend(*base, "api/v1", schema.ID)
138131
schema.Method = http.MethodPost
139132
schema.EncodingType = "application/x-www-form-urlencoded"

pkg/storage/protected.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,23 @@ func (storage *Storage) ReadSchema(ctx context.Context, tokenID domain.ID, data
4545
if authErr != nil {
4646
return entity, authErr
4747
}
48-
return storage.exec.SchemaEditor(ctx, conn).Read(token, data)
48+
49+
entity, readErr := storage.exec.SchemaEditor(ctx, conn).Read(token, data)
50+
if readErr != nil {
51+
return entity, readErr
52+
}
53+
54+
// TODO issue#logic duplicated
55+
{
56+
ptr := &entity.Definition
57+
ptr.ID = entity.ID.String()
58+
ptr.Title = entity.Title
59+
for i, input := range ptr.Inputs {
60+
ptr.Inputs[i].ID = ptr.ID + "_" + input.Name
61+
}
62+
}
63+
64+
return entity, nil
4965
}
5066

5167
// UpdateSchema TODO issue#173

pkg/storage/public.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ func (storage *Storage) Schema(ctx context.Context, id domain.ID) (domain.Schema
2222
if readErr != nil {
2323
return schema, readErr
2424
}
25-
entity.Definition.ID = entity.ID.String()
26-
entity.Definition.Title = entity.Title
25+
26+
// TODO issue#logic duplicated
27+
{
28+
ptr := &entity.Definition
29+
ptr.ID = entity.ID.String()
30+
ptr.Title = entity.Title
31+
for i, input := range ptr.Inputs {
32+
ptr.Inputs[i].ID = ptr.ID + "_" + input.Name
33+
}
34+
}
2735

2836
return entity.Definition, nil
2937
}

0 commit comments

Comments
 (0)