Added proper constraint for creating repo

cron
Jef Roosens 2022-03-28 13:34:22 +02:00
parent a4a71a2797
commit 08821725f9
Signed by untrusted user: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 15 additions and 8 deletions

View File

@ -16,7 +16,7 @@ pub mut:
arch []string arch []string
} }
fn (mut r GitRepo) patch_from_params(params &map[string]string) ? { fn (mut r GitRepo) patch_from_params(params map[string]string) {
$for field in GitRepo.fields { $for field in GitRepo.fields {
if field.name in params { if field.name in params {
$if field.typ is string { $if field.typ is string {
@ -26,16 +26,22 @@ fn (mut r GitRepo) patch_from_params(params &map[string]string) ? {
} $else $if field.typ is []string { } $else $if field.typ is []string {
r.$(field.name) = params[field.name].split(',') r.$(field.name) = params[field.name].split(',')
} }
}else{ }
}
}
fn repo_from_params(params map[string]string) ?GitRepo {
mut repo := GitRepo{}
// If we're creating a new GitRepo, we want all fields to be present before
// "patching".
$for field in GitRepo.fields {
if field.name !in params {
return error('Missing parameter: ${field.name}.') return error('Missing parameter: ${field.name}.')
} }
} }
}
fn repo_from_params(params &map[string]string) ?GitRepo { repo.patch_from_params(params)
mut repo := GitRepo{}
repo.patch_from_params(params) ?
return repo return repo
} }
@ -115,8 +121,9 @@ fn (mut app App) post_repo() web.Result {
return app.text('Unauthorized.') return app.text('Unauthorized.')
} }
new_repo := repo_from_params(&app.query) or { new_repo := repo_from_params(app.query) or {
return app.server_error(400) app.set_status(400, err.msg)
return app.ok("")
} }
id := rand.uuid_v4() id := rand.uuid_v4()
@ -190,7 +197,7 @@ fn (mut app App) patch_repo(id string) web.Result {
return app.not_found() return app.not_found()
} }
repos[id].patch_from_params(&app.query) repos[id].patch_from_params(app.query)
lock app.git_mutex { lock app.git_mutex {
write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) } write_repos(app.conf.repos_file, &repos) or { return app.server_error(500) }