feat: adding target returns id of added entry

pull/271/head
Jef Roosens 2022-09-05 10:10:02 +02:00
parent 3e0a2584fa
commit 7b59277931
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
4 changed files with 11 additions and 7 deletions

View File

@ -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<string> {
pub fn (c &Client) add_target(t NewTarget) ?Response<int> {
params := models.params_from<NewTarget>(t)
data := c.send_request<string>(Method.post, '/api/v1/targets', params)?
data := c.send_request<int>(Method.post, '/api/v1/targets', params)?
return data
}

View File

@ -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.

View File

@ -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.

View File

@ -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.