|
@@ -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
|
+}
|