Skip to content

Commit

Permalink
Answer CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
KavikaPalletenne committed Nov 12, 2024
1 parent 1dd865e commit 6293a54
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 15 deletions.
89 changes: 89 additions & 0 deletions backend/server/src/handler/answer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use serde_json::json;
use crate::models::answer::{Answer, NewAnswer};
use crate::models::app::AppState;
use crate::models::auth::{AnswerOwner, ApplicationOwner, AuthUser};
use crate::models::error::ChaosError;
use crate::models::transaction::DBTransaction;

pub struct AnswerHandler;

impl AnswerHandler {
pub async fn create(
State(state): State<AppState>,
Path(path): Path<i64>,
user: AuthUser,
mut transaction: DBTransaction<'_>,
Json(data): Json<NewAnswer>,
) -> Result<impl IntoResponse, ChaosError> {
let id = Answer::create(
user.user_id,
data.application_id,
data.question_id,
data.answer_data,
state.snowflake_generator,
&mut transaction.tx
).await?;

transaction.tx.commit().await?;

Ok((StatusCode::OK, Json(json!({"id": id}))))
}

pub async fn get_all_common_by_application(
Path(application_id): Path<i64>,
_owner: ApplicationOwner,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let answers = Answer::get_all_common_by_application(application_id, &mut transaction.tx)
.await?;

transaction.tx.commit().await?;

Ok((StatusCode::OK, Json(answers)))
}

pub async fn get_all_by_application_and_role(
Path((application_id, role_id)): Path<(i64, i64)>,
_owner: ApplicationOwner,
mut transaction: DBTransaction<'_>,
) -> Result<impl IntoResponse, ChaosError> {
let answers = Answer::get_all_by_application_and_role(application_id, role_id, &mut transaction.tx)
.await?;

transaction.tx.commit().await?;

Ok((StatusCode::OK, Json(answers)))
}

pub async fn update(
Path(answer_id): Path<i64>,
_owner: AnswerOwner,
mut transaction: DBTransaction<'_>,
Json(data): Json<NewAnswer>,
) -> Result<impl IntoResponse, ChaosError> {
Answer::update(
answer_id,
data.answer_data,
&mut transaction.tx
).await?;

transaction.tx.commit().await?;

Ok((StatusCode::OK, "Successfully updated answer"))
}

pub async fn delete(
Path(answer_id): Path<i64>,
_owner: AnswerOwner,
mut transaction: DBTransaction<'_>
) -> Result<impl IntoResponse, ChaosError> {
Answer::delete(answer_id, &mut transaction.tx).await?;

transaction.tx.commit().await?;

Ok((StatusCode::OK, "Successfully deleted answer"))
}
}
1 change: 1 addition & 0 deletions backend/server/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod rating;
pub mod role;
pub mod application;
pub mod question;
pub mod answer;
15 changes: 10 additions & 5 deletions backend/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use handler::user::UserHandler;
use crate::handler::campaign::CampaignHandler;
use crate::handler::organisation::OrganisationHandler;
use crate::handler::application::ApplicationHandler;
use crate::handler::answer::AnswerHandler;
use crate::models::storage::Storage;
use anyhow::Result;
use axum::routing::{get, patch, post, put};
Expand Down Expand Up @@ -68,7 +69,7 @@ async fn main() -> Result<()> {
};

let app = Router::new()
.route("/", get(|| async { "Hello, World!" }))
.route("/", get(|| async { "Join DevSoc! https://devsoc.app/" }))
.route("/api/auth/callback/google", get(google_callback))
.route("/api/v1/user", get(UserHandler::get))
.route("/api/v1/user/name", patch(UserHandler::update_name))
Expand Down Expand Up @@ -154,7 +155,7 @@ async fn main() -> Result<()> {
)
.route("/api/v1/campaign", get(CampaignHandler::get_all))
.route("/api/v1/campaign/:campaign_id/question", post(QuestionHandler::create))
.route("/api/v1/campaign/:campaign_id/question/:id", put(QuestionHandler::update).delete(QuestionHandler::delete))
.route("/api/v1/campaign/:campaign_id/question/:id", patch(QuestionHandler::update).delete(QuestionHandler::delete))
.route("/api/v1/campaign/:campaign_id/questions/common", get(QuestionHandler::get_all_common_by_campaign))
.route(
"/api/v1/campaign/:campaign_id/banner",
Expand All @@ -163,9 +164,13 @@ async fn main() -> Result<()> {
.route("api/v1/campaign/:campaign_id/application",
post(CampaignHandler::create_application)
)
.route("api/v1/application/:application_id", get(ApplicationHandler::get))
.route("api/v1/application/:application_id/status", patch(ApplicationHandler::set_status))
.route("api/v1/application/:application_id/private", patch(ApplicationHandler::set_private_status))
.route("/api/v1/application/:application_id", get(ApplicationHandler::get))
.route("/api/v1/application/:application_id/status", patch(ApplicationHandler::set_status))
.route("/api/v1/application/:application_id/private", patch(ApplicationHandler::set_private_status))
.route("/api/v1/application/:application_id/answers/common", get(AnswerHandler::get_all_common_by_application))
.route("/api/v1/application/:applicaiton_id/answer", post(AnswerHandler::create))
.route("/api/v1/application/:application_id/answers/role/:role_id", get(AnswerHandler::get_all_by_application_and_role))
.route("/api/v1/answer/:answer_id", patch(AnswerHandler::update).delete(AnswerHandler::delete))
.with_state(state);

let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
Expand Down
Loading

0 comments on commit 6293a54

Please sign in to comment.