Browse Source

Add route to view raw snippet

master
Dylan Baker 5 years ago
parent
commit
09332e7ea8
2 changed files with 15 additions and 1 deletions
  1. 5
    1
      src/main.rs
  2. 10
    0
      src/routes.rs

+ 5
- 1
src/main.rs View File

@@ -25,6 +25,7 @@ mod snippet;
25 25
 use crate::routes::static_rocket_catch_info_for_bad_request;
26 26
 use crate::routes::static_rocket_route_info_for_create_snippet;
27 27
 use crate::routes::static_rocket_route_info_for_index;
28
+use crate::routes::static_rocket_route_info_for_show_raw_snippet;
28 29
 use crate::routes::static_rocket_route_info_for_show_snippet;
29 30
 
30 31
 fn main() {
@@ -32,7 +33,10 @@ fn main() {
32 33
     rocket::ignite()
33 34
         .attach(Template::fairing())
34 35
         .manage(connection::init_pool())
35
-        .mount("/", routes![index, show_snippet, create_snippet])
36
+        .mount(
37
+            "/",
38
+            routes![index, show_snippet, show_raw_snippet, create_snippet],
39
+        )
36 40
         .register(catchers![bad_request])
37 41
         .launch();
38 42
 }

+ 10
- 0
src/routes.rs View File

@@ -45,6 +45,16 @@ pub fn show_snippet(id: i32, connection: DbConn) -> Template {
45 45
     Template::render("snippets/show", &context)
46 46
 }
47 47
 
48
+#[get("/snippets/<id>/raw")]
49
+pub fn show_raw_snippet(id: i32, connection: DbConn) -> String {
50
+    let result = match snippet::get(&connection, id) {
51
+        Ok(snippet) => Some(snippet),
52
+        Err(_) => None,
53
+    };
54
+
55
+    result.map_or(String::from("Snippet not found"), |snippet| snippet.body)
56
+}
57
+
48 58
 #[post("/api/snippets", format = "application/json", data = "<snippet>")]
49 59
 pub fn create_snippet(
50 60
     snippet: Json<InsertableSnippet>,

Loading…
Cancel
Save