-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalent.rs
142 lines (123 loc) · 4.29 KB
/
talent.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use rocket::{State};
use mysql;
use std::error::Error;
#[derive(Debug, Serialize, Deserialize)]
pub struct Talent {
id: Option<i32>,
name: String,
slug: String,
tier: i32,
active: i32,
faction: Option<i32>,
championship: Option<i32>,
show: Option<i32>,
image: Option<String>,
bio: Option<String>
}
pub fn retrieve_by_slug(pool: State<mysql::Pool>, slug: String) -> Option<Talent> {
let params = params!{
"slug" => slug
};
let talent: Option<Talent> =
pool.prep_exec("SELECT id, `name`, slug, tier, active, faction, championship, `show`, image, bio FROM talent WHERE slug = :slug", params)
.map(|mut result| {
let row = result.next();
if row.is_none() {
None
} else {
let row = row.unwrap().unwrap();
let talent: Talent = row_to_talent(row);
Some(talent)
}
}).unwrap();
talent
}
pub fn search_by_term(pool: State<mysql::Pool>, term: String) -> Vec<Talent> {
let params = params!{
"term" => term
};
let talents: Vec<Talent> =
pool.prep_exec("SELECT id, `name`, slug, tier, active, faction, championship, `show`, null as image, null as bio FROM talent WHERE name LIKE CONCAT('%', :term, '%') OR slug LIKE CONCAT('%', :term, '%')", params)
.map(|result| {
result.map(|x| x.unwrap()).map(|row| {
let talent: Talent = row_to_talent(row);
talent
}).collect()
}).unwrap();
talents
}
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,
"tier" => talent.tier,
"active" => talent.active,
"faction" => talent.faction,
"championship" => talent.championship,
"show" => talent.show,
"image" => talent.image,
"bio" => talent.bio
};
let query = "
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
`name` = VALUES(`name`),
slug = VALUES(slug),
tier = VALUES(tier),
active = VALUES(active),
faction = VALUES(faction),
championship = VALUES(championship),
`show` = VALUES(`show`),
image = VALUES(image),
bio = VALUES(bio)";
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 ?
// ON DUPLICATE KEY UPDATE
// id = VALUES(id),
// `name` = VALUES(`name`),
// slug = VALUES(slug),
// tier = VALUES(tier),
// active = VALUES(active),
// faction = VALUES(faction),
// championship = VALUES(championship),
// `show` = VALUES(`show`),
// image = VALUES(image),
// bio = VALUES(bio)";
// let mut stmt = pool.prepare(query).unwrap();
// let result = stmt.execute(params).unwrap();
// println!("{:?}", result);
}
fn row_to_talent(row: mysql::Row) -> Talent {
let (id, name, slug, tier, active, faction, championship, show, image, bio) = mysql::from_row(row);
Talent {
id: id,
name: name,
slug: slug,
tier: tier,
active: active,
faction: faction,
championship: championship,
show: show,
image: image,
bio: bio
}
}