8
8
9
9
// Packages
10
10
pg "github.com/djthorpe/go-pg"
11
- httpresponse "github.com/mutablelogic/go-service/pkg/httpresponse"
11
+ httpresponse "github.com/mutablelogic/go-server/pkg/httpresponse"
12
+ "github.com/mutablelogic/go-server/pkg/types"
12
13
)
13
14
14
15
////////////////////////////////////////////////////////////////////////////////
@@ -34,6 +35,15 @@ type Name struct {
34
35
Subject * string `json:"subject,omitempty"`
35
36
}
36
37
38
+ type NameList struct {
39
+ Count uint64 `json:"count"`
40
+ Body []Name `json:"body,omitempty"`
41
+ }
42
+
43
+ type NameListRequest struct {
44
+ pg.OffsetLimit
45
+ }
46
+
37
47
////////////////////////////////////////////////////////////////////////////////
38
48
// STRINGIFY
39
49
@@ -53,6 +63,14 @@ func (n Name) String() string {
53
63
return string (data )
54
64
}
55
65
66
+ func (n NameList ) String () string {
67
+ data , err := json .MarshalIndent (n , "" , " " )
68
+ if err != nil {
69
+ return err .Error ()
70
+ }
71
+ return string (data )
72
+ }
73
+
56
74
////////////////////////////////////////////////////////////////////////////////
57
75
// SELECT
58
76
@@ -65,11 +83,33 @@ func (n NameId) Select(bind *pg.Bind, op pg.Op) (string, error) {
65
83
66
84
// Return query
67
85
switch op {
86
+ case pg .Get :
87
+ return nameGet , nil
88
+ case pg .Update :
89
+ return namePatch , nil
90
+ case pg .Delete :
91
+ return nameDelete , nil
68
92
default :
69
93
return "" , httpresponse .ErrInternalError .Withf ("unsupported NameId operation %q" , op )
70
94
}
71
95
}
72
96
97
+ func (n NameListRequest ) Select (bind * pg.Bind , op pg.Op ) (string , error ) {
98
+ // Set empty where
99
+ bind .Set ("where" , "" )
100
+
101
+ // Bind offset and limit
102
+ n .OffsetLimit .Bind (bind , NameListLimit )
103
+
104
+ // Return query
105
+ switch op {
106
+ case pg .List :
107
+ return nameList , nil
108
+ default :
109
+ return "" , httpresponse .ErrInternalError .Withf ("unsupported NameListRequest operation %q" , op )
110
+ }
111
+ }
112
+
73
113
////////////////////////////////////////////////////////////////////////////////
74
114
// WRITER
75
115
@@ -79,13 +119,48 @@ func (n NameMeta) Insert(bind *pg.Bind) (string, error) {
79
119
} else {
80
120
bind .Set ("commonName" , commonName )
81
121
}
122
+ // TODO
82
123
83
124
// Return insert or replace
84
125
return nameReplace , nil
85
126
}
86
127
87
128
func (n NameMeta ) Update (bind * pg.Bind ) error {
88
- return httpresponse .ErrNotImplemented .With ("NameMeta.Update" )
129
+ bind .Del ("patch" )
130
+ if name := strings .TrimSpace (n .CommonName ); name != "" {
131
+ bind .Append ("patch" , `"commonName" = ` + bind .Set ("commonName" , name ))
132
+ }
133
+ if n .Org != nil {
134
+ bind .Append ("patch" , `"organizationName" = ` + bind .Set ("organizationName" , types .TrimStringPtr (* n .Org )))
135
+ }
136
+ if n .Unit != nil {
137
+ bind .Append ("patch" , `"organizationalUnit" = ` + bind .Set ("organizationalUnit" , types .TrimStringPtr (* n .Unit )))
138
+ }
139
+ if n .Country != nil {
140
+ bind .Append ("patch" , `"countryName" = ` + bind .Set ("countryName" , types .TrimStringPtr (* n .Country )))
141
+ }
142
+ if n .City != nil {
143
+ bind .Append ("patch" , `"localityName" = ` + bind .Set ("localityName" , types .TrimStringPtr (* n .City )))
144
+ }
145
+ if n .State != nil {
146
+ bind .Append ("patch" , `"stateOrProvinceName" = ` + bind .Set ("stateOrProvinceName" , types .TrimStringPtr (* n .State )))
147
+ }
148
+ if n .StreetAddress != nil {
149
+ bind .Append ("patch" , `"streetAddress" = ` + bind .Set ("streetAddress" , types .TrimStringPtr (* n .StreetAddress )))
150
+ }
151
+ if n .PostalCode != nil {
152
+ bind .Append ("patch" , `"postalCode" = ` + bind .Set ("postalCode" , types .TrimStringPtr (* n .PostalCode )))
153
+ }
154
+
155
+ // Join the patch fields
156
+ if patch := bind .Join ("patch" , ", " ); patch == "" {
157
+ return httpresponse .ErrBadRequest .With ("nothing to update" )
158
+ } else {
159
+ bind .Set ("patch" , patch )
160
+ }
161
+
162
+ // Return success
163
+ return nil
89
164
}
90
165
91
166
////////////////////////////////////////////////////////////////////////////////
@@ -95,6 +170,20 @@ func (n *Name) Scan(row pg.Row) error {
95
170
return row .Scan (& n .Id , & n .CommonName , & n .Org , & n .Unit , & n .Country , & n .City , & n .State , & n .StreetAddress , & n .PostalCode , & n .Ts )
96
171
}
97
172
173
+ func (n * NameList ) Scan (row pg.Row ) error {
174
+ var name Name
175
+ if err := name .Scan (row ); err != nil {
176
+ return err
177
+ } else {
178
+ n .Body = append (n .Body , name )
179
+ }
180
+ return nil
181
+ }
182
+
183
+ func (n * NameList ) ScanCount (row pg.Row ) error {
184
+ return row .Scan (& n .Count )
185
+ }
186
+
98
187
////////////////////////////////////////////////////////////////////////////////
99
188
// SQL
100
189
@@ -165,5 +254,5 @@ const (
165
254
${"schema"}."name"
166
255
`
167
256
nameGet = nameSelect + ` WHERE "id" = @id`
168
- nameList = `WITH q AS (` + nameSelect + `) SELECT * FROM q ${where} ${offsetlimit} `
257
+ nameList = `WITH q AS (` + nameSelect + `) SELECT * FROM q ${where}`
169
258
)
0 commit comments