Преглед на файлове

Allow creating gists

master
Dylan Baker преди 5 години
родител
ревизия
c1ec8ada94
променени са 3 файла, в които са добавени 29 реда и са изтрити 4 реда
  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 Целия файл

10
     pub body: String,
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
 pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
20
 pub fn all(connection: &PGC) -> QueryResult<Vec<Gist>> {
14
     gists::table.load::<Gist>(&*connection)
21
     gists::table.load::<Gist>(&*connection)
15
 }
22
 }
17
 pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
24
 pub fn get(connection: &PGC, id: i32) -> QueryResult<Gist> {
18
     gists::table.find(id).get_result::<Gist>(connection)
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 Целия файл

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

+ 14
- 3
src/routes.rs Целия файл

1
 use diesel::result::Error;
1
 use diesel::result::Error;
2
 use rocket::http::Status;
2
 use rocket::http::Status;
3
+use rocket::response::status;
3
 use rocket_contrib::json::Json;
4
 use rocket_contrib::json::Json;
4
 use rocket_contrib::templates::Template;
5
 use rocket_contrib::templates::Template;
5
 use std::collections::HashMap;
6
 use std::collections::HashMap;
6
 
7
 
7
 use crate::connection::DbConn;
8
 use crate::connection::DbConn;
8
 use crate::gists;
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
     match error {
13
     match error {
13
         Error::NotFound => Status::NotFound,
14
         Error::NotFound => Status::NotFound,
14
         _ => Status::InternalServerError,
15
         _ => Status::InternalServerError,
19
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
20
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
20
     gists::all(&connection)
21
     gists::all(&connection)
21
         .map(|gists| Json(gists))
22
         .map(|gists| Json(gists))
22
-        .map_err(|error| error_status(error))
23
+        .map_err(|error| error_response(error))
23
 }
24
 }
24
 
25
 
25
 #[get("/gists/<id>")]
26
 #[get("/gists/<id>")]
32
     context.insert("gist", gist);
33
     context.insert("gist", gist);
33
     Template::render("gists/show", &context)
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…
Отказ
Запис