|
@@ -25,42 +25,20 @@ pub fn index() -> Template {
|
25
|
25
|
|
26
|
26
|
#[get("/snippets/<id>")]
|
27
|
27
|
pub fn show_snippet(id: i32, connection: DbConn) -> Template {
|
28
|
|
- let result = match snippet::get(&connection, id) {
|
29
|
|
- Ok(snippet) => Some(snippet),
|
30
|
|
- Err(_) => None,
|
31
|
|
- };
|
32
|
|
-
|
33
|
|
- let (id, title, body) = result.map_or(
|
34
|
|
- (
|
35
|
|
- String::from(""),
|
36
|
|
- String::from("404 - Snippet not found"),
|
37
|
|
- String::from(""),
|
38
|
|
- ),
|
39
|
|
- |snippet| {
|
40
|
|
- (
|
41
|
|
- format!("{}", snippet.id),
|
42
|
|
- snippet.title,
|
43
|
|
- snippet.formatted_body,
|
44
|
|
- )
|
45
|
|
- },
|
46
|
|
- );
|
47
|
|
-
|
48
|
|
- let mut context: HashMap<&str, String> = HashMap::new();
|
49
|
|
- context.insert("id", id);
|
50
|
|
- context.insert("title", title);
|
51
|
|
- context.insert("body", body);
|
|
28
|
+ let mut context: HashMap<&str, Snippet> = HashMap::new();
|
|
29
|
+ if let Ok(snippet) = snippet::get(&connection, id) {
|
|
30
|
+ context.insert("snippet", snippet);
|
|
31
|
+ }
|
52
|
32
|
|
53
|
33
|
Template::render("snippets/show", &context)
|
54
|
34
|
}
|
55
|
35
|
|
56
|
36
|
#[get("/snippets/<id>/raw")]
|
57
|
37
|
pub fn show_raw_snippet(id: i32, connection: DbConn) -> String {
|
58
|
|
- let result = match snippet::get(&connection, id) {
|
59
|
|
- Ok(snippet) => Some(snippet),
|
60
|
|
- Err(_) => None,
|
61
|
|
- };
|
62
|
|
-
|
63
|
|
- result.map_or(String::from("Snippet not found"), |snippet| snippet.body)
|
|
38
|
+ match snippet::get(&connection, id) {
|
|
39
|
+ Ok(snippet) => snippet.body,
|
|
40
|
+ Err(_) => String::from("Snippet not found"),
|
|
41
|
+ }
|
64
|
42
|
}
|
65
|
43
|
|
66
|
44
|
#[post("/api/snippets", format = "application/json", data = "<snippet>")]
|