diff options
Diffstat (limited to 'rust/src')
-rw-r--r-- | rust/src/api_helpers.rs | 25 | ||||
-rw-r--r-- | rust/src/api_server.rs | 92 | ||||
-rw-r--r-- | rust/src/bin/fatcatd.rs | 2 | ||||
-rw-r--r-- | rust/src/database_models.rs | 16 | ||||
-rw-r--r-- | rust/src/database_schema.rs | 1 | ||||
-rw-r--r-- | rust/src/lib.rs | 5 |
6 files changed, 69 insertions, 72 deletions
diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs index 247eed88..62fc4569 100644 --- a/rust/src/api_helpers.rs +++ b/rust/src/api_helpers.rs @@ -1,9 +1,8 @@ - -use errors::*; -use diesel; -use diesel::prelude::*; use database_models::*; use database_schema::*; +use diesel; +use diesel::prelude::*; +use errors::*; pub fn get_or_create_editgroup(editor_id: i64, conn: &PgConnection) -> Result<i64> { // check for current active @@ -14,11 +13,8 @@ pub fn get_or_create_editgroup(editor_id: i64, conn: &PgConnection) -> Result<i6 // 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), - )) + .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)) @@ -29,7 +25,6 @@ pub fn get_or_create_editgroup(editor_id: i64, conn: &PgConnection) -> Result<i6 pub fn accept_editgroup(editgroup_id: i64, conn: &PgConnection) -> Result<ChangelogRow> { conn.build_transaction().run(|| { - // check that we haven't accepted already (in changelog) // NB: could leave this to a UNIQUE constraint let count: i64 = changelog::table @@ -60,8 +55,8 @@ pub fn accept_editgroup(editgroup_id: i64, conn: &PgConnection) -> Result<Change // Sketchy... but fast? Only a few queries per accept. for entity in ["container", "creator", "file", "work", "release"].iter() { - diesel::sql_query( - format!(" + diesel::sql_query(format!( + " UPDATE {entity}_ident SET is_live = true, @@ -71,16 +66,14 @@ pub fn accept_editgroup(editgroup_id: i64, conn: &PgConnection) -> Result<Change WHERE {entity}_ident.id = {entity}_edit.ident_id AND {entity}_edit.editgroup_id = $1", - entity = entity)) - .bind::<diesel::sql_types::BigInt, _>(editgroup_id) + entity = entity + )).bind::<diesel::sql_types::BigInt, _>(editgroup_id) .execute(conn)?; } // append log/changelog row let entry: ChangelogRow = diesel::insert_into(changelog::table) - .values(( - changelog::editgroup_id.eq(editgroup_id), - )) + .values((changelog::editgroup_id.eq(editgroup_id),)) .get_result(conn)?; // update any editor's active editgroup diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 3016b92b..c91af9c9 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -1,8 +1,8 @@ //! API endpoint handlers use ConnectionPool; -use chrono; use api_helpers::*; +use chrono; use database_models::*; use database_schema::{changelog, container_ident, container_rev, creator_ident, creator_rev, editgroup, editor, file_ident, file_rev, release_ident, release_rev, @@ -98,7 +98,7 @@ impl Server { name: rev.name, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -128,7 +128,7 @@ impl Server { name: rev.name, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -155,7 +155,7 @@ impl Server { orcid: rev.orcid, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -183,7 +183,7 @@ impl Server { orcid: rev.orcid, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -207,11 +207,11 @@ impl Server { let entity = FileEntity { sha1: rev.sha1, - size: rev.size.map(|v| v as isize), + size: rev.size.map(|v| v as i64), url: rev.url, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id.map(|v| v), redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -236,11 +236,11 @@ impl Server { let entity = FileEntity { sha1: rev.sha1, - size: rev.size.map(|v| v as isize), + size: rev.size.map(|v| v as i64), url: rev.url, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id.map(|v| v), redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -266,7 +266,7 @@ impl Server { work_type: rev.work_type, state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -300,7 +300,7 @@ impl Server { work_id: rev.work_ident_id.to_string(), state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; @@ -335,22 +335,23 @@ impl Server { work_id: rev.work_ident_id.to_string(), state: Some(ident.state().unwrap().shortname()), ident: Some(ident.id.to_string()), - revision: ident.rev_id.map(|v| v as isize), + revision: ident.rev_id, redirect: ident.redirect_id.map(|u| u.to_string()), editgroup_id: None, }; Ok(Some(entity)) } - fn editgroup_id_get_handler(&self, id: i32) -> Result<Option<Editgroup>> { + fn editgroup_id_get_handler(&self, id: i64) -> Result<Option<Editgroup>> { let conn = self.db_pool.get().expect("db_pool error"); let row: EditgroupRow = editgroup::table.find(id as i64).first(&conn)?; let eg = Editgroup { - id: Some(row.id as isize), - editor_id: row.editor_id as isize, + id: Some(row.id), + editor_id: row.editor_id, description: row.description, + extra: None, }; Ok(Some(eg)) } @@ -383,8 +384,8 @@ impl Server { let entries = changes .iter() .map(|(row, _)| ChangelogentriesInner { - index: row.id as isize, - editgroup_id: row.editgroup_id as isize, + index: row.id, + editgroup_id: row.editgroup_id, timestamp: chrono::DateTime::from_utc(row.timestamp, chrono::Utc), }) .collect(); @@ -417,7 +418,7 @@ impl Api for Server { editgroup_id_get, editgroup_id_get_handler, EditgroupIdGetResponse, - i32 + i64 ); wrap_lookup_handler!( @@ -455,7 +456,7 @@ impl Api for Server { _context: &Context, ) -> Box<Future<Item = ContainerPostResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let editor_id = 1; // TODO: auth + 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, @@ -480,10 +481,10 @@ impl Api for Server { let edit = &edit; let entity_edit = EntityEdit { - editgroup_id: Some(edit.editgroup_id as isize), - revision: Some(edit.rev_id.unwrap() as isize), + editgroup_id: Some(edit.editgroup_id), + revision: Some(edit.rev_id.unwrap()), ident: Some(edit.ident_id.to_string()), - edit_id: Some(edit.id as isize), + edit_id: Some(edit.id), }; Box::new(futures::done(Ok(ContainerPostResponse::CreatedEntity( entity_edit, @@ -496,7 +497,7 @@ impl Api for Server { _context: &Context, ) -> Box<Future<Item = CreatorPostResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let editor_id = 1; // TODO: auth + 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, @@ -520,10 +521,10 @@ impl Api for Server { let edit = &edit; let entity_edit = EntityEdit { - editgroup_id: Some(edit.editgroup_id as isize), - revision: Some(edit.rev_id.unwrap() as isize), + editgroup_id: Some(edit.editgroup_id), + revision: Some(edit.rev_id.unwrap()), ident: Some(edit.ident_id.to_string()), - edit_id: Some(edit.id as isize), + edit_id: Some(edit.id), }; Box::new(futures::done(Ok(CreatorPostResponse::CreatedEntity( entity_edit, @@ -536,7 +537,7 @@ impl Api for Server { _context: &Context, ) -> Box<Future<Item = FilePostResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let editor_id = 1; // TODO: auth + 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, @@ -563,10 +564,10 @@ impl Api for Server { let edit = &edit; let entity_edit = EntityEdit { - editgroup_id: Some(edit.editgroup_id as isize), - revision: Some(edit.rev_id.unwrap() as isize), + editgroup_id: Some(edit.editgroup_id), + revision: Some(edit.rev_id.unwrap()), ident: Some(edit.ident_id.to_string()), - edit_id: Some(edit.id as isize), + edit_id: Some(edit.id), }; Box::new(futures::done(Ok(FilePostResponse::CreatedEntity( entity_edit, @@ -579,7 +580,7 @@ impl Api for Server { _context: &Context, ) -> Box<Future<Item = WorkPostResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let editor_id = 1; // TODO: auth + 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, @@ -603,10 +604,10 @@ impl Api for Server { let edit = &edit; let entity_edit = EntityEdit { - editgroup_id: Some(edit.editgroup_id as isize), - revision: Some(edit.rev_id.unwrap() as isize), + editgroup_id: Some(edit.editgroup_id), + revision: Some(edit.rev_id.unwrap()), ident: Some(edit.ident_id.to_string()), - edit_id: Some(edit.id as isize), + edit_id: Some(edit.id), }; Box::new(futures::done(Ok(WorkPostResponse::CreatedEntity( entity_edit, @@ -619,7 +620,7 @@ impl Api for Server { _context: &Context, ) -> Box<Future<Item = ReleasePostResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let editor_id = 1; // TODO: auth + 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, @@ -656,10 +657,10 @@ impl Api for Server { let edit = &edit; let entity_edit = EntityEdit { - editgroup_id: Some(edit.editgroup_id as isize), - revision: Some(edit.rev_id.unwrap() as isize), + editgroup_id: Some(edit.editgroup_id), + revision: Some(edit.rev_id.unwrap()), ident: Some(edit.ident_id.to_string()), - edit_id: Some(edit.id as isize), + edit_id: Some(edit.id), }; Box::new(futures::done(Ok(ReleasePostResponse::CreatedEntity( entity_edit, @@ -668,16 +669,18 @@ impl Api for Server { fn editgroup_id_accept_post( &self, - id: i32, + id: i64, _context: &Context, ) -> Box<Future<Item = EditgroupIdAcceptPostResponse, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); accept_editgroup(id as i64, &conn).expect("failed to accept editgroup"); - Box::new(futures::done(Ok(EditgroupIdAcceptPostResponse::MergedSuccessfully( - Success { message: "horray!".to_string() } - )))) + Box::new(futures::done(Ok( + EditgroupIdAcceptPostResponse::MergedSuccessfully(Success { + message: "horray!".to_string(), + }), + ))) } fn editgroup_post( @@ -696,9 +699,10 @@ impl Api for Server { .expect("error creating edit group"); let new_eg = Editgroup { - id: Some(row.id as isize), - editor_id: row.editor_id as isize, + id: Some(row.id), + editor_id: row.editor_id, description: row.description, + extra: None, }; Box::new(futures::done(Ok( EditgroupPostResponse::SuccessfullyCreated(new_eg), diff --git a/rust/src/bin/fatcatd.rs b/rust/src/bin/fatcatd.rs index 460246b5..b50da2be 100644 --- a/rust/src/bin/fatcatd.rs +++ b/rust/src/bin/fatcatd.rs @@ -4,12 +4,12 @@ extern crate chrono; extern crate clap; extern crate diesel; //extern crate dotenv; +extern crate error_chain; extern crate fatcat; extern crate fatcat_api; extern crate futures; extern crate iron; extern crate iron_slog; -extern crate error_chain; #[macro_use] extern crate slog; extern crate slog_async; diff --git a/rust/src/database_models.rs b/rust/src/database_models.rs index 0e138439..33676c95 100644 --- a/rust/src/database_models.rs +++ b/rust/src/database_models.rs @@ -1,8 +1,8 @@ use chrono; -//use serde_json; use database_schema::*; -use uuid::Uuid; use errors::*; +use serde_json; +use uuid::Uuid; // Ugh. I thought the whole point was to *not* do this, but: // https://github.com/diesel-rs/diesel/issues/1589 @@ -19,7 +19,7 @@ impl EntityState { match self { EntityState::WorkInProgress => "wip", EntityState::Active(_) => "active", - EntityState::Redirect(_,_) => "redirect", + EntityState::Redirect(_, _) => "redirect", EntityState::Deleted => "deleted", }.to_string() } @@ -40,7 +40,7 @@ macro_rules! entity_structs { pub rev_id: Option<i64>, pub redirect_id: Option<Uuid>, pub editgroup_id: i64, - //pub extra_json: Option<String>, + //pub extra_json: Option<serde_json::Value>, } #[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)] @@ -61,7 +61,7 @@ macro_rules! entity_structs { (None, None) => Ok(EntityState::Deleted), (Some(redir), Some(rev)) => Ok(EntityState::Redirect(redir, rev)), (None, Some(rev)) => Ok(EntityState::Active(rev)), - _ => bail!("Invalid EntityIdentRow state") + _ => bail!("Invalid EntityIdentRow state"), } } } @@ -89,7 +89,7 @@ entity_structs!( #[table_name = "creator_rev"] pub struct CreatorRevRow { pub id: i64, - //extra_json: Option<Json>, + //extra_json: Option<String>, pub name: String, pub orcid: Option<String>, } @@ -105,7 +105,7 @@ entity_structs!( #[table_name = "file_rev"] pub struct FileRevRow { pub id: i64, - //extra_json: Option<Json>, + //extra_json: Option<String>, pub size: Option<i32>, pub sha1: Option<String>, pub url: Option<String>, @@ -181,7 +181,7 @@ pub struct FileReleaseRow { #[table_name = "editgroup"] pub struct EditgroupRow { pub id: i64, - //extra_json: Option<Json>, + pub extra_json: Option<serde_json::Value>, pub editor_id: i64, pub description: Option<String>, } diff --git a/rust/src/database_schema.rs b/rust/src/database_schema.rs index f355107a..b25c52e2 100644 --- a/rust/src/database_schema.rs +++ b/rust/src/database_schema.rs @@ -64,6 +64,7 @@ table! { table! { editgroup (id) { id -> Int8, + extra_json -> Nullable<Json>, editor_id -> Int8, description -> Nullable<Text>, } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index c12575a9..0038f9db 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,5 +1,5 @@ -extern crate fatcat_api; extern crate chrono; +extern crate fatcat_api; #[macro_use] extern crate diesel; extern crate diesel_migrations; @@ -14,8 +14,8 @@ extern crate error_chain; extern crate iron; extern crate serde_json; -pub mod api_server; pub mod api_helpers; +pub mod api_server; pub mod database_models; pub mod database_schema; @@ -68,7 +68,6 @@ pub fn server() -> Result<api_server::Server> { } pub fn test_server() -> Result<api_server::Server> { - dotenv().ok(); let database_url = env::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set"); env::set_var("DATABASE_URL", database_url); |