Browse Source

Use good code instead of bad

master
Dylan Baker 5 years ago
parent
commit
b140e3bff4
2 changed files with 22 additions and 40 deletions
  1. 8
    30
      src/routes.rs
  2. 14
    10
      templates/snippets/show.tera

+ 8
- 30
src/routes.rs View File

@@ -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>")]

+ 14
- 10
templates/snippets/show.tera View File

@@ -1,12 +1,16 @@
1 1
 <html>
2 2
   <head>
3
-    <title>{{ title }}</title>
3
+    <title>{% if snippet %}{{ snippet.title }}{% else %}404 - snippet not found{% endif %}</title>
4 4
     <style>
5 5
       .container {
6 6
         margin: auto;
7 7
         max-width: 800px;
8 8
       }
9 9
 
10
+      .header {
11
+        padding: 15px;
12
+      }
13
+
10 14
       .header__raw-link {
11 15
         font-size: 16px;
12 16
         font-weight: normal;
@@ -16,25 +20,25 @@
16 20
         font-size: 16px;
17 21
         padding: 15px;
18 22
       }
19
-
20
-      .body > pre.plaintext {
21
-        padding: 0;
22
-      }
23 23
     </style>
24 24
     <meta name="viewport" content="width=device-width">
25 25
   </head>
26 26
   <body>
27 27
     <div class="container">
28 28
       <h1 class="header">
29
-        {{ title }}
29
+        {% if snippet %}
30
+          {{ snippet.title }}
30 31
 
31
-        {% if id %}
32
-          <a class="header__raw-link" href="/snippets/{{ id }}/raw">raw</a>
32
+          {% if id %}
33
+            <a class="header__raw-link" href="/snippets/{{ id }}/raw">raw</a>
34
+          {% endif %}
35
+        {% else %}
36
+          404 - snippet not found
33 37
         {% endif %}
34 38
       </h1>
35
-      {% if body %}
39
+      {% if snippet %}
36 40
         <div class="body">
37
-          {{ body | safe }}
41
+          {{ snippet.formatted_body | safe }}
38 42
         </div>
39 43
       {% endif %}
40 44
     </div>

Loading…
Cancel
Save