Skip to content

Commit

Permalink
ran cargo fmt & remove unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
KavikaPalletenne committed Nov 26, 2024
1 parent 9d346e0 commit 6a82a2c
Show file tree
Hide file tree
Showing 20 changed files with 280 additions and 175 deletions.
10 changes: 5 additions & 5 deletions backend/server/src/handler/answer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::models::answer::{Answer, NewAnswer};
use crate::models::app::AppState;
use crate::models::auth::{AnswerOwner, ApplicationOwner, AuthUser};
use crate::models::auth::{AnswerOwner, ApplicationOwner};
use crate::models::error::ChaosError;
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
Expand All @@ -13,14 +13,14 @@ pub struct AnswerHandler;
impl AnswerHandler {
pub async fn create(
State(state): State<AppState>,
Path(path): Path<i64>,
user: AuthUser,
Path(application_id): Path<i64>,
_user: ApplicationOwner,
mut transaction: DBTransaction<'_>,
Json(data): Json<NewAnswer>,
) -> Result<impl IntoResponse, ChaosError> {
// TODO: Check whether the question is contained in the campaign being applied to
let id = Answer::create(
user.user_id,
data.application_id,
application_id,
data.question_id,
data.answer_data,
state.snowflake_generator,
Expand Down
28 changes: 19 additions & 9 deletions backend/server/src/handler/campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ use crate::models;
use crate::models::app::AppState;
use crate::models::application::Application;
use crate::models::application::NewApplication;
use crate::models::auth::{AuthUser, SuperUser};
use crate::models::auth::AuthUser;
use crate::models::auth::CampaignAdmin;
use crate::models::campaign::{Campaign};
use crate::models::campaign::Campaign;
use crate::models::error::ChaosError;
use crate::models::offer::Offer;
use crate::models::role::{Role, RoleUpdate};
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use crate::models::offer::Offer;
use crate::models::organisation::{Organisation, SlugCheck};

pub struct CampaignHandler;
impl CampaignHandler {
Expand All @@ -31,7 +30,8 @@ impl CampaignHandler {
Path((organisation_slug, campaign_slug)): Path<(String, String)>,
_user: AuthUser,
) -> Result<impl IntoResponse, ChaosError> {
let campaign = Campaign::get_by_slugs(organisation_slug, campaign_slug, &mut transaction.tx).await?;
let campaign =
Campaign::get_by_slugs(organisation_slug, campaign_slug, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(campaign)))
}
Expand Down Expand Up @@ -62,7 +62,8 @@ impl CampaignHandler {
Path(id): Path<i64>,
_admin: CampaignAdmin,
) -> Result<impl IntoResponse, ChaosError> {
let banner_url = Campaign::update_banner(id, &mut transaction.tx, &state.storage_bucket).await?;
let banner_url =
Campaign::update_banner(id, &mut transaction.tx, &state.storage_bucket).await?;
transaction.tx.commit().await?;
Ok((StatusCode::OK, Json(banner_url)))
}
Expand Down Expand Up @@ -133,9 +134,18 @@ impl CampaignHandler {
State(state): State<AppState>,
_admin: CampaignAdmin,
mut transaction: DBTransaction<'_>,
Json(data): Json<Offer>
Json(data): Json<Offer>,
) -> Result<impl IntoResponse, ChaosError> {
let _ = Offer::create(id, data.application_id, data.email_template_id, data.role_id, data.expiry, &mut transaction.tx, state.snowflake_generator).await?;
let _ = Offer::create(
id,
data.application_id,
data.email_template_id,
data.role_id,
data.expiry,
&mut transaction.tx,
state.snowflake_generator,
)
.await?;
transaction.tx.commit().await?;

Ok((StatusCode::OK, "Successfully created offer"))
Expand All @@ -144,7 +154,7 @@ impl CampaignHandler {
pub async fn get_offers(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_user: CampaignAdmin
_user: CampaignAdmin,
) -> Result<impl IntoResponse, ChaosError> {
let offers = Offer::get_by_campaign(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Expand Down
10 changes: 5 additions & 5 deletions backend/server/src/handler/email_template.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use axum::extract::{Path, State, Json};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use crate::models::app::AppState;
use crate::models::auth::EmailTemplateAdmin;
use crate::models::email_template::EmailTemplate;
use crate::models::error::ChaosError;
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;

pub struct EmailTemplateHandler;
impl EmailTemplateHandler {
Expand All @@ -23,7 +23,7 @@ impl EmailTemplateHandler {
_user: EmailTemplateAdmin,
Path(id): Path<i64>,
State(state): State<AppState>,
Json(request_body): Json<EmailTemplate>
Json(request_body): Json<EmailTemplate>,
) -> Result<impl IntoResponse, ChaosError> {
EmailTemplate::update(id, request_body.name, request_body.template, &state.db).await?;

Expand All @@ -39,4 +39,4 @@ impl EmailTemplateHandler {

Ok((StatusCode::OK, "Successfully delete email template"))
}
}
}
4 changes: 2 additions & 2 deletions backend/server/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ pub mod answer;
pub mod application;
pub mod auth;
pub mod campaign;
pub mod email_template;
pub mod offer;
pub mod organisation;
pub mod question;
pub mod rating;
pub mod role;
pub mod user;
pub mod email_template;
pub mod offer;
13 changes: 6 additions & 7 deletions backend/server/src/handler/offer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use axum::extract::{Path, Json};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use crate::models::auth::{AuthUser, CampaignAdmin, OfferAdmin, OfferRecipient};
use crate::models::auth::{OfferAdmin, OfferRecipient};
use crate::models::error::ChaosError;
use crate::models::offer::{Offer, OfferReply};
use crate::models::transaction::DBTransaction;

use axum::extract::{Json, Path};
use axum::http::StatusCode;
use axum::response::IntoResponse;

pub struct OfferHandler;
impl OfferHandler {
Expand All @@ -23,7 +22,7 @@ impl OfferHandler {
pub async fn delete(
mut transaction: DBTransaction<'_>,
Path(id): Path<i64>,
_user: OfferAdmin
_user: OfferAdmin,
) -> Result<impl IntoResponse, ChaosError> {
Offer::delete(id, &mut transaction.tx).await?;
transaction.tx.commit().await?;
Expand Down Expand Up @@ -64,4 +63,4 @@ impl OfferHandler {

Ok((StatusCode::OK, "Successfully sent offer"))
}
}
}
19 changes: 11 additions & 8 deletions backend/server/src/handler/organisation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ use crate::models;
use crate::models::app::AppState;
use crate::models::auth::SuperUser;
use crate::models::auth::{AuthUser, OrganisationAdmin};
use crate::models::campaign::Campaign;
use crate::models::email_template::EmailTemplate;
use crate::models::error::ChaosError;
use crate::models::organisation::{AdminToRemove, AdminUpdateList, NewOrganisation, Organisation, SlugCheck};
use crate::models::organisation::{
AdminToRemove, AdminUpdateList, NewOrganisation, Organisation, SlugCheck,
};
use crate::models::transaction::DBTransaction;
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use crate::models::campaign::{Campaign};
use crate::models::email_template::EmailTemplate;

pub struct OrganisationHandler;

Expand Down Expand Up @@ -163,7 +165,7 @@ impl OrganisationHandler {

pub async fn create_campaign(
Path(id): Path<i64>,
State(mut state): State<AppState>,
State(state): State<AppState>,
_admin: OrganisationAdmin,
Json(request_body): Json<Campaign>,
) -> Result<impl IntoResponse, ChaosError> {
Expand Down Expand Up @@ -195,25 +197,26 @@ impl OrganisationHandler {

pub async fn create_email_template(
Path(id): Path<i64>,
State(mut state): State<AppState>,
State(state): State<AppState>,
_admin: OrganisationAdmin,
Json(request_body): Json<models::email_template::EmailTemplate>
Json(request_body): Json<models::email_template::EmailTemplate>,
) -> Result<impl IntoResponse, ChaosError> {
Organisation::create_email_template(
id,
request_body.name,
request_body.template,
&state.db,
state.snowflake_generator,
).await?;
)
.await?;

Ok((StatusCode::OK, "Successfully created email template"))
}

pub async fn get_all_email_templates(
_user: OrganisationAdmin,
Path(id): Path<i64>,
State(state): State<AppState>
State(state): State<AppState>,
) -> Result<impl IntoResponse, ChaosError> {
let email_templates = EmailTemplate::get_all_by_organisation(id, &state.db).await?;

Expand Down
3 changes: 1 addition & 2 deletions backend/server/src/handler/rating.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::models::app::AppState;
use crate::models::auth::{
ApplicationCreatorGivenApplicationId, ApplicationReviewerGivenApplicationId,
ApplicationReviewerGivenRatingId, RatingCreator,
ApplicationReviewerGivenApplicationId, ApplicationReviewerGivenRatingId, RatingCreator,
};
use crate::models::error::ChaosError;
use crate::models::rating::{NewRating, Rating};
Expand Down
10 changes: 2 additions & 8 deletions backend/server/src/models/answer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::models::error::ChaosError;
use crate::models::question::{
MultiOptionData, MultiOptionQuestionOption, QuestionData, QuestionType, QuestionTypeParent,
};
use crate::models::question::QuestionType;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use snowflake::SnowflakeIdGenerator;
use sqlx::{Pool, Postgres, Transaction};
use sqlx::{Postgres, Transaction};
use std::ops::DerefMut;

/// The `Answer` type that will be sent in API responses.
Expand Down Expand Up @@ -64,7 +62,6 @@ pub struct AnswerTypeApplicationId {

impl Answer {
pub async fn create(
user_id: i64,
application_id: i64,
question_id: i64,
answer_data: AnswerData,
Expand Down Expand Up @@ -379,9 +376,6 @@ impl AnswerData {
let options = ranking_answers.expect("Data should exist for Ranking variant");
AnswerData::Ranking(options)
}
_ => {
AnswerData::ShortAnswer("".to_string()) // Should never be reached, hence return ShortAnswer
}
};
}

Expand Down
40 changes: 23 additions & 17 deletions backend/server/src/models/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::handler::answer::AnswerHandler;
use crate::handler::application::ApplicationHandler;
use crate::handler::auth::google_callback;
use crate::handler::campaign::CampaignHandler;
use crate::handler::email_template::EmailTemplateHandler;
use crate::handler::offer::OfferHandler;
use crate::handler::organisation::OrganisationHandler;
use crate::handler::question::QuestionHandler;
use crate::handler::rating::RatingHandler;
Expand All @@ -18,9 +20,6 @@ use snowflake::SnowflakeIdGenerator;
use sqlx::postgres::PgPoolOptions;
use sqlx::{Pool, Postgres};
use std::env;
use crate::handler::email_template::EmailTemplateHandler;
use crate::handler::offer::OfferHandler;
use crate::models::organisation::Organisation;

#[derive(Clone)]
pub struct AppState {
Expand Down Expand Up @@ -92,32 +91,37 @@ pub async fn app() -> Result<Router, ChaosError> {
get(ApplicationHandler::get_from_curr_user),
)
.route("/api/v1/organisation", post(OrganisationHandler::create))
.route("/api/v1/organisation/slug_check", post(OrganisationHandler::check_organisation_slug_availability))
.route(
"/api/v1/organisation/slug_check",
post(OrganisationHandler::check_organisation_slug_availability),
)
.route(
"/api/v1/organisation/:organisation_id",
get(OrganisationHandler::get).delete(OrganisationHandler::delete),
)
.route("/api/v1/organisation/slug/:slug",
get(OrganisationHandler::get_by_slug))
.route(
"/api/v1/organisation/slug/:slug",
get(OrganisationHandler::get_by_slug),
)
.route(
"/api/v1/organisation/:organisation_id/campaign",
post(OrganisationHandler::create_campaign),
)
.route(
"/api/v1/organisation/:organisation_id/campaign/slug_check",
post(OrganisationHandler::check_campaign_slug_availability)
post(OrganisationHandler::check_campaign_slug_availability),
)
.route(
"/api/v1/organisation/:organisation_id/campaigns",
get(OrganisationHandler::get_campaigns),
)
.route(
"/api/v1/organisation/:organisation_id/email_template",
post(OrganisationHandler::create_email_template)
post(OrganisationHandler::create_email_template),
)
.route(
"/api/v1/organisation/:organisation_id/email_templates",
get(OrganisationHandler::get_all_email_templates)
get(OrganisationHandler::get_all_email_templates),
)
.route(
"/api/v1/organisation/:organisation_id/logo",
Expand Down Expand Up @@ -183,7 +187,7 @@ pub async fn app() -> Result<Router, ChaosError> {
)
.route(
"/api/v1/campaign/slug/:organisation_slug/:campaign_slug",
get(CampaignHandler::get_by_slugs)
get(CampaignHandler::get_by_slugs),
)
.route("/api/v1/campaign", get(CampaignHandler::get_all))
.route(
Expand All @@ -208,11 +212,11 @@ pub async fn app() -> Result<Router, ChaosError> {
)
.route(
"/api/v1/campaign/:campaign_id/offer",
post(CampaignHandler::create_offer)
post(CampaignHandler::create_offer),
)
.route(
"/api/v1/campaign/:campaign_id/offers",
get(CampaignHandler::get_offers)
get(CampaignHandler::get_offers),
)
.route(
"/api/v1/application/:application_id",
Expand All @@ -231,7 +235,7 @@ pub async fn app() -> Result<Router, ChaosError> {
get(AnswerHandler::get_all_common_by_application),
)
.route(
"/api/v1/application/:applicaiton_id/answer",
"/api/v1/application/:application_id/answer",
post(AnswerHandler::create),
)
.route(
Expand All @@ -246,19 +250,21 @@ pub async fn app() -> Result<Router, ChaosError> {
"/api/v1/email_template/:template_id",
get(EmailTemplateHandler::get)
.patch(EmailTemplateHandler::update)
.delete(EmailTemplateHandler::delete)
.delete(EmailTemplateHandler::delete),
)
.route(
"/api/v1/offer/:offer_id",
get(OfferHandler::get).delete(OfferHandler::delete).post(OfferHandler::reply)
get(OfferHandler::get)
.delete(OfferHandler::delete)
.post(OfferHandler::reply),
)
.route(
"/api/v1/offer/:offer_id/preview",
get(OfferHandler::preview_email)
get(OfferHandler::preview_email),
)
.route(
"/api/v1/offer/:offer_id/send",
post(OfferHandler::send_offer)
post(OfferHandler::send_offer),
)
.with_state(state))
}
Loading

0 comments on commit 6a82a2c

Please sign in to comment.