Skip to content

Commit

Permalink
feat: create/delete permit project inside transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaro00 committed Mar 27, 2024
1 parent 86d86c0 commit 0009469
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
13 changes: 0 additions & 13 deletions gateway/src/api/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,6 @@ async fn create_project(
.await?;
let idle_minutes = project.state.idle_minutes();

service
.permit_client
.create_project(&id, &project.project_id)
.await
.map_err(|_| Error::from(ErrorKind::Internal))?;

service
.new_task()
.project(project_name.clone())
Expand Down Expand Up @@ -386,13 +380,6 @@ async fn delete_project(
.await?;
task.await;

state
.service
.permit_client
.delete_project(&project.project_id)
.await
.map_err(|_| Error::from(ErrorKind::Internal))?;

service.delete_project(&project_name).await?;

Ok(AxumJson("project successfully deleted".to_owned()))
Expand Down
17 changes: 15 additions & 2 deletions gateway/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,15 @@ impl GatewayService {
),
));

let mut transaction = self.db.begin().await?;
query("INSERT INTO projects (project_id, project_name, account_name, user_id, initial_key, project_state) VALUES (?1, ?2, ?3, ?4, ?5, ?6)")
.bind(&project_id.to_string())
.bind(&project_name)
.bind("")
.bind(user_id)
.bind(project.initial_key().unwrap())
.bind(&project)
.execute(&self.db)
.execute(&mut *transaction)
.await
.map_err(|err| {
// If the error is a broken PK constraint, this is a
Expand All @@ -644,6 +645,13 @@ impl GatewayService {
err.into()
})?;

self.permit_client
.create_project(&user_id, &project_id.to_string())
.await
.map_err(|_| Error::from(ErrorKind::Internal))?;

transaction.commit().await?;

let project = project.0;

Ok(FindProjectPayload {
Expand All @@ -662,7 +670,7 @@ impl GatewayService {
let mut transaction = self.db.begin().await?;

query("DELETE FROM custom_domains WHERE project_id = ?1")
.bind(project_id)
.bind(&project_id)
.execute(&mut *transaction)
.await?;

Expand All @@ -671,6 +679,11 @@ impl GatewayService {
.execute(&mut *transaction)
.await?;

self.permit_client
.delete_project(&project_id)
.await
.map_err(|_| Error::from(ErrorKind::Internal))?;

transaction.commit().await?;

Ok(())
Expand Down

0 comments on commit 0009469

Please sign in to comment.