Преглед изворни кода

Use good code instead of bad

master
Dylan Baker пре 5 година
родитељ
комит
b140e3bff4
2 измењених фајлова са 22 додато и 40 уклоњено
  1. 8
    30
      src/routes.rs
  2. 14
    10
      templates/snippets/show.tera

+ 8
- 30
src/routes.rs Прегледај датотеку

25
 
25
 
26
 #[get("/snippets/<id>")]
26
 #[get("/snippets/<id>")]
27
 pub fn show_snippet(id: i32, connection: DbConn) -> Template {
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
     Template::render("snippets/show", &context)
33
     Template::render("snippets/show", &context)
54
 }
34
 }
55
 
35
 
56
 #[get("/snippets/<id>/raw")]
36
 #[get("/snippets/<id>/raw")]
57
 pub fn show_raw_snippet(id: i32, connection: DbConn) -> String {
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
 #[post("/api/snippets", format = "application/json", data = "<snippet>")]
44
 #[post("/api/snippets", format = "application/json", data = "<snippet>")]

+ 14
- 10
templates/snippets/show.tera Прегледај датотеку

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

Loading…
Откажи
Сачувај