forked from vieter-v/vieter
feat(server): added better query params to GitRepo API
parent
7627b28bcf
commit
1e079143cd
|
@ -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
|
||||
}
|
14
src/db/git.v
14
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 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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue