feat(server): added better query params to GitRepo API

Jef Roosens 2022-05-17 13:50:46 +02:00
parent 7627b28bcf
commit 1e079143cd
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
3 changed files with 41 additions and 3 deletions

25
src/db/filter.v 100644
View File

@ -0,0 +1,25 @@
module db
pub struct GitRepoFilter {
pub mut:
limit u64 = 25
offset u64
repo string
}
pub fn filter_from_params<T>(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
}

View File

@ -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 order by id
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 limit filter.limit offset filter.offset
}
return res

View File

@ -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<db.GitRepoFilter>(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))
}