Properly catch violations of unique constraint
This commit is contained in:
parent
ce551e9c8b
commit
3d537f5987
3 changed files with 20 additions and 37 deletions
|
@ -1,6 +1,7 @@
|
|||
use tokio_postgres::GenericClient;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::catch_unique_violation;
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::models::cleanup::{
|
||||
find_orphaned_files,
|
||||
|
@ -21,7 +22,7 @@ pub async fn create_profile(
|
|||
) -> Result<DbActorProfile, DatabaseError> {
|
||||
let profile_id = Uuid::new_v4();
|
||||
let extra_fields = ExtraFields(profile_data.extra_fields.clone());
|
||||
let result = db_client.query_one(
|
||||
let row = db_client.query_one(
|
||||
"
|
||||
INSERT INTO actor_profile (
|
||||
id, username, display_name, acct, bio, bio_source,
|
||||
|
@ -43,15 +44,8 @@ pub async fn create_profile(
|
|||
&extra_fields,
|
||||
&profile_data.actor,
|
||||
],
|
||||
).await;
|
||||
let profile = match result {
|
||||
Ok(row) => row.try_get("actor_profile")?,
|
||||
Err(err) => {
|
||||
// TODO: catch profile already exists error
|
||||
log::warn!("{}", err);
|
||||
return Err(DatabaseError::AlreadyExists("profile"));
|
||||
},
|
||||
};
|
||||
).await.map_err(catch_unique_violation("profile"))?;
|
||||
let profile = row.try_get("actor_profile")?;
|
||||
Ok(profile)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ use std::convert::TryFrom;
|
|||
use tokio_postgres::GenericClient;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::catch_unique_violation;
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::models::notifications::queries::create_follow_notification;
|
||||
use crate::models::profiles::queries::{
|
||||
|
@ -87,17 +88,13 @@ pub async fn follow(
|
|||
target_id: &Uuid,
|
||||
) -> Result<Relationship, DatabaseError> {
|
||||
let transaction = db_client.transaction().await?;
|
||||
let result = transaction.execute(
|
||||
transaction.execute(
|
||||
"
|
||||
INSERT INTO relationship (source_id, target_id)
|
||||
VALUES ($1, $2)
|
||||
",
|
||||
&[&source_id, &target_id],
|
||||
).await;
|
||||
if let Err(err) = result {
|
||||
log::info!("{}", err);
|
||||
return Err(DatabaseError::AlreadyExists("relationship"));
|
||||
};
|
||||
).await.map_err(catch_unique_violation("relationship"))?;
|
||||
let target_profile = update_follower_count(&transaction, target_id, 1).await?;
|
||||
update_following_count(&transaction, source_id, 1).await?;
|
||||
if target_profile.is_local() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use tokio_postgres::GenericClient;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::catch_unique_violation;
|
||||
use crate::errors::DatabaseError;
|
||||
use crate::models::profiles::queries::create_profile;
|
||||
use crate::models::profiles::types::{DbActorProfile, ProfileCreateData};
|
||||
|
@ -150,7 +151,7 @@ pub async fn create_user(
|
|||
};
|
||||
let profile = create_profile(&transaction, &profile_data).await?;
|
||||
// Create user
|
||||
let result = transaction.query_one(
|
||||
let row = transaction.query_one(
|
||||
"
|
||||
INSERT INTO user_account (
|
||||
id, wallet_address, password_hash, private_key, invite_code
|
||||
|
@ -165,26 +166,17 @@ pub async fn create_user(
|
|||
&private_key_pem,
|
||||
&user_data.invite_code,
|
||||
],
|
||||
).await;
|
||||
match result {
|
||||
Ok(row) => {
|
||||
transaction.commit().await?;
|
||||
let db_user: DbUser = row.try_get("user_account")?;
|
||||
let user = User {
|
||||
id: db_user.id,
|
||||
wallet_address: db_user.wallet_address,
|
||||
password_hash: db_user.password_hash,
|
||||
private_key: db_user.private_key,
|
||||
profile,
|
||||
};
|
||||
Ok(user)
|
||||
},
|
||||
Err(err) => {
|
||||
// TODO: catch user already exists error
|
||||
log::info!("{}", err);
|
||||
Err(DatabaseError::AlreadyExists("user"))?
|
||||
},
|
||||
}
|
||||
).await.map_err(catch_unique_violation("user"))?;
|
||||
let db_user: DbUser = row.try_get("user_account")?;
|
||||
let user = User {
|
||||
id: db_user.id,
|
||||
wallet_address: db_user.wallet_address,
|
||||
password_hash: db_user.password_hash,
|
||||
private_key: db_user.private_key,
|
||||
profile,
|
||||
};
|
||||
transaction.commit().await?;
|
||||
Ok(user)
|
||||
}
|
||||
|
||||
pub async fn get_user_by_wallet_address(
|
||||
|
|
Loading…
Reference in a new issue