Skip to content

Commit

Permalink
Merge branch 'CHAOS-224-KHAOS-rewrite' into CHAOS-461-usercrud
Browse files Browse the repository at this point in the history
  • Loading branch information
KavikaPalletenne authored Aug 2, 2024
2 parents 251b4a8 + f140dd6 commit 7968aa6
Show file tree
Hide file tree
Showing 33 changed files with 2,206 additions and 62 deletions.
20 changes: 20 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,26 @@

CHAOS' backend is implemented in Rust and for data persistence, we use PostgreSQL.

## Table of Contents

- [Dev Setup](#dev-setup)
- [Code Structure](#code-structure)
- [Tech Stack](#tech-stack)


## Dev Setup

To run the backend in a dev/testing environment:
1. Install `docker-compose` (see [official installation guide](https://docs.docker.com/compose/install/)).
2. Navigate to the directory this file is in (`backend`) in your terminal (not `backend/server`).
3. Possibly terminate any running instances of postgres, as the dockerized postgres we will spawn uses the same default port, so the two might interefere with each other.
4. Run `./setup-dev-env.sh` (you might have to make it executable before with `chmod +x setup-dev-env.sh`), which should drop you into a new shell that has the required tools installed.
5. Now, you can `cd server` and should be able to `cargo build` successfully.
6. Once you exit out of the newly created shell (e.g. type `exit`, or kill the terminal), the dockerized postgres instance should automatically be torn down, so it's not unnecessarily running in the background all the time.


## Code Structure

### Service
The service module contains all functions that conduct business logic, and also interact with the database. This
separation from the request handling makes it easy to swap out any new form of requests, but reuse the same business
Expand All @@ -27,6 +46,7 @@ Request -> Middleware (optional) -> Handler -> Service -> Middleware (Optional)


## Tech Stack

### Web Server
- [Axum](https://github.com/tokio-rs/axum)

Expand Down
4 changes: 4 additions & 0 deletions backend/api.json
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@
"name": {
"type": "string",
"example": "Clancy Lion"
},
"role": {
"type": "string",
"example": "Admin"
}
}
}
Expand Down
238 changes: 238 additions & 0 deletions backend/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,9 @@ paths:
name:
type: string
example: Clancy Lion
role:
type: string
example: Admin
'403':
description: User is not an organisation admin or member.
content:
Expand Down Expand Up @@ -814,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
17 changes: 10 additions & 7 deletions backend/migrations/20240406024211_create_organisations.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
CREATE TABLE organisations (
id BIGINT PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
logo TEXT,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
logo UUID,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL
);

CREATE TABLE organisation_admins (
id SERIAL PRIMARY KEY,
CREATE TYPE organisation_role AS ENUM ('User', 'Admin');

CREATE TABLE organisation_members (
id BIGSERIAL PRIMARY KEY,
organisation_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
CONSTRAINT FK_organisation_admins_organisation
role organisation_role DEFAULT 'User' NOT NULL,
CONSTRAINT FK_organisation_members_organisation
FOREIGN KEY(organisation_id)
REFERENCES organisations(id)
ON DELETE CASCADE
ON UPDATE CASCADE
);

CREATE INDEX IDX_organisation_admins_organisation on organisation_admins (organisation_id);
CREATE INDEX IDX_organisation_admins_organisation on organisation_members (organisation_id);
18 changes: 9 additions & 9 deletions backend/migrations/20240406025537_create_campaigns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ CREATE TABLE campaigns (
id BIGINT PRIMARY KEY,
organisation_id BIGINT NOT NULL,
name TEXT NOT NULL,
cover_image TEXT,
cover_image UUID,
description TEXT,
starts_at TIMESTAMPTZ NOT NULL,
ends_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT FK_campaigns_organisations
FOREIGN KEY(organisation_id)
REFERENCES organisations(id)
Expand All @@ -16,15 +16,15 @@ 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,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
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
FOREIGN KEY(campaign_id)
REFERENCES campaigns(id)
Expand Down
6 changes: 3 additions & 3 deletions backend/migrations/20240406031400_create_questions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ CREATE TABLE questions (
required BOOLEAN,
question_type question_type NOT NULL,
campaign_id BIGINT NOT NULL,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP NOT NULL,
CONSTRAINT FK_questions_campaigns
FOREIGN KEY(campaign_id)
REFERENCES campaigns(id)
Expand All @@ -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
Loading

0 comments on commit 7968aa6

Please sign in to comment.