Skip to content

Commit

Permalink
Campaign Role CRUD (#497)
Browse files Browse the repository at this point in the history
* feat: added Role struct in model, implmented get and post for Role, updated api ymal for Role, added migration to change Role table in db

* feat:added methods for delte and update role;added handler for role and added some routes into main regarding role

* feat: added more api yaml documentation, added handler for create and get all roles in a campaign, added get all in roles in a campagin in model

* fix: changed get_roles to Campaign Handler, changede migration file

* change all id columns to `BIGINT` or `BIGSERIAL`

* create `RoleAdmin` struct

* move `create_role()` to `CampaignHandler`

* cleanup

* ran cargo fmt

---------

Co-authored-by: Alex_Miao_WSL <[email protected]>
Co-authored-by: Kavika <[email protected]>
  • Loading branch information
3 people authored Jul 20, 2024
1 parent ff4cff4 commit ba6fb2b
Show file tree
Hide file tree
Showing 14 changed files with 539 additions and 11 deletions.
235 changes: 235 additions & 0 deletions backend/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,241 @@ paths:
example: Not logged in.
'403':
description: User is not an organisation admin.
content:
application/json:
schema:
properties:
error:
type: string
example: Unauthorized

/campaign/{id}/role:
post:
operationId: createRole
parameters:
- name: id
in: path
description: Campaign ID
required: true
schema:
type: integer
format: int64
description: Creates a new role in a campaign.
tags:
- Campaign
requestBody:
required: true
content:
application/json:
schema:
properties:
name:
type: string
example: Chief Mouser
description:
type: string
required: False
example: Larry the cat is dead, now we need someone else to handle the rat issues at 10th Downing st.
min_available:
type: int32
example: 1
max_available:
type: int32
example: 3
finalised:
type: boolean
description: Whether this role has been finalised (e.g. max avaliable number)
example: False
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
message:
type: string
example: Successfully created organisation.
'403':
description: User is not a Campaign Admin.
content:
application/json:
schema:
properties:
error:
type: string
example: Unauthorized

/campaign/{id}/roles:
get:
operationId: getRolesByCampaignId
parameters:
- name: id
in: path
description: Campaign ID
required: true
schema:
type: integer
format: int64
description: Returns info about all roles in a campaign
tags:
- Campaign
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
campaigns:
type: array
items:
type: object
properties:
name:
type: string
example: Chief Mouser
description:
type: string
example: Larry the cat gone missing! now we need someone else to handle the rat issues at 10th Downing st.
min_available:
type: int32
example: 1
max_available:
type: int32
example: 3
finalised:
type: boolean
description: Whether this role has been finalised (e.g. max avaliable number)
example: False
/role/{id}:
get:
operationId: getRoleById
parameters:
- name: id
in: path
description: Role ID
required: true
schema:
type: integer
format: int32
description: Returns info about specified role.
tags:
- Role
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
name:
type: string
example: Chief Mouser
description:
type: string
example: Larry the cat gone missing! now we need someone else to handle the rat issues at 10th Downing st.
min_available:
type: int32
example: 1
max_available:
type: int32
example: 3
finalised:
type: boolean
description: Whether this role has been finalised (e.g. max avaliable number)
example: False
'401':
description: Not logged in.
content:
application/json:
schema:
properties:
error:
type: string
example: Not logged in.

put:
operationId: updateRoleById
parameters:
- name: id
in: path
description: Role ID
required: true
schema:
type: integer
format: int32
description: Update a role given the role id.
tags:
- Role
requestBody:
required: true
content:
application/json:
schema:
properties:
name:
type: string
example: Chief Whip
description:
type: string
required: False
example: Put a bit of stick about!
min_available:
type: int32
example: 1
max_available:
type: int32
example: 3
finalised:
type: boolean
description: Whether this role has been finalised (e.g. max avaliable number)
example: true
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
message:
type: string
example: Successfully update organisation.
'403':
description: User is not a Campaign Admin.
content:
application/json:
schema:
properties:
error:
type: string
example: Unauthorized

delete:
operationId: deleteRoleById
parameters:
- name: id
in: path
description: Role ID
required: true
schema:
type: integer
format: int32
description: Deletes specified role.
tags:
- Role
responses:
'200':
description: OK
content:
application/json:
schema:
properties:
message:
type: string
example: Successfully deleted role.
'403':
description: User is not an admin of role's Campaign.
content:
application/json:
schema:
Expand Down
2 changes: 1 addition & 1 deletion backend/migrations/20240406024211_create_organisations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CREATE TABLE organisations (
CREATE TYPE organisation_role AS ENUM ('User', 'Admin');

CREATE TABLE organisation_members (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
organisation_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
role organisation_role DEFAULT 'User' NOT NULL,
Expand Down
8 changes: 4 additions & 4 deletions backend/migrations/20240406025537_create_campaigns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ CREATE TABLE campaigns (
);

CREATE TABLE campaign_roles (
id SERIAL PRIMARY KEY,
id BIGINT PRIMARY KEY,
campaign_id BIGINT NOT NULL,
name TEXT NOT NULL,
description TEXT,
min_available INTEGER,
max_available INTEGER,
finalised BOOLEAN,
min_available INTEGER NOT NULL,
max_available INTEGER NOT NULL,
finalised BOOLEAN NOT NULL,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT FK_campaign_roles_campaign
Expand Down
2 changes: 1 addition & 1 deletion backend/migrations/20240406031400_create_questions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ CREATE TABLE questions (
);

CREATE TABLE multi_option_question_options (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
text TEXT NOT NULL,
question_id INTEGER NOT NULL,
CONSTRAINT FK_multi_option_question_options_questions
Expand Down
10 changes: 5 additions & 5 deletions backend/migrations/20240406031915_create_applications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ CREATE TABLE applications (
);

CREATE TABLE application_roles (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
application_id INTEGER NOT NULL,
campaign_role_id INTEGER NOT NULL,
CONSTRAINT FK_application_roles_applications
Expand All @@ -40,7 +40,7 @@ CREATE INDEX IDX_application_roles_applications on application_roles (applicatio
CREATE INDEX IDX_application_roles_campaign_roles on application_roles (campaign_role_id);

CREATE TABLE answers (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
application_id BIGINT NOT NULL,
question_id BIGINT NOT NULL,
CONSTRAINT FK_answers_applications
Expand All @@ -59,7 +59,7 @@ CREATE INDEX IDX_answers_applications on answers (application_id);
CREATE INDEX IDX_answers_questions on answers (question_id);

CREATE TABLE short_answer_answers (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
text TEXT NOT NULL,
answer_id INTEGER NOT NULL,
CONSTRAINT FK_short_answer_answers_answers
Expand All @@ -72,7 +72,7 @@ CREATE TABLE short_answer_answers (
CREATE INDEX IDX_short_answer_answers_answers on short_answer_answers (answer_id);

CREATE TABLE multi_option_answer_options (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
option_id BIGINT NOT NULL,
answer_id INTEGER NOT NULL,
CONSTRAINT FK_multi_option_answer_options_question_options
Expand All @@ -91,7 +91,7 @@ CREATE INDEX IDX_multi_option_answer_options_question_options on multi_option_an
CREATE INDEX IDX_multi_option_answer_options_answers on multi_option_answer_options (answer_id);

CREATE TABLE application_ratings (
id SERIAL PRIMARY KEY,
id BIGSERIAL PRIMARY KEY,
application_id BIGINT NOT NULL,
rater_id BIGINT NOT NULL,
rating INTEGER NOT NULL,
Expand Down
21 changes: 21 additions & 0 deletions backend/server/src/handler/campaign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::models::auth::AuthUser;
use crate::models::auth::CampaignAdmin;
use crate::models::campaign::Campaign;
use crate::models::error::ChaosError;
use crate::models::role::{Role, RoleUpdate};
use axum::extract::{Json, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
Expand Down Expand Up @@ -54,4 +55,24 @@ impl CampaignHandler {
Campaign::delete(id, &state.db).await?;
Ok((StatusCode::OK, "Successfully deleted campaign"))
}

pub async fn create_role(
State(state): State<AppState>,
Path(id): Path<i64>,
_admin: CampaignAdmin,
Json(data): Json<RoleUpdate>,
) -> Result<impl IntoResponse, ChaosError> {
Role::create(id, data, &state.db, state.snowflake_generator).await?;
Ok((StatusCode::OK, "Successfully created role"))
}

pub async fn get_roles(
State(state): State<AppState>,
Path(id): Path<i64>,
_user: AuthUser,
) -> Result<impl IntoResponse, ChaosError> {
let roles = Role::get_all_in_campaign(id, &state.db).await?;

Ok((StatusCode::OK, Json(roles)))
}
}
1 change: 1 addition & 0 deletions backend/server/src/handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod auth;
pub mod campaign;
pub mod organisation;
pub mod role;
Loading

0 comments on commit ba6fb2b

Please sign in to comment.