diff --git a/Cargo.lock b/Cargo.lock index 76ed9fb0..1e58d803 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2330,9 +2330,9 @@ dependencies = [ [[package]] name = "pq-sys" -version = "0.6.1" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a24ff9e4cf6945c988f0db7005d87747bf72864965c3529d259ad155ac41d584" +checksum = "31c0052426df997c0cbd30789eb44ca097e3541717a7b8fa36b1c464ee7edebd" dependencies = [ "vcpkg", ] diff --git a/thoth-api/src/graphql/model.rs b/thoth-api/src/graphql/model.rs index 94443be8..15921df1 100644 --- a/thoth-api/src/graphql/model.rs +++ b/thoth-api/src/graphql/model.rs @@ -1973,35 +1973,6 @@ impl MutationRoot { #[graphql(description = "Values to apply to existing location")] data: PatchLocation, ) -> FieldResult { context.token.jwt.as_ref().ok_or(ThothError::Unauthorised)?; - let current_location = Location::from_id(&context.db, &data.location_id).unwrap(); - - context - .account_access - .can_edit(current_location.publisher_id(&context.db)?)?; - - if data.publication_id != current_location.publication_id { - context - .account_access - .can_edit(publisher_id_from_publication_id( - &context.db, - data.publication_id, - )?)?; - } - - // check has been commented out to allow for changing canonical location. If this check - // is left in place, canonical location can't be updated because check is violated during - // the mutation when there are (temporarily) two canonical locations before the mutation - // is completed. Equivalent check is maintained in the DB, so hopefully this can - // be left out and the check will still be completed at the DB level, preventing more - // than one canonical location. - // if data.canonical != current_location.canonical { - // // This looks at any time you want to change canonical location. - // // Each publication must have exactly one canonical location. - // // Updating an existing location would always violate this, - // // as it should always result in either zero or two canonical locations. - // return Err(ThothError::CanonicalLocationError.into()); - // } - let all_location_platforms = Some(vec![ LocationPlatform::ProjectMuse, @@ -2023,33 +1994,59 @@ impl MutationRoot { LocationPlatform::Other, ]); - // get current publication - let publication = Publication::from_id(&context.db, &data.publication_id).unwrap(); - // get all locations from current publication - let locations = Publication::locations(&publication, context, Some(100), Some(0), Some(LocationOrderBy::default()), all_location_platforms); - // println!("publication contains: {:?}", publication); - // println!("locations are: {:?}", locations); - println!("PatchLocation data is: {:?}", &data); - // example of PatchLocation data: - // PatchLocation { location_id: b2e14c78-5fbe-4526-8206-f28dd335a981, - // publication_id: 5ece37a1-3aae-4e9c-8580-5c1d38375f4a, - // landing_page: Some("https://thoth-arch.lib.cam.ac.uk/handle/1811/86ggggggaaa444555444333121121"), - // full_text_url: Some("https://fulltext30.com"), - // location_platform: Zenodo, canonical: false } + let location = Location::from_id(&context.db, &data.location_id).unwrap(); + let mut canonical_location: Option = None; - // for location in locations { - // println!("Individual Location is: {:?}", location); - // } + context + .account_access + .can_edit(location.publisher_id(&context.db)?)?; + if data.publication_id != location.publication_id { + context + .account_access + .can_edit(publisher_id_from_publication_id( + &context.db, + data.publication_id, + )?)?; + } - // example of publication variable: - // Publication { publication_id: 5ece37a1-3aae-4e9c-8580-5c1d38375f4a, - // publication_type: Pdf, work_id: 5fb9a23a-bf8e-4527-9c9c-3786b7219285, - // isbn: Some(Isbn("978-1-80064-730-5")), created_at: Timestamp(2022-06-13T08:24:51.502655Z), - // updated_at: Timestamp(2022-06-13T08:24:51.502655Z), width_mm: None, - // width_in: None, height_mm: None, height_in: None, depth_mm: None, depth_in: None, weight_g: None, weight_oz: None } + if data.canonical != location.canonical { + return Err(ThothError::CanonicalLocationError.into()); + } + let publication = Publication::from_id(&context.db, &data.publication_id).unwrap(); + + // get all locations from current publication + let locations = Publication::locations( + &publication, + context, + Some(100), + Some(0), + Some(LocationOrderBy::default()), + all_location_platforms, + ); + + if let Ok(locations) = locations { + for location in locations { + if location.canonical { + canonical_location = Some(PatchLocation { + location_id: location.location_id, + publication_id: location.publication_id, + landing_page: location.landing_page.clone(), + full_text_url: location.full_text_url.clone(), + location_platform: location.location_platform.clone(), + canonical: location.canonical, + }); + break; + } + } + } + if let Some(canonical_location) = canonical_location { + println!("Canonical PatchLocation: {:?}", canonical_location); + } else { + println!("No canonical location found."); + } if data.canonical { data.canonical_record_complete(&context.db)?; @@ -2058,25 +2055,25 @@ impl MutationRoot { // let account_id = context.token.jwt.as_ref().unwrap().account_id(&context.db); let connection = &mut context.db.get().unwrap(); - let zenodo_location_id = Uuid::parse_str("b2e14c78-5fbe-4526-8206-f28dd335a981").unwrap(); - let zenodo_patch_location = PatchLocation { - location_id: Uuid::parse_str("b2e14c78-5fbe-4526-8206-f28dd335a981").unwrap(), - publication_id: Uuid::parse_str("5ece37a1-3aae-4e9c-8580-5c1d38375f4a").unwrap(), - landing_page: Some("https://thoth-arch.lib.cam.ac.uk/handle/1811/hardcoded_zenodo".to_string()), - full_text_url: Some("https://fulltext30zenodo.com".to_string()), - location_platform: LocationPlatform::Zenodo, - canonical: true, - }; - - let scienceopen_location_id = Uuid::parse_str("8b5791e5-15c2-42aa-8211-65041d8cf2e5").unwrap(); - let scienceopen_patch_location = PatchLocation { - location_id: Uuid::parse_str("8b5791e5-15c2-42aa-8211-65041d8cf2e5").unwrap(), - publication_id: Uuid::parse_str("5ece37a1-3aae-4e9c-8580-5c1d38375f4a").unwrap(), - landing_page: Some("https://thoth-arch.lib.cam.ac.uk/handle/1811/hardcoded_scienceopen".to_string()), - full_text_url: Some("https://fulltext30scienceopen.com".to_string()), - location_platform: LocationPlatform::ScienceOpen, - canonical: false, - }; + // let zenodo_location_id = Uuid::parse_str("b2e14c78-5fbe-4526-8206-f28dd335a981").unwrap(); + // let zenodo_patch_location = PatchLocation { + // location_id: Uuid::parse_str("b2e14c78-5fbe-4526-8206-f28dd335a981").unwrap(), + // publication_id: Uuid::parse_str("5ece37a1-3aae-4e9c-8580-5c1d38375f4a").unwrap(), + // landing_page: Some("https://thoth-arch.lib.cam.ac.uk/handle/1811/hardcoded_zenodo".to_string()), + // full_text_url: Some("https://fulltext30zenodo.com".to_string()), + // location_platform: LocationPlatform::Zenodo, + // canonical: false, + // }; + + // let scienceopen_location_id = Uuid::parse_str("8b5791e5-15c2-42aa-8211-65041d8cf2e5").unwrap(); + // let scienceopen_patch_location = PatchLocation { + // location_id: Uuid::parse_str("8b5791e5-15c2-42aa-8211-65041d8cf2e5").unwrap(), + // publication_id: Uuid::parse_str("5ece37a1-3aae-4e9c-8580-5c1d38375f4a").unwrap(), + // landing_page: Some("https://thoth-arch.lib.cam.ac.uk/handle/1811/hardcoded_scienceopen".to_string()), + // full_text_url: Some("https://fulltext30scienceopen.com".to_string()), + // location_platform: LocationPlatform::ScienceOpen, + // canonical: true, + // }; // if in transaction you try to set the new canonical to true first, you have two canonical locations at the same time, // and get @@ -2089,13 +2086,13 @@ impl MutationRoot { // if not canonical location and want to make canonical, update existing canonical // to set to false, set new to true connection.transaction(|connection| { - // diesel::update(location::table.find(data.location_id)) - diesel::update(location::table.find(scienceopen_location_id)) - .set(scienceopen_patch_location) - // .set(&data) - .get_result::(connection); - diesel::update(location::table.find(zenodo_location_id)) - .set(zenodo_patch_location) + diesel::update(location::table.find(data.location_id)) + // diesel::update(location::table.find(zenodo_location_id)) + // .set(zenodo_patch_location) + .set(&data) + // .get_result::(connection); + // diesel::update(location::table.find(scienceopen_location_id)) + // .set(scienceopen_patch_location) // .set(&data) .get_result::(connection) .map_err(|e| e.into()) diff --git a/thoth-app/src/models/location/location_query.rs b/thoth-app/src/models/location/location_query.rs index 1b4c1a8d..0abc97cd 100644 --- a/thoth-app/src/models/location/location_query.rs +++ b/thoth-app/src/models/location/location_query.rs @@ -3,16 +3,6 @@ use serde::Serialize; use thoth_api::model::location::Location; use uuid::Uuid; -// locationId: Uuid! -// publicationId: Uuid! -// landingPage: String -// fullTextUrl: String -// locationPlatform: LocationPlatform! -// canonical: Boolean! -// createdAt: Timestamp! -// updatedAt: Timestamp! -// publication: Publication! - pub const LOCATION_QUERY: &str = " query LocationQuery($locationId: Uuid!) { location(locationId: $locationId) {