Skip to content

Commit

Permalink
complete superstar upsert functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sean3z committed Mar 1, 2017
1 parent 4c9cb70 commit 1fdc6dd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn rocket() -> Rocket {
rocket::ignite()
.manage(pool)
.mount("/superstar", routes![
superstar::create,
superstar::upsert,
superstar::retrieve,
superstar::search,
])
Expand Down
19 changes: 14 additions & 5 deletions src/superstar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,20 @@ use talent;

#[put("/<slug>", format = "application/json", data = "<superstar>")]
#[allow(unused_variables)]
pub fn create(pool: State<mysql::Pool>, slug: String, superstar: JSON<talent::Talent>) -> JSON<Value> {
talent::upsert_superstar(pool, superstar.into_inner());

// need to require admin privledges
JSON(json!({ "status": "ok" }))
pub fn upsert(pool: State<mysql::Pool>, slug: String, superstar: JSON<talent::Talent>) -> JSON<Value> {
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("/<slug>", format = "application/json")]
Expand Down
26 changes: 22 additions & 4 deletions src/talent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

use rocket::{State};
use mysql;
use std::error::Error;

#[derive(Debug, Serialize, Deserialize)]
pub struct Talent {
Expand Down Expand Up @@ -55,11 +56,11 @@ pub fn search_by_term(pool: State<mysql::Pool>, term: String) -> Vec<Talent> {
talents
}

pub fn upsert_superstar(pool: State<mysql::Pool>, talent: Talent) {
pub fn upsert_superstar(pool: State<mysql::Pool>, talent: Talent) -> Result<String, String> {
let params = params!{
"id" => talent.id,
"name" => talent.name,
"slug" => talent.slug,
"slug" => &talent.slug,
"tier" => talent.tier,
"active" => talent.active,
"faction" => talent.faction,
Expand All @@ -73,7 +74,6 @@ pub fn upsert_superstar(pool: State<mysql::Pool>, 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),
Expand All @@ -84,7 +84,25 @@ pub fn upsert_superstar(pool: State<mysql::Pool>, 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 ?
Expand Down

0 comments on commit 1fdc6dd

Please sign in to comment.