Browse Source

Allow creating gists

master
Dylan Baker 5 years ago
parent
commit
c1ec8ada94
3 changed files with 29 additions and 4 deletions
  1. 13
    0
      src/gists/mod.rs
  2. 2
    1
      src/main.rs
  3. 14
    3
      src/routes.rs

+ 13
- 0
src/gists/mod.rs View File

@@ -10,6 +10,13 @@ pub struct Gist {
10 10
     pub body: String,
11 11
 }
12 12
 
13
+#[derive(Insertable, Serialize, Deserialize)]
14
+#[table_name = "gists"]
15
+pub struct InsertableGist {
16
+    pub title: String,
17
+    pub body: String,
18
+}
19
+
13 20
 pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
14 21
     gists::table.load::<Gist>(&*connection)
15 22
 }
@@ -17,3 +24,9 @@ pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
17 24
 pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
18 25
     gists::table.find(id).get_result::<Gist>(connection)
19 26
 }
27
+
28
+pub fn insert(gist: InsertableGist, connection: &PGC) -> QueryResult<Gist> {
29
+    diesel::insert_into(gists::table)
30
+        .values(gist)
31
+        .get_result(connection)
32
+}

+ 2
- 1
src/main.rs View File

@@ -19,6 +19,7 @@ mod gists;
19 19
 mod routes;
20 20
 mod schema;
21 21
 
22
+use crate::routes::static_rocket_route_info_for_create_gist;
22 23
 use crate::routes::static_rocket_route_info_for_index;
23 24
 use crate::routes::static_rocket_route_info_for_show_gist;
24 25
 
@@ -27,6 +28,6 @@ fn main() {
27 28
     rocket::ignite()
28 29
         .attach(Template::fairing())
29 30
         .manage(connection::init_pool())
30
-        .mount("/", routes![index, show_gist])
31
+        .mount("/", routes![index, show_gist, create_gist])
31 32
         .launch();
32 33
 }

+ 14
- 3
src/routes.rs View File

@@ -1,14 +1,15 @@
1 1
 use diesel::result::Error;
2 2
 use rocket::http::Status;
3
+use rocket::response::status;
3 4
 use rocket_contrib::json::Json;
4 5
 use rocket_contrib::templates::Template;
5 6
 use std::collections::HashMap;
6 7
 
7 8
 use crate::connection::DbConn;
8 9
 use crate::gists;
9
-use crate::gists::Gist;
10
+use crate::gists::{Gist, InsertableGist};
10 11
 
11
-fn error_status(error: Error) -> Status {
12
+fn error_response(error: Error) -> Status {
12 13
     match error {
13 14
         Error::NotFound => Status::NotFound,
14 15
         _ => Status::InternalServerError,
@@ -19,7 +20,7 @@ fn error_status(error: Error) -> Status {
19 20
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
20 21
     gists::all(&connection)
21 22
         .map(|gists| Json(gists))
22
-        .map_err(|error| error_status(error))
23
+        .map_err(|error| error_response(error))
23 24
 }
24 25
 
25 26
 #[get("/gists/<id>")]
@@ -32,3 +33,13 @@ pub fn show_gist(connection: DbConn, id: i32) -> Template {
32 33
     context.insert("gist", gist);
33 34
     Template::render("gists/show", &context)
34 35
 }
36
+
37
+#[post("/api/gists", format = "application/json", data = "<gist>")]
38
+pub fn create_gist<'a>(
39
+    gist: Json<InsertableGist>,
40
+    connection: DbConn,
41
+) -> Result<status::Created<Json<Gist>>, Status> {
42
+    gists::insert(gist.into_inner(), &connection)
43
+        .map(|gist| status::Created(String::from(""), Some(Json(gist))))
44
+        .map_err(|error| error_response(error))
45
+}

Loading…
Cancel
Save