Compare commits

..

No commits in common. "63427899217aae4390e01b72238758ac1457856d" and "b6168a3060752474bb1ba4bd961ac119eddce16f" have entirely different histories.

4 changed files with 14 additions and 35 deletions

View File

@ -49,13 +49,6 @@ pub fn new_job_queue(default_schedule CronExpression, default_base_image string)
} }
} }
// insert_all executes insert for each architecture of the given Target.
pub fn (mut q BuildJobQueue) insert_all(target Target) ! {
for arch in target.arch {
q.insert(target, arch.value)!
}
}
// insert a new target's job into the queue for the given architecture. This // insert a new target's job into the queue for the given architecture. This
// job will then be endlessly rescheduled after being pop'ed, unless removed // job will then be endlessly rescheduled after being pop'ed, unless removed
// explicitely. // explicitely.

View File

@ -84,8 +84,6 @@ pub fn (db &VieterDb) add_build_log(log BuildLog) int {
insert log into BuildLog insert log into BuildLog
} }
// Here, this does work because a log doesn't contain any foreign keys,
// meaning the ORM only has to do a single add
inserted_id := db.conn.last_id() as int inserted_id := db.conn.last_id() as int
return inserted_id return inserted_id

View File

@ -38,17 +38,14 @@ pub fn (db &VieterDb) get_target(target_id int) ?Target {
} }
// add_target inserts the given target into the database. // add_target inserts the given target into the database.
pub fn (db &VieterDb) add_target(target Target) int { pub fn (db &VieterDb) add_target(repo Target) int {
sql db.conn { sql db.conn {
insert target into Target insert repo into Target
} }
// ID of inserted target is the largest id inserted_id := db.conn.last_id() as int
inserted_target := sql db.conn {
select from Target order by id desc limit 1
}
return inserted_target.id return inserted_id
} }
// delete_target deletes the target with the given id from the database. // delete_target deletes the target with the given id from the database.

View File

@ -12,17 +12,17 @@ fn (mut app App) v1_get_targets() web.Result {
filter := models.from_params<TargetFilter>(app.query) or { filter := models.from_params<TargetFilter>(app.query) or {
return app.json(http.Status.bad_request, new_response('Invalid query parameters.')) return app.json(http.Status.bad_request, new_response('Invalid query parameters.'))
} }
targets := app.db.get_targets(filter) repos := app.db.get_targets(filter)
return app.json(.ok, new_data_response(targets)) return app.json(.ok, new_data_response(repos))
} }
// v1_get_single_target returns the information for a single target. // v1_get_single_target returns the information for a single target.
['/api/v1/targets/:id'; auth; get] ['/api/v1/targets/:id'; auth; get]
fn (mut app App) v1_get_single_target(id int) web.Result { fn (mut app App) v1_get_single_target(id int) web.Result {
target := app.db.get_target(id) or { return app.not_found() } repo := app.db.get_target(id) or { return app.not_found() }
return app.json(.ok, new_data_response(target)) return app.json(.ok, new_data_response(repo))
} }
// v1_post_target creates a new target from the provided query string. // v1_post_target creates a new target from the provided query string.
@ -30,27 +30,22 @@ fn (mut app App) v1_get_single_target(id int) web.Result {
fn (mut app App) v1_post_target() web.Result { fn (mut app App) v1_post_target() web.Result {
mut params := app.query.clone() mut params := app.query.clone()
// If a target is created without specifying the arch, we assume it's meant // If a repo is created without specifying the arch, we assume it's meant
// for the default architecture. // for the default architecture.
if 'arch' !in params || params['arch'] == '' { if 'arch' !in params || params['arch'] == '' {
params['arch'] = app.conf.default_arch params['arch'] = app.conf.default_arch
} }
mut new_target := models.from_params<Target>(params) or { new_repo := models.from_params<Target>(params) or {
return app.json(http.Status.bad_request, new_response(err.msg())) return app.json(http.Status.bad_request, new_response(err.msg()))
} }
// Ensure someone doesn't submit an invalid kind // Ensure someone doesn't submit an invalid kind
if new_target.kind !in models.valid_kinds { if new_repo.kind !in models.valid_kinds {
return app.json(http.Status.bad_request, new_response('Invalid kind.')) return app.json(http.Status.bad_request, new_response('Invalid kind.'))
} }
id := app.db.add_target(new_target) id := app.db.add_target(new_repo)
new_target.id = id
// Add the target to the job queue
// TODO return better error here if it's the cron schedule that's incorrect
app.job_queue.insert_all(new_target) or { return app.status(.internal_server_error) }
return app.json(.ok, new_data_response(id)) return app.json(.ok, new_data_response(id))
} }
@ -59,7 +54,6 @@ fn (mut app App) v1_post_target() web.Result {
['/api/v1/targets/:id'; auth; delete] ['/api/v1/targets/:id'; auth; delete]
fn (mut app App) v1_delete_target(id int) web.Result { fn (mut app App) v1_delete_target(id int) web.Result {
app.db.delete_target(id) app.db.delete_target(id)
app.job_queue.invalidate(id)
return app.json(.ok, new_response('')) return app.json(.ok, new_response(''))
} }
@ -75,10 +69,7 @@ fn (mut app App) v1_patch_target(id int) web.Result {
app.db.update_target_archs(id, arch_objs) app.db.update_target_archs(id, arch_objs)
} }
target := app.db.get_target(id) or { return app.status(.internal_server_error) } repo := app.db.get_target(id) or { return app.status(.internal_server_error) }
app.job_queue.invalidate(id) return app.json(.ok, new_data_response(repo))
app.job_queue.insert_all(target) or { return app.status(.internal_server_error) }
return app.json(.ok, new_data_response(target))
} }