Browse Source

Rename Gists to Snippets

master
Dylan Baker 5 years ago
parent
commit
08b2e44e77

+ 1
- 0
migrations/2019-05-02-152131_rename_gists_to_snippets/down.sql View File

@@ -0,0 +1 @@
1
+ALTER TABLE snippets RENAME TO gists;

+ 1
- 0
migrations/2019-05-02-152131_rename_gists_to_snippets/up.sql View File

@@ -0,0 +1 @@
1
+ALTER TABLE gists RENAME TO snippets;

+ 4
- 4
src/main.rs View File

@@ -17,19 +17,19 @@ use dotenv::dotenv;
17 17
 use rocket_contrib::templates::Template;
18 18
 
19 19
 mod connection;
20
-mod gists;
21 20
 mod routes;
22 21
 mod schema;
22
+mod snippet;
23 23
 
24
-use crate::routes::static_rocket_route_info_for_create_gist;
24
+use crate::routes::static_rocket_route_info_for_create_snippet;
25 25
 use crate::routes::static_rocket_route_info_for_index;
26
-use crate::routes::static_rocket_route_info_for_show_gist;
26
+use crate::routes::static_rocket_route_info_for_show_snippet;
27 27
 
28 28
 fn main() {
29 29
     dotenv().ok();
30 30
     rocket::ignite()
31 31
         .attach(Template::fairing())
32 32
         .manage(connection::init_pool())
33
-        .mount("/", routes![index, show_gist, create_gist])
33
+        .mount("/", routes![index, show_snippet, create_snippet])
34 34
         .launch();
35 35
 }

+ 16
- 16
src/routes.rs View File

@@ -6,8 +6,8 @@ use rocket_contrib::templates::Template;
6 6
 use std::collections::HashMap;
7 7
 
8 8
 use crate::connection::DbConn;
9
-use crate::gists;
10
-use crate::gists::{Gist, InsertableGist};
9
+use crate::snippet;
10
+use crate::snippet::{InsertableSnippet, Snippet};
11 11
 
12 12
 fn error_response(error: Error) -> Status {
13 13
     match error {
@@ -22,34 +22,34 @@ pub fn index() -> Template {
22 22
     Template::render("index", &context)
23 23
 }
24 24
 
25
-#[get("/gists/<id>")]
26
-pub fn show_gist(id: i32, connection: DbConn) -> Template {
27
-    let result = match gists::get(&connection, id) {
28
-        Ok(gist) => Some(gist),
25
+#[get("/snippets/<id>")]
26
+pub fn show_snippet(id: i32, connection: DbConn) -> Template {
27
+    let result = match snippet::get(&connection, id) {
28
+        Ok(snippet) => Some(snippet),
29 29
         Err(_) => None,
30 30
     };
31 31
 
32 32
     let (title, body) = result.map_or(
33 33
         (
34
-            String::from("404 - Gist not found"),
35
-            format!("<h3'>{}</h3>", "Gist not found"),
34
+            String::from("404 - Snippet not found"),
35
+            format!("<h3'>{}</h3>", "Snippet not found"),
36 36
         ),
37
-        |gist| (gist.title, gist.formatted_body),
37
+        |snippet| (snippet.title, snippet.formatted_body),
38 38
     );
39 39
 
40 40
     let mut context: HashMap<&str, String> = HashMap::new();
41 41
     context.insert("title", title);
42 42
     context.insert("body", body);
43 43
 
44
-    Template::render("gists/show", &context)
44
+    Template::render("snippets/show", &context)
45 45
 }
46 46
 
47
-#[post("/api/gists", format = "application/json", data = "<gist>")]
48
-pub fn create_gist<'a>(
49
-    gist: Json<InsertableGist>,
47
+#[post("/api/snippets", format = "application/json", data = "<snippet>")]
48
+pub fn create_snippet<'a>(
49
+    snippet: Json<InsertableSnippet>,
50 50
     connection: DbConn,
51
-) -> Result<status::Created<Json<Gist>>, Status> {
52
-    gists::insert(gist.into_inner(), &connection)
53
-        .map(|gist| status::Created(String::from(""), Some(Json(gist))))
51
+) -> Result<status::Created<Json<Snippet>>, Status> {
52
+    snippet::insert(snippet.into_inner(), &connection)
53
+        .map(|snippet| status::Created(String::from(""), Some(Json(snippet))))
54 54
         .map_err(|error| error_response(error))
55 55
 }

+ 1
- 1
src/schema.rs View File

@@ -1,5 +1,5 @@
1 1
 table! {
2
-    gists (id) {
2
+    snippets (id) {
3 3
         id -> Int4,
4 4
         title -> Varchar,
5 5
         body -> Text,

src/gists.rs → src/snippet.rs View File

@@ -5,10 +5,10 @@ use syntect::highlighting::ThemeSet;
5 5
 use syntect::html::highlighted_html_for_string;
6 6
 use syntect::parsing::SyntaxSet;
7 7
 
8
-use crate::schema::gists;
8
+use crate::schema::snippets;
9 9
 
10 10
 #[derive(Queryable, Serialize, Deserialize)]
11
-pub struct Gist {
11
+pub struct Snippet {
12 12
     pub id: i32,
13 13
     pub title: String,
14 14
     pub body: String,
@@ -18,15 +18,15 @@ pub struct Gist {
18 18
 }
19 19
 
20 20
 #[derive(Insertable, AsChangeset, Serialize, Deserialize)]
21
-#[table_name = "gists"]
22
-pub struct InsertableGist {
21
+#[table_name = "snippets"]
22
+pub struct InsertableSnippet {
23 23
     pub title: String,
24 24
     pub body: String,
25 25
     pub filetype: Option<String>,
26 26
     pub formatted_body: String,
27 27
 }
28 28
 
29
-fn format_gist(filetype: Option<String>, body: String) -> String {
29
+fn format_snippet(filetype: Option<String>, body: String) -> String {
30 30
     filetype.map_or(
31 31
         format!("<pre class='plaintext'>{}</pre>", body),
32 32
         |filetype| {
@@ -42,18 +42,18 @@ fn format_gist(filetype: Option<String>, body: String) -> String {
42 42
     )
43 43
 }
44 44
 
45
-pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
46
-    gists::table.find(id).get_result::<Gist>(connection)
45
+pub fn get(connection: &PGC, id: i32) -> QueryResult<Snippet> {
46
+    snippets::table.find(id).get_result::<Snippet>(connection)
47 47
 }
48 48
 
49
-pub fn insert(gist: InsertableGist, connection: &PGC) -> QueryResult<Gist> {
50
-    let formatted_gist = InsertableGist {
51
-        filetype: gist.filetype.clone(),
52
-        title: gist.title,
53
-        body: gist.body.clone(),
54
-        formatted_body: format_gist(gist.filetype, gist.body),
49
+pub fn insert(snippet: InsertableSnippet, connection: &PGC) -> QueryResult<Snippet> {
50
+    let formatted_snippet = InsertableSnippet {
51
+        filetype: snippet.filetype.clone(),
52
+        title: snippet.title,
53
+        body: snippet.body.clone(),
54
+        formatted_body: format_snippet(snippet.filetype, snippet.body),
55 55
     };
56
-    diesel::insert_into(gists::table)
57
-        .values(formatted_gist)
56
+    diesel::insert_into(snippets::table)
57
+        .values(formatted_snippet)
58 58
         .get_result(connection)
59 59
 }

templates/gists/show.tera → templates/snippets/show.tera View File


Loading…
Cancel
Save