boolean-uk / team-dev-server

3 stars 12 forks source link

Team Simulation - Server

Setting up

  1. Copy .env.example and name it .env
  2. Create a postgres database and add its URL into the DATABASE_URL environment variable, keeping ?schema=prisma on the end
    • Postgres db URLs are in the format: postgres://[USERNAME]:[PASSWORD]@[HOST]:[PORT]/[DATABASE_NAME]
    • Note that prisma doesn't store data in the public schema, so set the search path to prisma in your db client. For PSQL client
    • use \dn to show available schemas
    • use SQL to set the search path to the correct schema: SET search_path to prisma;
    • \dt will then show available tables (once migrations have been run)
  3. If using a cloud database provider:
    • Create another database and run create schema shadow on it
    • Add its URL into the SHADOW_DATABASE_URL env var, keeping ?schema=shadow on the end
  4. npm ci to install dependencies
  5. npx prisma migrate reset to apply migrations to your db
  6. npm run dev to run the app

Sample Requests

If you use Insomnia, you can import this request collection .json file in the ./assets/ folder to hit the ground running with all of the requests ready to test.

API Spec

POST /user Example Request ```sh curl -X POST http://localhost:4000/user \ -H 'Content-Type: application/json' \ -d '{"first_name":"Nathan","last_name":"King","email":"ngk5@gmail.com","password":"mysecurepassword","biography":"Hello world","github_url":"https://github.com/vherus"}' ``` Example body ```sh { "first_name": "Nathan", "last_name": "King", "email": "ngk5@gmail.com", "password": "mysecurepassword", "biography": "Hello world", "github_url": "https://github.com/vherus" } ``` Example response ```sh { "status": "success", "data": { "user": { "id": 8, "cohort_id": null, "role": "STUDENT", "first_name": "Nathan", "last_name": "King", "email": "ngk5@gmail.com", "biography": "Hello world", "github_url": "https://github.com/vherus" } } } ```
POST /login Example body ```sh { "email": "ngk5@gmail.com", "password": "mysecurepassword" } ``` Example response ```sh { "status": "success", "data": { "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOjUsImlhdCI6MTY0OTQxMzk0OSwiZXhwIjoxNjQ5NTAwMzQ5fQ.b37lSRtpFWJ9kqUYAc6PUIP28JXjAYtBN_GpU5TcEuc", "user": { "id": 5, "cohort_id": null, "role": "STUDENT", "first_name": "Nathan", "last_name": "King", "email": "ngk2@gmail.com", "biography": "Hello world", "github_url": "https://github.com/vherus" } } } ```
POST /post (hardcoded responses) Headers ```sh Authorization: Bearer <token> ``` Example body ```sh { "content": "Hello world!" } ``` Example response ```sh { "status": "success", "data": { "post": { "id": 1, "content": "Hello world!" } } } ```
POST /cohort Only auth tokens for users with the TEACHER role can use this route Headers ```sh Authorization: Bearer <token> ``` No body required Example response ```sh { "status": "success", "data": { "cohort": { "id": 3 } } } ```
POST /log (hardcoded responses) Only auth tokens for users with the TEACHER role can use this route Headers ```sh Authorization: Bearer <token> ``` Example body ```sh { "date": "2022-05-05", "cohort_id": 3, "lines": [ { "content": "Caught up with James" }, { "content": "Punished Joel" } ] } ``` Example response ```sh { "status": "success", "data": { "log": { "id": 1, "cohort_id": 3, "date": "2022-05-05", "author": { "id": 5, "first_name": "Nathan", "last_name": "King" }, "lines": [ { "id": 1, "content": "Caught up with James" }, { "id": 2, "content": "Punished Joel" } ] } } } ```
GET /posts (hardcoded responses) Headers ```sh Authorization: Bearer <token> ``` Example response ```sh { "status": "success", "data": { "posts": [ { "id": 1, "content": "Hello world!", "author": { "id": 5, "cohortId": null, "firstName": "Nathan", "lastName": "King", "email": "ngk2@gmail.com", "bio": "Hello world", "githubUrl": "https://github.com/vherus", "role": "STUDENT" } }, { "id": 2, "content": "Hello from the void!", "author": { "id": 5, "cohortId": null, "firstName": "Nathan", "lastName": "King", "email": "ngk2@gmail.com", "bio": "Hello world", "githubUrl": "https://github.com/vherus", "role": "STUDENT" } } ] } } ```
GET /user/:id Headers ```sh Authorization: Bearer <token> ``` Example response ```sh { "status": "success", "data": { "user": { "id": 1, "cohort_id": null, "role": "STUDENT", "first_name": "Nathan", "last_name": "King", "email": "ngk6@gmail.com", "biography": "Hello world", "github_url": "https://github.com/vherus" } } } ```
GET /users?first_name=Name The first_name query parameter is optional and case sensitive Headers ```sh Authorization: Bearer <token> ``` Example response ```sh { "status": "success", "data": { "users": [ { "id": 1, "cohort_id": null, "role": "STUDENT", "first_name": "Nathan", "last_name": "King", "email": "nk3@gmail.com", "biography": "Hello world", "github_url": "https://github.com/vherus" }, { "id": 3, "cohort_id": null, "role": "STUDENT", "first_name": "Nathan", "last_name": "Queen", "email": "nk2@gmail.com", "biography": "Hello world", "github_url": "https://github.com/vherus" } ] } } ```
PATCH /user/:id (hardcoded responses) Only auth tokens for users with the TEACHER role can use this route Headers ```sh Authorization: Bearer <token> ``` Example body ```sh { "cohort_id": 3 } ``` Example response ```sh { "status": "success", "data": { "user": { "cohort_id": 3 } } } ```