From 7b59277931172997759572ac5a00930d677a4d67 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Mon, 5 Sep 2022 10:10:02 +0200 Subject: [PATCH] feat: adding target returns id of added entry --- src/client/targets.v | 4 ++-- src/console/targets/targets.v | 2 +- src/db/targets.v | 6 +++++- src/server/api_targets.v | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/client/targets.v b/src/client/targets.v index f5258a4..c5e44fe 100644 --- a/src/client/targets.v +++ b/src/client/targets.v @@ -49,9 +49,9 @@ pub struct NewTarget { } // add_target adds a new target to the server. -pub fn (c &Client) add_target(t NewTarget) ?Response { +pub fn (c &Client) add_target(t NewTarget) ?Response { params := models.params_from(t) - data := c.send_request(Method.post, '/api/v1/targets', params)? + data := c.send_request(Method.post, '/api/v1/targets', params)? return data } diff --git a/src/console/targets/targets.v b/src/console/targets/targets.v index 66d48fb..a555936 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(res.message) + println("Target added with id $res.data") } // remove removes a repository from the server's list. diff --git a/src/db/targets.v b/src/db/targets.v index 9102033..a705ebb 100644 --- a/src/db/targets.v +++ b/src/db/targets.v @@ -38,10 +38,14 @@ pub fn (db &VieterDb) get_target(target_id int) ?Target { } // add_target inserts the given target into the database. -pub fn (db &VieterDb) add_target(repo Target) { +pub fn (db &VieterDb) add_target(repo Target) int { sql db.conn { insert repo into Target } + + inserted_id := db.conn.last_id() as int + + return inserted_id } // delete_target deletes the target with the given id from the database. diff --git a/src/server/api_targets.v b/src/server/api_targets.v index c9e7963..5757d76 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 } +import web.response { new_data_response, new_response, new_full_response } import db import models { Target, TargetArch, TargetFilter } @@ -45,9 +45,9 @@ fn (mut app App) v1_post_target() web.Result { return app.json(http.Status.bad_request, new_response('Invalid kind.')) } - app.db.add_target(new_repo) + id := app.db.add_target(new_repo) - return app.json(http.Status.ok, new_response('Repo added successfully.')) + return app.json(http.Status.ok, new_data_response(id)) } // v1_delete_target removes a given target from the server's list.