From 210508f1ee251b3cb61ab5bf146805f10b600c1c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Sun, 11 Sep 2022 20:34:02 +0200 Subject: [PATCH] feat: logs api now also returns id --- CHANGELOG.md | 1 + src/client/logs.v | 4 ++-- src/console/targets/targets.v | 2 +- src/db/logs.v | 6 +++++- src/server/api_logs.v | 9 +++++---- src/server/api_targets.v | 10 +++++----- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aa0e43..00a3539 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 repository will be cloned with the default branch * Build containers now explicitely set the PATH variable * Refactor of web framework +* API endpoints now return id of newly created entries ### Removed diff --git a/src/client/logs.v b/src/client/logs.v index b52c3d0..b414245 100644 --- a/src/client/logs.v +++ b/src/client/logs.v @@ -39,7 +39,7 @@ pub fn (c &Client) get_build_log_content(id int) ?string { } // add_build_log adds a new build log to the server. -pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response { +pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time time.Time, arch string, exit_code int, content string) ?Response { params := { 'target': target_id.str() 'startTime': start_time.unix_time().str() @@ -48,7 +48,7 @@ pub fn (c &Client) add_build_log(target_id int, start_time time.Time, end_time t 'exitCode': exit_code.str() } - data := c.send_request_with_body(Method.post, '/api/v1/logs', params, content)? + data := c.send_request_with_body(Method.post, '/api/v1/logs', params, content)? return data } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index a555936..2784dbc 100644 --- a/src/console/targets/targets.v +++ b/src/console/targets/targets.v @@ -206,7 +206,7 @@ fn add(conf Config, t &NewTarget) ? { c := client.new(conf.address, conf.api_key) res := c.add_target(t)? - println("Target added with id $res.data") + println('Target added with id $res.data') } // remove removes a repository from the server's list. diff --git a/src/db/logs.v b/src/db/logs.v index af5f53c..923dde2 100644 --- a/src/db/logs.v +++ b/src/db/logs.v @@ -79,10 +79,14 @@ pub fn (db &VieterDb) get_build_log(id int) ?BuildLog { } // add_build_log inserts the given BuildLog into the database. -pub fn (db &VieterDb) add_build_log(log BuildLog) { +pub fn (db &VieterDb) add_build_log(log BuildLog) int { sql db.conn { insert log into BuildLog } + + inserted_id := db.conn.last_id() as int + + return inserted_id } // delete_build_log delete the BuildLog with the given ID from the database. diff --git a/src/server/api_logs.v b/src/server/api_logs.v index 021c1ac..287755a 100644 --- a/src/server/api_logs.v +++ b/src/server/api_logs.v @@ -19,7 +19,7 @@ fn (mut app App) v1_get_logs() web.Result { } logs := app.db.get_build_logs(filter) - return app.json(http.Status.ok, new_data_response(logs)) + return app.json(.ok, new_data_response(logs)) } // v1_get_single_log returns the build log with the given id. @@ -27,7 +27,7 @@ fn (mut app App) v1_get_logs() web.Result { fn (mut app App) v1_get_single_log(id int) web.Result { log := app.db.get_build_log(id) or { return app.not_found() } - return app.json(http.Status.ok, new_data_response(log)) + return app.json(.ok, new_data_response(log)) } // v1_get_log_content returns the actual build log file for the given id. @@ -95,7 +95,8 @@ fn (mut app App) v1_post_log() web.Result { exit_code: exit_code } - app.db.add_build_log(log) + // id of newly created log + log_id := app.db.add_build_log(log) repo_logs_dir := os.join_path(app.conf.data_dir, logs_dir_name, target_id.str(), arch) @@ -122,5 +123,5 @@ fn (mut app App) v1_post_log() web.Result { return app.status(http.Status.length_required) } - return app.json(http.Status.ok, new_response('Logs added successfully.')) + return app.json(.ok, new_data_response(log_id)) } diff --git a/src/server/api_targets.v b/src/server/api_targets.v index 5757d76..6f284af 100644 --- a/src/server/api_targets.v +++ b/src/server/api_targets.v @@ -2,7 +2,7 @@ module server import web import net.http -import web.response { new_data_response, new_response, new_full_response } +import web.response { new_data_response, new_response } import db import models { Target, TargetArch, TargetFilter } @@ -14,7 +14,7 @@ fn (mut app App) v1_get_targets() web.Result { } repos := app.db.get_targets(filter) - return app.json(http.Status.ok, new_data_response(repos)) + return app.json(.ok, new_data_response(repos)) } // v1_get_single_target returns the information for a single target. @@ -22,7 +22,7 @@ fn (mut app App) v1_get_targets() web.Result { fn (mut app App) v1_get_single_target(id int) web.Result { repo := app.db.get_target(id) or { return app.not_found() } - return app.json(http.Status.ok, new_data_response(repo)) + return app.json(.ok, new_data_response(repo)) } // v1_post_target creates a new target from the provided query string. @@ -55,7 +55,7 @@ fn (mut app App) v1_post_target() web.Result { fn (mut app App) v1_delete_target(id int) web.Result { app.db.delete_target(id) - return app.json(http.Status.ok, new_response('Repo removed successfully.')) + return app.status(.ok) } // v1_patch_target updates a target's data with the given query params. @@ -69,5 +69,5 @@ fn (mut app App) v1_patch_target(id int) web.Result { app.db.update_target_archs(id, arch_objs) } - return app.json(http.Status.ok, new_response('Repo updated successfully.')) + return app.status(.ok) }