diff options
Diffstat (limited to 'rust/src')
-rw-r--r-- | rust/src/api_entity_crud.rs | 37 | ||||
-rw-r--r-- | rust/src/api_helpers.rs | 41 | ||||
-rw-r--r-- | rust/src/api_server.rs | 14 | ||||
-rw-r--r-- | rust/src/api_wrappers.rs | 19 |
4 files changed, 52 insertions, 59 deletions
diff --git a/rust/src/api_entity_crud.rs b/rust/src/api_entity_crud.rs index 401b9835..1cb9adf8 100644 --- a/rust/src/api_entity_crud.rs +++ b/rust/src/api_entity_crud.rs @@ -41,8 +41,6 @@ where type IdentNewRow; type RevRow; - fn parse_editgroup_id(&self) -> Result<Option<FatCatId>>; - // Generic Methods fn db_get(conn: &DbConn, ident: FatCatId) -> Result<Self>; fn db_get_rev(conn: &DbConn, rev_id: Uuid) -> Result<Self>; @@ -81,18 +79,6 @@ where fn db_insert_revs(conn: &DbConn, models: &[&Self]) -> Result<Vec<Uuid>>; } -// TODO: this could be a separate trait on all entities -macro_rules! generic_parse_editgroup_id{ - () => { - fn parse_editgroup_id(&self) -> Result<Option<FatCatId>> { - match &self.editgroup_id { - Some(s) => Ok(Some(FatCatId::from_str(&s)?)), - None => Ok(None), - } - } - } -} - macro_rules! generic_db_get { ($ident_table:ident, $rev_table:ident) => { fn db_get(conn: &DbConn, ident: FatCatId) -> Result<Self> { @@ -394,7 +380,6 @@ impl EntityCrud for ContainerEntity { type IdentNewRow = ContainerIdentNewRow; type RevRow = ContainerRevRow; - generic_parse_editgroup_id!(); generic_db_get!(container_ident, container_rev); generic_db_get_rev!(container_rev); generic_db_expand!(); @@ -432,7 +417,7 @@ impl EntityCrud for ContainerEntity { revision: Some(rev_row.id.to_string()), redirect: redirect_id, extra: rev_row.extra_json, - editgroup_id: None, + edit_extra: None, }) } @@ -475,7 +460,6 @@ impl EntityCrud for CreatorEntity { type IdentNewRow = CreatorIdentNewRow; type RevRow = CreatorRevRow; - generic_parse_editgroup_id!(); generic_db_get!(creator_ident, creator_rev); generic_db_get_rev!(creator_rev); generic_db_expand!(); @@ -510,8 +494,8 @@ impl EntityCrud for CreatorEntity { ident: ident_id, revision: Some(rev_row.id.to_string()), redirect: redirect_id, - editgroup_id: None, extra: rev_row.extra_json, + edit_extra: None, }) } @@ -553,7 +537,6 @@ impl EntityCrud for FileEntity { type IdentNewRow = FileIdentNewRow; type RevRow = FileRevRow; - generic_parse_editgroup_id!(); generic_db_get!(file_ident, file_rev); generic_db_get_rev!(file_rev); generic_db_expand!(); @@ -608,8 +591,8 @@ impl EntityCrud for FileEntity { ident: ident_id, revision: Some(rev_row.id.to_string()), redirect: redirect_id, - editgroup_id: None, extra: rev_row.extra_json, + edit_extra: None, }) } @@ -691,7 +674,6 @@ impl EntityCrud for ReleaseEntity { type IdentNewRow = ReleaseIdentNewRow; type RevRow = ReleaseRevRow; - generic_parse_editgroup_id!(); generic_db_get!(release_ident, release_rev); generic_db_get_rev!(release_rev); //generic_db_create!(release_ident, release_edit); @@ -703,18 +685,16 @@ impl EntityCrud for ReleaseEntity { generic_db_insert_rev!(); fn db_expand(&mut self, conn: &DbConn, expand: ExpandFlags) -> Result<()> { - let ident = match &self.ident { None => bail!("Can't expand a non-concrete entity"), - Some(s) => FatCatId::from_str(&s)? + Some(s) => FatCatId::from_str(&s)?, }; if expand.files { self.files = Some(get_release_files(ident, conn)?); } if expand.container { if let Some(ref cid) = self.container_id { - self.container = - Some(ContainerEntity::db_get(conn, FatCatId::from_str(&cid)?)?); + self.container = Some(ContainerEntity::db_get(conn, FatCatId::from_str(&cid)?)?); } } Ok(()) @@ -744,8 +724,8 @@ impl EntityCrud for ReleaseEntity { revision: None, redirect: None, state: None, - editgroup_id: None, extra: None, + edit_extra: None, }); }; } @@ -904,8 +884,8 @@ impl EntityCrud for ReleaseEntity { ident: ident_id, revision: Some(rev_row.id.to_string()), redirect: redirect_id, - editgroup_id: None, extra: rev_row.extra_json, + edit_extra: None, }) } @@ -1086,7 +1066,6 @@ impl EntityCrud for WorkEntity { type IdentNewRow = WorkIdentNewRow; type RevRow = WorkRevRow; - generic_parse_editgroup_id!(); generic_db_get!(work_ident, work_rev); generic_db_get_rev!(work_rev); generic_db_expand!(); @@ -1117,8 +1096,8 @@ impl EntityCrud for WorkEntity { ident: ident_id, revision: Some(rev_row.id.to_string()), redirect: redirect_id, - editgroup_id: None, extra: rev_row.extra_json, + edit_extra: None, }) } diff --git a/rust/src/api_helpers.rs b/rust/src/api_helpers.rs index ee1f9f14..f260d25f 100644 --- a/rust/src/api_helpers.rs +++ b/rust/src/api_helpers.rs @@ -21,7 +21,7 @@ pub struct EditContext { pub autoaccept: bool, } -#[derive(Clone,Copy,PartialEq)] +#[derive(Clone, Copy, PartialEq)] pub struct ExpandFlags { pub files: bool, pub container: bool, @@ -30,7 +30,6 @@ pub struct ExpandFlags { } impl ExpandFlags { - pub fn from_string(param: &str) -> ExpandFlags { let list: Vec<&str> = param.split_terminator(",").collect(); ExpandFlags::from_strings(&list) @@ -72,24 +71,34 @@ fn test_expand_flags() { assert!(ExpandFlags::from_strings(&vec![]).files == false); assert!(ExpandFlags::from_strings(&vec!["files"]).files == true); assert!(ExpandFlags::from_strings(&vec!["file"]).files == false); - let all = ExpandFlags::from_strings(&vec!["files", "container", "other_thing", "releases", "creators"]); - assert!(all == ExpandFlags { - files: true, - container: true, - releases: true, - creators: true - }); + let all = ExpandFlags::from_strings(&vec![ + "files", + "container", + "other_thing", + "releases", + "creators", + ]); + assert!( + all == ExpandFlags { + files: true, + container: true, + releases: true, + creators: true + } + ); assert!(ExpandFlags::from_string("").files == false); assert!(ExpandFlags::from_string("files").files == true); assert!(ExpandFlags::from_string("something,,files").files == true); assert!(ExpandFlags::from_string("file").files == false); let all = ExpandFlags::from_string("files,container,other_thing,releases,creators"); - assert!(all == ExpandFlags { - files: true, - container: true, - releases: true, - creators: true - }); + assert!( + all == ExpandFlags { + files: true, + container: true, + releases: true, + creators: true + } + ); assert!(all == ExpandFlags::all()); } @@ -169,7 +178,7 @@ pub fn accept_editgroup(editgroup_id: FatCatId, conn: &DbConn) -> Result<Changel Ok(entry) } -#[derive(Clone,Copy,PartialEq)] +#[derive(Clone, Copy, PartialEq)] pub struct FatCatId(Uuid); impl ToString for FatCatId { diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index f25e6169..055758e0 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -19,15 +19,11 @@ macro_rules! entity_batch_handler { &self, entity_list: &[models::$model], autoaccept: bool, - editgroup: Option<String>, + editgroup_id: Option<FatCatId>, conn: &DbConn, ) -> Result<Vec<EntityEdit>> { - let editgroup_id: Option<FatCatId> = match editgroup { - Some(s) => Some(FatCatId::from_str(&s)?), - None => None, - }; - let edit_context = make_edit_context(conn, editgroup_id.clone(), autoaccept)?; + let edit_context = make_edit_context(conn, editgroup_id, autoaccept)?; let model_list: Vec<&models::$model> = entity_list.iter().map(|e| e).collect(); let edits = $model::db_create_batch(conn, &edit_context, model_list.as_slice())?; @@ -57,10 +53,7 @@ pub struct Server { pub db_pool: ConnectionPool, } -pub fn get_release_files ( - id: FatCatId, - conn: &DbConn, -) -> Result<Vec<FileEntity>> { +pub fn get_release_files(id: FatCatId, conn: &DbConn) -> Result<Vec<FileEntity>> { let rows: Vec<(FileRevRow, FileIdentRow, FileReleaseRow)> = file_rev::table .inner_join(file_ident::table) .inner_join(file_release::table) @@ -75,7 +68,6 @@ pub fn get_release_files ( } impl Server { - pub fn lookup_container_handler(&self, issnl: &str, conn: &DbConn) -> Result<ContainerEntity> { check_issn(issnl)?; let (ident, rev): (ContainerIdentRow, ContainerRevRow) = container_ident::table diff --git a/rust/src/api_wrappers.rs b/rust/src/api_wrappers.rs index ca4081da..1f61cc9b 100644 --- a/rust/src/api_wrappers.rs +++ b/rust/src/api_wrappers.rs @@ -68,11 +68,15 @@ macro_rules! wrap_entity_handlers { fn $post_fn( &self, entity: models::$model, + editgroup: Option<String>, _context: &Context, ) -> Box<Future<Item = $post_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); let ret = match conn.transaction(|| { - let edit_context = make_edit_context(&conn, entity.parse_editgroup_id()?, false)?; + let editgroup_id = if let Some(s) = editgroup { + Some(FatCatId::from_str(&s)?) + } else { None }; + let edit_context = make_edit_context(&conn, editgroup_id, false)?; entity.db_create(&conn, &edit_context)?.into_model() }) { Ok(edit) => @@ -102,7 +106,12 @@ macro_rules! wrap_entity_handlers { _context: &Context, ) -> Box<Future<Item = $post_batch_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); - let ret = match conn.transaction(|| self.$post_batch_handler(entity_list, autoaccept.unwrap_or(false), editgroup, &conn)) { + let ret = match conn.transaction(|| { + let editgroup_id = if let Some(s) = editgroup { + Some(FatCatId::from_str(&s)?) + } else { None }; + self.$post_batch_handler(entity_list, autoaccept.unwrap_or(false), editgroup_id, &conn) + }) { Ok(edit) => $post_batch_resp::CreatedEntities(edit), Err(Error(ErrorKind::Diesel(e), _)) => @@ -126,12 +135,16 @@ macro_rules! wrap_entity_handlers { &self, id: String, entity: models::$model, + editgroup: Option<String>, _context: &Context, ) -> Box<Future<Item = $update_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); let ret = match conn.transaction(|| { let entity_id = FatCatId::from_str(&id)?; - let edit_context = make_edit_context(&conn, entity.parse_editgroup_id()?, false)?; + let editgroup_id = if let Some(s) = editgroup { + Some(FatCatId::from_str(&s)?) + } else { None }; + let edit_context = make_edit_context(&conn, editgroup_id, false)?; entity.db_update(&conn, &edit_context, entity_id)?.into_model() }) { Ok(edit) => |