From 1fdc6dd4ccb2fb4d70b4f812a0d5c31ea3e26137 Mon Sep 17 00:00:00 2001 From: sean3z Date: Tue, 28 Feb 2017 23:18:33 -0500 Subject: [PATCH] complete superstar upsert functionality --- src/main.rs | 2 +- src/superstar.rs | 19 ++++++++++++++----- src/talent.rs | 26 ++++++++++++++++++++++---- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b2369a4..d6b7fcc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,7 +34,7 @@ fn rocket() -> Rocket { rocket::ignite() .manage(pool) .mount("/superstar", routes![ - superstar::create, + superstar::upsert, superstar::retrieve, superstar::search, ]) diff --git a/src/superstar.rs b/src/superstar.rs index 017b6c2..bd4919b 100644 --- a/src/superstar.rs +++ b/src/superstar.rs @@ -8,11 +8,20 @@ use talent; #[put("/", format = "application/json", data = "")] #[allow(unused_variables)] -pub fn create(pool: State, slug: String, superstar: JSON) -> JSON { - talent::upsert_superstar(pool, superstar.into_inner()); - - // need to require admin privledges - JSON(json!({ "status": "ok" })) +pub fn upsert(pool: State, slug: String, superstar: JSON) -> JSON { + let upsert = talent::upsert_superstar(pool, superstar.into_inner()); + + if upsert.is_ok() { + return JSON(json!({ + "status": "ok", + "message": upsert.unwrap() + })) + } else { + return JSON(json!({ + "status": "error", + "message": upsert.unwrap_err() + })) + } } #[get("/", format = "application/json")] diff --git a/src/talent.rs b/src/talent.rs index 0849f66..077905b 100644 --- a/src/talent.rs +++ b/src/talent.rs @@ -1,6 +1,7 @@ use rocket::{State}; use mysql; +use std::error::Error; #[derive(Debug, Serialize, Deserialize)] pub struct Talent { @@ -55,11 +56,11 @@ pub fn search_by_term(pool: State, term: String) -> Vec { talents } -pub fn upsert_superstar(pool: State, talent: Talent) { +pub fn upsert_superstar(pool: State, talent: Talent) -> Result { let params = params!{ "id" => talent.id, "name" => talent.name, - "slug" => talent.slug, + "slug" => &talent.slug, "tier" => talent.tier, "active" => talent.active, "faction" => talent.faction, @@ -73,7 +74,6 @@ pub fn upsert_superstar(pool: State, talent: Talent) { INSERT INTO talent (id, `name`, slug, tier, active, faction, championship, `show`, image, bio) VALUES (:id, :name, :slug, :tier, :active, :faction, :championship, :show, :image, :bio) ON DUPLICATE KEY UPDATE - id = VALUES(id), `name` = VALUES(`name`), slug = VALUES(slug), tier = VALUES(tier), @@ -84,7 +84,25 @@ pub fn upsert_superstar(pool: State, talent: Talent) { image = VALUES(image), bio = VALUES(bio)"; - pool.prep_exec(query, params); + let result = pool.prep_exec(query, params); + + match result.is_ok() { + true => Ok(match result.unwrap().affected_rows() { + 0 => format!("{} record is the same", talent.slug), + 1 => format!("{} record created", talent.slug), + 2 => format!("{} record was updated", talent.slug), + _ => format!("no clue what the hell happened") + }), + false => Err(format!("{}", result.unwrap_err().cause().unwrap())) + } + + /* + With ON DUPLICATE KEY UPDATE, the affected-rows value per row: + 1 if the row is inserted as a new row + 2 if an existing row is updated + 0 if an existing row is set to its current values. + */ + // result.affected_rows(); // let query = " // INSERT INTO talent SET ?