From f3c1089e58c7bf6a027a169263053f0689f7af2a Mon Sep 17 00:00:00 2001 From: Jef Roosens Date: Fri, 4 Nov 2022 11:16:30 +0100 Subject: [PATCH] WIP: currently broken database code --- src/db/targets.v | 28 ++++++++++++++-------------- src/server/api_targets.v | 10 +++++----- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/db/targets.v b/src/db/targets.v index a705ebb..2248493 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -3,45 +3,45 @@ module db import models { Target, TargetArch, TargetFilter } // 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 // around. if filter.repo != '' { res := sql db.conn { select from Target where repo == filter.repo order by id limit filter.limit offset filter.offset - } + } or { return err } return res } res := sql db.conn { select from Target order by id limit filter.limit offset filter.offset - } + } or { return err } return res } // 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 { select from Target where id == target_id - } + } or { return err } // If a select statement fails, it returns a zeroed object. By // checking one of the required fields, we can see whether the query // returned a result or not. if res.id == 0 { - return none + return error('none') } return res } // 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 { insert repo into Target - } + } or { return err } 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. -pub fn (db &VieterDb) delete_target(target_id int) { +pub fn (db &VieterDb) delete_target(target_id int) ! { sql db.conn { delete from Target where 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. -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{} // 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. -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{ ...it target_id: target_id @@ -85,12 +85,12 @@ pub fn (db &VieterDb) update_target_archs(target_id int, archs []TargetArch) { sql db.conn { delete from TargetArch where target_id == target_id - } + } or { return err } for arch in archs_with_id { sql db.conn { insert arch into TargetArch - } + } or { return err } } } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 6f284af..170c002 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -12,7 +12,7 @@ fn (mut app App) v1_get_targets() web.Result { filter := models.from_params(app.query) or { 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)) } @@ -45,7 +45,7 @@ fn (mut app App) v1_post_target() web.Result { 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)) } @@ -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. ['/api/v1/targets/:id'; auth; delete] 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) } @@ -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. ['/api/v1/targets/:id'; auth; patch] 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 { 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)