Skip to content

Commit

Permalink
Refactor to move database actions out of model.rs and into location/c…
Browse files Browse the repository at this point in the history
…rud.rs
  • Loading branch information
brendan-oconnell committed Sep 23, 2024
1 parent 548d623 commit 5b6f4a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
22 changes: 2 additions & 20 deletions thoth-api/src/graphql/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ use crate::model::Orcid;
use crate::model::Ror;
use crate::model::Timestamp;
use crate::model::WeightUnit;
use crate::schema::location;
use diesel::{Connection, ExpressionMethods, QueryDsl, RunQueryDsl};
use thoth_errors::{ThothError, ThothResult};

use super::utils::{Direction, Expression};
Expand Down Expand Up @@ -2013,12 +2011,8 @@ impl MutationRoot {
// change the old canonical location to non-canonical, and change the old non-canonical location to canonical
} else {
let mut old_canonical_location_id: Option<Uuid> = None;
let connection = &mut context.db.get().unwrap();

let canonical_location = location::table
.filter(location::publication_id.eq(data.publication_id))
.filter(location::canonical.eq(true))
.first::<Location>(&mut context.db.get()?);
let canonical_location = data.get_canonical_location(&context.db);

let final_canonical_location = match canonical_location {
Ok(location) => location,
Expand All @@ -2040,19 +2034,7 @@ impl MutationRoot {
}

if let Some(old_canonical_location_id) = old_canonical_location_id {
let _ = connection.transaction(|connection| {
// Update the current canonical location to non-canonical
diesel::update(location::table.find(old_canonical_location_id))
.set(old_canonical_location)
.execute(connection)?;

// Update the current non-canonical location to canonical
diesel::update(location::table.find(data.location_id))
.set(&data)
.execute(connection)?;

Ok::<_, diesel::result::Error>(())
});
let _ = data.update_canonical_location(old_canonical_location, old_canonical_location_id, &context.db);
}
Ok(location)
}
Expand Down
39 changes: 39 additions & 0 deletions thoth-api/src/model/location/crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,45 @@ impl PatchLocation {
db,
)
}

pub fn get_canonical_location(
&self,
db: &crate::db::PgPool
) -> ThothResult<Location> {
let mut connection = db.get()?;

let canonical_location = crate::schema::location::table
.filter(crate::schema::location::publication_id.eq(self.publication_id))
.filter(crate::schema::location::canonical.eq(true))
.first::<Location>(&mut connection)
.expect("Error loading canonical location for publication");
Ok(canonical_location)
}

pub fn update_canonical_location(
&self,
old_canonical_location: Option<PatchLocation>,
old_canonical_location_id: Uuid,
db: &crate::db::PgPool
) -> ThothResult<()> {
use crate::diesel::Connection;
let mut connection = db.get()?;
let _ = connection.transaction(|connection| {
// Update the current canonical location to non-canonical
diesel::update(location::table.find(old_canonical_location_id))
.set(old_canonical_location)
.execute(connection)?;

// Update the current non-canonical location to canonical
diesel::update(location::table.find(&self.location_id))
.set(self)
.execute(connection)?;

Ok::<_, diesel::result::Error>(())
});
Ok(())

}
}

fn location_canonical_record_complete(
Expand Down

0 comments on commit 5b6f4a0

Please sign in to comment.