WIP: currently broken database code
ci/woodpecker/pr/docs Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build Pipeline failed Details
ci/woodpecker/pr/docker unknown status Details
ci/woodpecker/pr/man unknown status Details
ci/woodpecker/pr/test Pipeline was successful Details

remotes/1725063365800743787/database-fixes
Jef Roosens 2022-11-04 11:16:30 +01:00
parent aff6dff06a
commit f3c1089e58
2 changed files with 19 additions and 19 deletions

View File

@ -3,45 +3,45 @@ module db
import models { Target, TargetArch, TargetFilter } import models { Target, TargetArch, TargetFilter }
// get_targets returns all targets in the database. // get_targets returns all targets in the database.
pub fn (db &VieterDb) get_targets(filter TargetFilter) []Target { pub fn (db &VieterDb) get_targets(filter TargetFilter) ![]Target {
// This seems to currently be blocked by a bug in the ORM, I'll have to ask // This seems to currently be blocked by a bug in the ORM, I'll have to ask
// around. // around.
if filter.repo != '' { if filter.repo != '' {
res := sql db.conn { res := sql db.conn {
select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset
} } or { return err }
return res return res
} }
res := sql db.conn { res := sql db.conn {
select from Target order by id limit filter.limit offset filter.offset select from Target order by id limit filter.limit offset filter.offset
} } or { return err }
return res return res
} }
// get_target tries to return a specific target. // get_target tries to return a specific target.
pub fn (db &VieterDb) get_target(target_id int) ?Target { pub fn (db &VieterDb) get_target(target_id int) !Target {
res := sql db.conn { res := sql db.conn {
select from Target where id == target_id select from Target where id == target_id
} } or { return err }
// If a select statement fails, it returns a zeroed object. By // If a select statement fails, it returns a zeroed object. By
// checking one of the required fields, we can see whether the query // checking one of the required fields, we can see whether the query
// returned a result or not. // returned a result or not.
if res.id == 0 { if res.id == 0 {
return none return error('none')
} }
return res return res
} }
// add_target inserts the given target into the database. // add_target inserts the given target into the database.
pub fn (db &VieterDb) add_target(repo Target) int { pub fn (db &VieterDb) add_target(repo Target) !int {
sql db.conn { sql db.conn {
insert repo into Target insert repo into Target
} } or { return err }
inserted_id := db.conn.last_id() as int inserted_id := db.conn.last_id() as int
@ -49,15 +49,15 @@ pub fn (db &VieterDb) add_target(repo Target) int {
} }
// delete_target deletes the target with the given id from the database. // delete_target deletes the target with the given id from the database.
pub fn (db &VieterDb) delete_target(target_id int) { pub fn (db &VieterDb) delete_target(target_id int) ! {
sql db.conn { sql db.conn {
delete from Target where id == target_id delete from Target where id == target_id
delete from TargetArch where target_id == target_id delete from TargetArch where target_id == target_id
} } or { return err }
} }
// update_target updates any non-array values for a given target. // update_target updates any non-array values for a given target.
pub fn (db &VieterDb) update_target(target_id int, params map[string]string) { pub fn (db &VieterDb) update_target(target_id int, params map[string]string) ! {
mut values := []string{} mut values := []string{}
// TODO does this allow for SQL injection? // TODO does this allow for SQL injection?
@ -77,7 +77,7 @@ pub fn (db &VieterDb) update_target(target_id int, params map[string]string) {
} }
// update_target_archs updates a given target's arch value. // update_target_archs updates a given target's arch value.
pub fn (db &VieterDb) update_target_archs(target_id int, archs []TargetArch) { pub fn (db &VieterDb) update_target_archs(target_id int, archs []TargetArch) ! {
archs_with_id := archs.map(TargetArch{ archs_with_id := archs.map(TargetArch{
...it ...it
target_id: target_id target_id: target_id
@ -85,12 +85,12 @@ pub fn (db &VieterDb) update_target_archs(target_id int, archs []TargetArch) {
sql db.conn { sql db.conn {
delete from TargetArch where target_id == target_id delete from TargetArch where target_id == target_id
} } or { return err }
for arch in archs_with_id { for arch in archs_with_id {
sql db.conn { sql db.conn {
insert arch into TargetArch insert arch into TargetArch
} } or { return err }
} }
} }

View File

@ -12,7 +12,7 @@ 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.'))
} }
repos := app.db.get_targets(filter) repos := app.db.get_targets(filter) or { return app.status(.internal_server_error) }
return app.json(.ok, new_data_response(repos)) return app.json(.ok, new_data_response(repos))
} }
@ -45,7 +45,7 @@ fn (mut app App) v1_post_target() web.Result {
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_repo) id := app.db.add_target(new_repo) or { return app.status(.internal_server_error) }
return app.json(http.Status.ok, new_data_response(id)) return app.json(http.Status.ok, new_data_response(id))
} }
@ -53,7 +53,7 @@ fn (mut app App) v1_post_target() web.Result {
// v1_delete_target removes a given target from the server's list. // v1_delete_target removes a given target from the server's list.
['/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) or { return app.status(.internal_server_error) }
return app.status(.ok) return app.status(.ok)
} }
@ -61,12 +61,12 @@ fn (mut app App) v1_delete_target(id int) web.Result {
// v1_patch_target updates a target's data with the given query params. // v1_patch_target updates a target's data with the given query params.
['/api/v1/targets/:id'; auth; patch] ['/api/v1/targets/:id'; auth; patch]
fn (mut app App) v1_patch_target(id int) web.Result { fn (mut app App) v1_patch_target(id int) web.Result {
app.db.update_target(id, app.query) app.db.update_target(id, app.query) or { return app.status(.internal_server_error) }
if 'arch' in app.query { if 'arch' in app.query {
arch_objs := app.query['arch'].split(',').map(TargetArch{ value: it }) arch_objs := app.query['arch'].split(',').map(TargetArch{ value: it })
app.db.update_target_archs(id, arch_objs) app.db.update_target_archs(id, arch_objs) or { return app.status(.internal_server_error) }
} }
return app.status(.ok) return app.status(.ok)