forked from vieter-v/vieter
refactor: renamed api routes & client code to 'targets'
This commit is contained in:
parent
6b79f7b5ed
commit
4d581da7bf
8 changed files with 101 additions and 95 deletions
88
src/server/api_targets.v
Normal file
88
src/server/api_targets.v
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
module server
|
||||
|
||||
import web
|
||||
import net.http
|
||||
import response { new_data_response, new_response }
|
||||
import db
|
||||
import models { GitRepo, GitRepoArch, GitRepoFilter }
|
||||
|
||||
// v1_get_targets returns the current list of repos.
|
||||
['/api/v1/targets'; get]
|
||||
fn (mut app App) v1_get_targets() web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
filter := models.from_params<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))
|
||||
}
|
||||
|
||||
// v1_get_single_target returns the information for a single repo.
|
||||
['/api/v1/targets/:id'; get]
|
||||
fn (mut app App) v1_get_single_target(id int) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
repo := app.db.get_git_repo(id) or { return app.not_found() }
|
||||
|
||||
return app.json(http.Status.ok, new_data_response(repo))
|
||||
}
|
||||
|
||||
// v1_post_target creates a new repo from the provided query string.
|
||||
['/api/v1/targets'; post]
|
||||
fn (mut app App) v1_post_target() web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
mut params := app.query.clone()
|
||||
|
||||
// If a repo is created without specifying the arch, we assume it's meant
|
||||
// for the default architecture.
|
||||
if 'arch' !in params {
|
||||
params['arch'] = app.conf.default_arch
|
||||
}
|
||||
|
||||
new_repo := models.from_params<GitRepo>(params) or {
|
||||
return app.json(http.Status.bad_request, new_response(err.msg()))
|
||||
}
|
||||
|
||||
app.db.add_git_repo(new_repo)
|
||||
|
||||
return app.json(http.Status.ok, new_response('Repo added successfully.'))
|
||||
}
|
||||
|
||||
// v1_delete_target removes a given repo from the server's list.
|
||||
['/api/v1/targets/:id'; delete]
|
||||
fn (mut app App) v1_delete_target(id int) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
app.db.delete_git_repo(id)
|
||||
|
||||
return app.json(http.Status.ok, new_response('Repo removed successfully.'))
|
||||
}
|
||||
|
||||
// v1_patch_target updates a repo's data with the given query params.
|
||||
['/api/v1/targets/:id'; patch]
|
||||
fn (mut app App) v1_patch_target(id int) web.Result {
|
||||
if !app.is_authorized() {
|
||||
return app.json(http.Status.unauthorized, new_response('Unauthorized.'))
|
||||
}
|
||||
|
||||
app.db.update_git_repo(id, app.query)
|
||||
|
||||
if 'arch' in app.query {
|
||||
arch_objs := app.query['arch'].split(',').map(GitRepoArch{ value: it })
|
||||
|
||||
app.db.update_git_repo_archs(id, arch_objs)
|
||||
}
|
||||
|
||||
return app.json(http.Status.ok, new_response('Repo updated successfully.'))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue