From 1e079143cd29673ff2a20e8513dfc741493f0e9b Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Tue, 17 May 2022 13:50:46 +0200 Subject: [PATCH] feat(server): added better query params to GitRepo API --- src/db/filter.v | 25 +++++++++++++++++++++++++ src/db/git.v | 14 ++++++++++++-- src/server/git.v | 5 ++++- 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 src/db/filter.v diff --git a/src/db/filter.v b/src/db/filter.v new file mode 100644 index 00000000..79e281b3 --- /dev/null +++ b/src/db/filter.v @@ -0,0 +1,25 @@ +module db + +pub struct GitRepoFilter { +pub mut: + limit u64 = 25 + offset u64 + repo string +} + +pub fn filter_from_params(params map[string]string) ?T { + mut o := GitRepoFilter{} + + $for field in T.fields { + if field.name in params { + val := params[field.name] + + $if field.typ is string { + o.$(field.name) = val + } $else $if field.typ is u64 { + o.$(field.name) = val.u64() + } + } + } + return o +} diff --git a/src/db/git.v b/src/db/git.v index 9a475a54..cb31bf04 100644 --- a/src/db/git.v +++ b/src/db/git.v @@ -77,9 +77,19 @@ pub fn git_repo_from_params(params map[string]string) ?GitRepo { } // get_git_repos returns all GitRepo's in the database. -pub fn (db &VieterDb) get_git_repos() []GitRepo { +pub fn (db &VieterDb) get_git_repos(filter GitRepoFilter) []GitRepo { + // This seems to currently be blocked by a bug in the ORM, I'll have to ask + // around. + if filter.repo != '' { + res := sql db.conn { + select from GitRepo where repo == filter.repo order by id limit filter.limit offset filter.offset + } + + return res + } + res := sql db.conn { - select from GitRepo order by id + select from GitRepo order by id limit filter.limit offset filter.offset } return res diff --git a/src/server/git.v b/src/server/git.v index c5cbc0a6..921251f5 100644 --- a/src/server/git.v +++ b/src/server/git.v @@ -12,7 +12,10 @@ fn (mut app App) get_repos() web.Result { return app.json(http.Status.unauthorized, new_response('Unauthorized.')) } - repos := app.db.get_git_repos() + filter := db.filter_from_params(app.query) or { + return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) + } + repos := app.db.get_git_repos(filter) return app.json(http.Status.ok, new_data_response(repos)) }