Skip to content

Commit

Permalink
For all PATCH routes, in respective model methods, return id in sql q…
Browse files Browse the repository at this point in the history
…uery to ensure id is valid
  • Loading branch information
Alex_Miao_WSL committed Aug 8, 2024
1 parent 15aa828 commit 711f086
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
14 changes: 8 additions & 6 deletions backend/server/src/models/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,32 +355,34 @@ impl Application {
}

pub async fn set_status(id: i64, new_status: ApplicationStatus, pool: &Pool<Postgres>) -> Result<(), ChaosError> {
sqlx::query!(
let _ = sqlx::query!(
"
UPDATE applications
SET status = $2
WHERE id = $1;
WHERE id = $1
RETURNING id;
",
id,
new_status as ApplicationStatus
)
.execute(pool)
.fetch_one(pool)
.await?;

Ok(())
}

pub async fn set_private_status(id: i64, new_status: ApplicationStatus, pool: &Pool<Postgres>) -> Result<(), ChaosError> {
sqlx::query!(
let _ = sqlx::query!(
"
UPDATE applications
SET private_status = $2
WHERE id = $1;
WHERE id = $1
RETURNING id;
",
id,
new_status as ApplicationStatus
)
.execute(pool)
.fetch_one(pool)
.await?;

Ok(())
Expand Down
10 changes: 6 additions & 4 deletions backend/server/src/models/campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,20 @@ impl Campaign {
update: CampaignUpdate,
pool: &Pool<Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
let _ = sqlx::query!(
"
UPDATE campaigns
SET name = $1, description = $2, starts_at = $3, ends_at = $4
WHERE id = $5
RETURNING id;
",
update.name,
update.description,
update.starts_at,
update.ends_at,
id
)
.execute(pool)
.fetch_one(pool)
.await?;

Ok(())
Expand All @@ -125,17 +126,18 @@ impl Campaign {
let image_id = Uuid::new_v4();
let current_time = dt;

sqlx::query!(
let _ = sqlx::query!(
"
UPDATE campaigns
SET cover_image = $1, updated_at = $2
WHERE id = $3
RETURNING id;
",
image_id,
current_time,
id
)
.execute(pool)
.fetch_one(pool)
.await?;

let upload_url =
Expand Down
37 changes: 23 additions & 14 deletions backend/server/src/models/organisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ impl Organisation {
admin_id_list: Vec<i64>,
transaction: &mut Transaction<'_, Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
"DELETE FROM organisation_members WHERE organisation_id = $1 AND role = $2",
let _ = sqlx::query!(
"DELETE FROM organisation_members
WHERE organisation_id = $1 AND role = $2
RETURNING organisation_id;",
organisation_id,
OrganisationRole::Admin as OrganisationRole
)
.execute(transaction.deref_mut())
.fetch_one(transaction.deref_mut())
.await?;

for admin_id in admin_id_list {
Expand All @@ -205,12 +207,14 @@ impl Organisation {
member_id_list: Vec<i64>,
transaction: &mut Transaction<'_, Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
"DELETE FROM organisation_members WHERE organisation_id = $1 AND role = $2",
let _ = sqlx::query!(
"DELETE FROM organisation_members
WHERE organisation_id = $1 AND role = $2
RETURNING organisation_id;",
organisation_id,
OrganisationRole::User as OrganisationRole
)
.execute(transaction.deref_mut())
.fetch_one(transaction.deref_mut())
.await?;

for member_id in member_id_list {
Expand All @@ -235,15 +239,17 @@ impl Organisation {
admin_to_remove: i64,
pool: &Pool<Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
let _ = sqlx::query!(
"
UPDATE organisation_members SET role = $3 WHERE user_id = $1 AND organisation_id = $2
UPDATE organisation_members SET role = $3
WHERE user_id = $1 AND organisation_id = $2
RETURNING (user_id, organisation_id)
",
admin_to_remove,
organisation_id,
OrganisationRole::User as OrganisationRole
)
.execute(pool)
.fetch_one(pool)
.await?;

Ok(())
Expand All @@ -254,14 +260,16 @@ impl Organisation {
user_id: i64,
pool: &Pool<Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
let _ = sqlx::query!(
"
DELETE FROM organisation_members WHERE user_id = $1 AND organisation_id = $2
DELETE FROM organisation_members
WHERE user_id = $1 AND organisation_id = $2
RETURNING (user_id, organisation_id)
",
user_id,
organisation_id
)
.execute(pool)
.fetch_one(pool)
.await?;

Ok(())
Expand All @@ -276,17 +284,18 @@ impl Organisation {

let logo_id = Uuid::new_v4();
let current_time = dt;
sqlx::query!(
let _ = sqlx::query!(
"
UPDATE organisations
SET logo = $2, updated_at = $3
WHERE id = $1
RETURNING id;
",
id,
logo_id,
current_time
)
.execute(pool)
.fetch_one(pool)
.await?;

let upload_url =
Expand Down
7 changes: 4 additions & 3 deletions backend/server/src/models/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ impl Role {
role_data: RoleUpdate,
pool: &Pool<Postgres>,
) -> Result<(), ChaosError> {
sqlx::query!(
let _ = sqlx::query!(
"
UPDATE campaign_roles
SET (name, description, min_available, max_available, finalised) = ($2, $3, $4, $5, $6)
WHERE id = $1;
WHERE id = $1
RETURNING id;
",
id,
role_data.name,
Expand All @@ -112,7 +113,7 @@ impl Role {
role_data.max_avaliable,
role_data.finalised
)
.execute(pool)
.fetch_one(pool)
.await?;

Ok(())
Expand Down

0 comments on commit 711f086

Please sign in to comment.