aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2018-05-23 20:19:41 -0700
committerBryan Newbold <bnewbold@robocracy.org>2018-05-24 15:21:32 -0700
commitaff11938989901327fdf0198d08f97b7105e7b3b (patch)
treedaa370f45ee809fadf56c95d4b08c26c9c181218
parentcf937ed89c3fe10d891c41612017301a7ff0fbae (diff)
downloadfatcat-aff11938989901327fdf0198d08f97b7105e7b3b.tar.gz
fatcat-aff11938989901327fdf0198d08f97b7105e7b3b.zip
get_or_create_editgroup() helper for POSTs
-rw-r--r--rust/src/api_helpers.rs28
-rw-r--r--rust/src/api_server.rs41
-rw-r--r--rust/src/lib.rs1
-rw-r--r--rust/tests/test_api_server.rs23
4 files changed, 78 insertions, 15 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs
new file mode 100644
index 00000000..200b70a8
--- /dev/null
+++ b/rust/src/api_helpers.rs
@@ -0,0 +1,28 @@
+
+use errors::*;
+use diesel;
+use diesel::prelude::*;
+use database_models::*;
+use database_schema::{editgroup, editor};
+
+pub fn get_or_create_editgroup(editor_id: i64, conn: &PgConnection) -> Result<i64> {
+ // check for current active
+ let ed_row: EditorRow = editor::table.find(editor_id).first(conn)?;
+ if let Some(current) = ed_row.active_editgroup_id {
+ return Ok(current);
+ }
+
+ // need to insert and update
+ conn.build_transaction().run(|| {
+
+ let eg_row: EditgroupRow = diesel::insert_into(editgroup::table)
+ .values((
+ editgroup::editor_id.eq(ed_row.id),
+ ))
+ .get_result(conn)?;
+ diesel::update(editor::table.find(ed_row.id))
+ .set(editor::active_editgroup_id.eq(eg_row.id))
+ .execute(conn)?;
+ Ok(eg_row.id)
+ })
+}
diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs
index 29d9a555..b183f938 100644
--- a/rust/src/api_server.rs
+++ b/rust/src/api_server.rs
@@ -2,6 +2,7 @@
use ConnectionPool;
use chrono;
+use api_helpers::*;
use database_models::*;
use database_schema::{changelog, container_edit, container_ident, container_rev, creator_edit,
creator_ident, creator_rev, editgroup, editor, file_edit, file_ident,
@@ -454,10 +455,12 @@ impl Api for Server {
body: models::ContainerEntity,
_context: &Context,
) -> Box<Future<Item = ContainerPostResponse, Error = ApiError> + Send> {
- //let editgroup_id: i64 = body.editgroup.expect("need editgroup_id") as i64;
- // TODO: or find/create
- let editgroup_id = 1;
let conn = self.db_pool.get().expect("db_pool error");
+ let editor_id = 1; // TODO: auth
+ let editgroup_id = match body.editgroup_id {
+ None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
+ Some(param) => param as i64,
+ };
let edit: ContainerEditRow = diesel::sql_query(
"WITH rev AS ( INSERT INTO container_rev (name, publisher, issn)
@@ -493,10 +496,12 @@ impl Api for Server {
body: models::CreatorEntity,
_context: &Context,
) -> Box<Future<Item = CreatorPostResponse, Error = ApiError> + Send> {
- //let editgroup_id: i64 = body.editgroup.expect("need editgroup_id") as i64;
- // TODO: or find/create
- let editgroup_id = 1;
let conn = self.db_pool.get().expect("db_pool error");
+ let editor_id = 1; // TODO: auth
+ let editgroup_id = match body.editgroup_id {
+ None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
+ Some(param) => param as i64,
+ };
let edit: CreatorEditRow = diesel::sql_query(
"WITH rev AS ( INSERT INTO creator_rev (name, orcid)
@@ -531,10 +536,12 @@ impl Api for Server {
body: models::FileEntity,
_context: &Context,
) -> Box<Future<Item = FilePostResponse, Error = ApiError> + Send> {
- //let editgroup_id: i64 = body.editgroup.expect("need editgroup_id") as i64;
- // TODO: or find/create
- let editgroup_id = 1;
let conn = self.db_pool.get().expect("db_pool error");
+ let editor_id = 1; // TODO: auth
+ let editgroup_id = match body.editgroup_id {
+ None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
+ Some(param) => param as i64,
+ };
let edit: FileEditRow = diesel::sql_query(
"WITH rev AS ( INSERT INTO file_rev (size, sha1, url)
@@ -572,10 +579,12 @@ impl Api for Server {
body: models::WorkEntity,
_context: &Context,
) -> Box<Future<Item = WorkPostResponse, Error = ApiError> + Send> {
- //let editgroup_id: i64 = body.editgroup.expect("need editgroup_id") as i64;
- // TODO: or find/create
- let editgroup_id = 1;
let conn = self.db_pool.get().expect("db_pool error");
+ let editor_id = 1; // TODO: auth
+ let editgroup_id = match body.editgroup_id {
+ None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
+ Some(param) => param as i64,
+ };
let edit: WorkEditRow =
diesel::sql_query(
@@ -610,10 +619,12 @@ impl Api for Server {
body: models::ReleaseEntity,
_context: &Context,
) -> Box<Future<Item = ReleasePostResponse, Error = ApiError> + Send> {
- //let editgroup_id: i64 = body.editgroup.expect("need editgroup_id") as i64;
- // TODO: or find/create
- let editgroup_id = 1;
let conn = self.db_pool.get().expect("db_pool error");
+ let editor_id = 1; // TODO: auth
+ let editgroup_id = match body.editgroup_id {
+ None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"),
+ Some(param) => param as i64,
+ };
let work_id = uuid::Uuid::parse_str(&body.work_id).expect("invalid UUID");
let _container_id: Option<uuid::Uuid> = match body.container_id {
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 37929ac8..4e1dc8ba 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -15,6 +15,7 @@ extern crate iron;
extern crate serde_json;
pub mod api_server;
+pub mod api_helpers;
pub mod database_models;
pub mod database_schema;
diff --git a/rust/tests/test_api_server.rs b/rust/tests/test_api_server.rs
index fdabdea2..4c16d4df 100644
--- a/rust/tests/test_api_server.rs
+++ b/rust/tests/test_api_server.rs
@@ -4,6 +4,8 @@ extern crate iron;
extern crate iron_test;
use iron::{status, Headers};
+use iron::mime::Mime;
+use iron::headers::ContentType;
use iron_test::{request, response};
#[test]
@@ -51,3 +53,24 @@ fn test_lookups() {
let body = response::extract_body_to_string(response);
assert!(body.contains("Christine Moran"));
}
+
+#[test]
+fn test_post_container() {
+ let server = fatcat::server().unwrap();
+ let router = fatcat_api::router(server);
+ let mut headers = Headers::new();
+ let mime: Mime = "application/json".parse().unwrap();
+ headers.set(ContentType(mime));
+
+
+ let response = request::post(
+ "http://localhost:9411/v0/container",
+ headers,
+ r#"{"name": "test journal"}"#,
+ &router,
+ ).unwrap();
+ assert_eq!(response.status, Some(status::Created));
+ let body = response::extract_body_to_string(response);
+ println!("{}", body);
+ //assert!(body.contains("test journal"));
+}