Skip to content

Commit

Permalink
feat: added model file for application; added create method. fix: cha…
Browse files Browse the repository at this point in the history
…nged schema for application_role, fixed id type for role.
  • Loading branch information
Alex_Miao_WSL committed Jul 21, 2024
1 parent ba6fb2b commit 900a911
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
4 changes: 2 additions & 2 deletions backend/migrations/20240406031915_create_applications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ CREATE TABLE applications (

CREATE TABLE application_roles (
id BIGSERIAL PRIMARY KEY,
application_id INTEGER NOT NULL,
campaign_role_id INTEGER NOT NULL,
application_id BIGINT NOT NULL,
campaign_role_id BIGINT NOT NULL,
CONSTRAINT FK_application_roles_applications
FOREIGN KEY(application_id)
REFERENCES applications(id)
Expand Down
87 changes: 87 additions & 0 deletions backend/server/src/models/application.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::models::error::ChaosError;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use snowflake::SnowflakeIdGenerator;
use sqlx::{FromRow, Pool, Postgres, Transaction};
use std::ops::DerefMut;

#[derive(Deserialize, Serialize, Clone, FromRow, Debug)]
pub struct Application {
pub id: i64,
pub campaign_id: i64,
pub user_id: i64,
pub status: ApplicationStatus,
pub private_status: ApplicationStatus,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}

/*
User could apply for more than one roles at a time, for each application
into a role it will be represented by row in application_roles table which
is linked to the main Application body through application_id
*/
#[derive(Deserialize, Serialize, Clone, FromRow, Debug)]
pub struct ApplicationRole {
pub id: i64,
pub application_id: i64,
pub campaign_role_id: i64,
}

pub struct NewApplication {
pub user_id: i64,
pub status: ApplicationStatus,
pub private_status: ApplicationStatus,
pub applied_roles: Vec<ApplicationRole>,
}


#[derive(Deserialize, Serialize, sqlx::Type, Clone, Debug)]
#[sqlx(type_name = "application_status", rename_all = "PascalCase")]
pub enum ApplicationStatus {
Pending,
Rejected,
Successful,
}

impl Application {
pub async fn create(
campaign_id: i64,
application_data: NewApplication,
mut snowflake_generator: SnowflakeIdGenerator,
transaction: &mut Transaction<'_, Postgres>,
) -> Result<(), ChaosError> {
let id = snowflake_generator.generate();

// Insert into table applications
sqlx::query!(
"
INSERT INTO applications (id, campaign_id, user_id, status, private_status)
VALUES ($1, $2, $3, $4, $5)
",
id,
campaign_id,
application_data.user_id,
application_data.status as ApplicationStatus,
application_data.private_status as ApplicationStatus,
)
.execute(transaction.deref_mut())
.await?;

// Insert into table application_roles
for role_applied in application_data.applied_roles {
sqlx::query!(
"
INSERT INTO application_roles (application_id, campaign_role_id)
VALUES ($1, $2)
",
id,
role_applied.campaign_role_id
)
.execute(transaction.deref_mut())
.await?;
}

Ok(())
}
}
1 change: 1 addition & 0 deletions backend/server/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod role;
pub mod storage;
pub mod transaction;
pub mod user;
pub mod application;
2 changes: 1 addition & 1 deletion backend/server/src/models/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sqlx::{FromRow, Pool, Postgres};

#[derive(Deserialize, Serialize, Clone, FromRow, Debug)]
pub struct Role {
pub id: i32,
pub id: i64,
pub campaign_id: i64,
pub name: Option<String>,
pub description: String,
Expand Down

0 comments on commit 900a911

Please sign in to comment.