@@ -155,8 +155,8 @@ func GetConfigKind(ctx context.Context, resourceType string) ([]string, error) {
155155 return results , nil
156156}
157157
158- // GetConfigSource returns a list of resource configurations from the database.
159- func GetConfigSource (ctx context.Context , kind , id , config string ) ([]model.ConfigSource , error ) {
158+ // GetSourceConfigs returns a list of resource configurations from the database.
159+ func GetSourceConfigs (ctx context.Context , kind , id , config string , limit , page int ) ([]model.ConfigSource , int , error ) {
160160 table := configSourceTableName
161161
162162 // SELECT id, kind, created_at, updated_at, config FROM " + table
@@ -183,17 +183,39 @@ func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.Conf
183183 )
184184 }
185185
186+ // Get total count of results
187+ totalCount := 0
188+ totalQuery := psql .Select (sm .From (query ), sm .Columns ("count(*)" ))
189+ totalQueryString , totalArgs , err := totalQuery .Build (ctx )
190+ if err != nil {
191+ logrus .Errorf ("building total count query failed: %s\n \t %s" , totalQueryString , err )
192+ return nil , 0 , err
193+ }
194+
195+ if err = DB .QueryRow (ctx , totalQueryString , totalArgs ... ).Scan (
196+ & totalCount ,
197+ ); err != nil {
198+ logrus .Errorf ("parsing total count result: %s" , err )
199+ }
200+
201+ if limit < totalCount && limit > 0 {
202+ query .Apply (
203+ sm .Limit (limit ),
204+ sm .Offset ((page - 1 )* limit ),
205+ )
206+ }
207+
186208 queryString , args , err := query .Build (ctx )
187209 if err != nil {
188210 logrus .Errorf ("building query failed: %s\n \t %s" , queryString , err )
189- return nil , err
211+ return nil , 0 , err
190212 }
191213
192214 rows , err := DB .Query (context .Background (), queryString , args ... )
193215
194216 if err != nil {
195217 logrus .Errorf ("query failed: %q\n \t %s" , queryString , err )
196- return nil , err
218+ return nil , 0 , err
197219 }
198220
199221 results := []model.ConfigSource {}
@@ -206,7 +228,7 @@ func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.Conf
206228 err := rows .Scan (& r .ID , & r .Kind , & r .Created_at , & r .Updated_at , & config )
207229 if err != nil {
208230 logrus .Errorf ("parsing Source result: %s" , err )
209- return nil , err
231+ return nil , 0 , err
210232 }
211233
212234 err = json .Unmarshal ([]byte (config ), & r .Config )
@@ -218,11 +240,11 @@ func GetConfigSource(ctx context.Context, kind, id, config string) ([]model.Conf
218240 results = append (results , r )
219241 }
220242
221- return results , nil
243+ return results , totalCount , nil
222244}
223245
224- // GetConfigCondition returns a list of resource configurations from the database.
225- func GetConfigCondition (ctx context.Context , kind , id , config string ) ([]model.ConfigCondition , error ) {
246+ // GetConditionConfigs returns a list of resource configurations from the database.
247+ func GetConditionConfigs (ctx context.Context , kind , id , config string , limit , page int ) ([]model.ConfigCondition , int , error ) {
226248 table := configConditionTableName
227249
228250 // SELECT id, kind, created_at, updated_at, config FROM " + table
@@ -249,17 +271,39 @@ func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.C
249271 )
250272 }
251273
274+ totalCount := 0
275+ totalQuery := psql .Select (sm .From (query ), sm .Columns ("count(*)" ))
276+ totalQueryString , totalArgs , err := totalQuery .Build (ctx )
277+ if err != nil {
278+ logrus .Errorf ("building total count query failed: %s\n \t %s" , totalQueryString , err )
279+ return nil , 0 , err
280+ }
281+
282+ if err = DB .QueryRow (ctx , totalQueryString , totalArgs ... ).Scan (
283+ & totalCount ,
284+ ); err != nil {
285+ logrus .Errorf ("parsing total count result: %s" , err )
286+ }
287+
288+ // Apply pagination if limit and page are set
289+ if limit < totalCount && limit > 0 {
290+ query .Apply (
291+ sm .Limit (limit ),
292+ sm .Offset ((page - 1 )* limit ),
293+ )
294+ }
295+
252296 queryString , args , err := query .Build (ctx )
253297 if err != nil {
254298 logrus .Errorf ("building query failed: %s\n \t %s" , queryString , err )
255- return nil , err
299+ return nil , 0 , err
256300 }
257301
258302 rows , err := DB .Query (context .Background (), queryString , args ... )
259303
260304 if err != nil {
261305 logrus .Errorf ("query failed: %q\n \t %s" , queryString , err )
262- return nil , err
306+ return nil , 0 , err
263307 }
264308
265309 results := []model.ConfigCondition {}
@@ -275,7 +319,7 @@ func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.C
275319
276320 logrus .Errorf ("Query: %q\n \t %s" , queryString , err )
277321 logrus .Errorf ("parsing condition result: %s" , err )
278- return nil , err
322+ return nil , 0 , err
279323 }
280324
281325 err = json .Unmarshal ([]byte (config ), & r .Config )
@@ -287,11 +331,11 @@ func GetConfigCondition(ctx context.Context, kind, id, config string) ([]model.C
287331 results = append (results , r )
288332 }
289333
290- return results , nil
334+ return results , totalCount , nil
291335}
292336
293- // GetConfigTarget returns a list of resource configurations from the database.
294- func GetConfigTarget (ctx context.Context , kind , id , config string ) ([]model.ConfigTarget , error ) {
337+ // GetTargetConfigs returns a list of resource configurations from the database.
338+ func GetTargetConfigs (ctx context.Context , kind , id , config string , limit , page int ) ([]model.ConfigTarget , int , error ) {
295339 table := configTargetTableName
296340
297341 // SELECT id, kind, created_at, updated_at, config FROM " + table
@@ -318,17 +362,39 @@ func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.Conf
318362 )
319363 }
320364
365+ totalCount := 0
366+ totalQuery := psql .Select (sm .From (query ), sm .Columns ("count(*)" ))
367+ totalQueryString , totalArgs , err := totalQuery .Build (ctx )
368+ if err != nil {
369+ logrus .Errorf ("building total count query failed: %s\n \t %s" , totalQueryString , err )
370+ return nil , 0 , err
371+ }
372+
373+ if err = DB .QueryRow (ctx , totalQueryString , totalArgs ... ).Scan (
374+ & totalCount ,
375+ ); err != nil {
376+ logrus .Errorf ("parsing total count result: %s" , err )
377+ }
378+
379+ // Apply pagination if limit and page are set
380+ if limit < totalCount && limit > 0 {
381+ query .Apply (
382+ sm .Limit (limit ),
383+ sm .Offset ((page - 1 )* limit ),
384+ )
385+ }
386+
321387 queryString , args , err := query .Build (ctx )
322388 if err != nil {
323389 logrus .Errorf ("building query failed: %s\n \t %s" , queryString , err )
324- return nil , err
390+ return nil , 0 , err
325391 }
326392
327393 rows , err := DB .Query (context .Background (), queryString , args ... )
328394
329395 if err != nil {
330396 logrus .Errorf ("query failed: %q\n \t %s" , queryString , err )
331- return nil , err
397+ return nil , 0 , err
332398 }
333399
334400 results := []model.ConfigTarget {}
@@ -342,7 +408,7 @@ func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.Conf
342408 if err != nil {
343409 logrus .Errorf ("Query: %q\n \t %s" , queryString , err )
344410 logrus .Errorf ("parsing target result: %s" , err )
345- return nil , err
411+ return nil , 0 , err
346412 }
347413
348414 err = json .Unmarshal ([]byte (config ), & r .Config )
@@ -354,5 +420,5 @@ func GetConfigTarget(ctx context.Context, kind, id, config string) ([]model.Conf
354420 results = append (results , r )
355421 }
356422
357- return results , nil
423+ return results , totalCount , nil
358424}
0 commit comments