Skip to content

Commit df8790a

Browse files
authored
Merge pull request #12 from CS3219-AY2425S1/PEER-250-Endpoint-Schema
PEER-250 Endpoint Schema
2 parents 27442b3 + 7d5cc18 commit df8790a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+510
-683
lines changed

backend/collaboration/docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
services:
22
postgres:
33
hostname: postgres
4-
image: 'collaboration-db'
4+
image: postgres:16.4
55
container_name: 'collaboration_db'
66
build:
77
context: ./src/lib/db
8-
dockerfile: ./postgres.Dockerfile
98
environment:
109
POSTGRES_DB: 'Collaboration'
1110
POSTGRES_USER: 'user'
@@ -26,7 +25,7 @@ services:
2625
dockerfile: ./express.Dockerfile
2726
target: build
2827
ports:
29-
- '8002:8001'
28+
- '9003:8001'
3029
command: node dist/index.js
3130
depends_on:
3231
postgres:

backend/collaboration/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import app from './server';
2-
import { logger } from './lib/utils';
1+
import { logger } from '@/lib/utils';
2+
import app, { dbHealthCheck } from '@/server';
33

44
const port = process.env.PORT || 8001;
55

66
const listenMessage = `App listening on port: ${port}`;
77
app.listen(port, () => {
8+
void dbHealthCheck();
89
logger.info(listenMessage);
910
});

backend/collaboration/src/lib/db/postgres.Dockerfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

backend/collaboration/src/server.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { exit } from 'process';
2+
13
import express, { json } from 'express';
24
import pino from 'pino-http';
3-
import { db, tableName } from './lib/db';
5+
import { sql } from 'drizzle-orm';
6+
7+
import { db } from '@/lib/db';
8+
import { logger } from '@/lib/utils';
49

510
const app = express();
611
app.use(pino());
@@ -12,9 +17,20 @@ app.get('/', async (_req, res) => {
1217
});
1318
});
1419

20+
export const dbHealthCheck = async () => {
21+
try {
22+
await db.execute(sql`SELECT 1`);
23+
logger.info('Connected to DB');
24+
} catch (error) {
25+
const { message } = error as Error;
26+
logger.error('Cannot connect to DB: ' + message);
27+
exit(1);
28+
}
29+
};
30+
1531
// Ensure DB service is up before running.
1632
app.get('/test-db', async (_req, res) => {
17-
await db.select().from(tableName);
33+
await dbHealthCheck();
1834
res.json({ message: 'OK ' });
1935
});
2036

backend/question/docker-compose.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
services:
22
postgres:
33
hostname: postgres
4-
image: 'question-db'
4+
image: postgres:16.4
55
container_name: 'question_db'
66
build:
77
context: ./src/lib/db
8-
dockerfile: ./postgres.Dockerfile
98
environment:
109
POSTGRES_DB: 'Question'
1110
POSTGRES_USER: 'user'
@@ -26,7 +25,7 @@ services:
2625
dockerfile: ./express.Dockerfile
2726
target: build
2827
ports:
29-
- '8000:8001'
28+
- '9002:8001'
3029
command: node dist/index.js
3130
depends_on:
3231
postgres:

backend/question/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import app from './server';
2-
import { logger } from './lib/utils';
1+
import { logger } from '@/lib/utils';
2+
import app, { dbHealthCheck } from '@/server';
33

44
const port = process.env.PORT || 8001;
55

66
const listenMessage = `App listening on port: ${port}`;
77
app.listen(port, () => {
8+
void dbHealthCheck();
89
logger.info(listenMessage);
910
});

backend/question/src/lib/db/postgres.Dockerfile

Lines changed: 0 additions & 1 deletion
This file was deleted.

backend/question/src/server.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import { exit } from 'process';
2+
13
import express, { json } from 'express';
24
import pino from 'pino-http';
3-
import { db, tableName } from './lib/db';
5+
import { sql } from 'drizzle-orm';
6+
7+
import { db } from '@/lib/db';
8+
import { logger } from '@/lib/utils';
49

510
const app = express();
611
app.use(pino());
@@ -12,9 +17,20 @@ app.get('/', async (_req, res) => {
1217
});
1318
});
1419

20+
export const dbHealthCheck = async () => {
21+
try {
22+
await db.execute(sql`SELECT 1`);
23+
logger.info('Connected to DB');
24+
} catch (error) {
25+
const { message } = error as Error;
26+
logger.error('Cannot connect to DB: ' + message);
27+
exit(1);
28+
}
29+
};
30+
1531
// Ensure DB service is up before running.
1632
app.get('/test-db', async (_req, res) => {
17-
await db.select().from(tableName);
33+
await dbHealthCheck();
1834
res.json({ message: 'OK ' });
1935
});
2036

backend/question/src/services/get/index.ts

Whitespace-only changes.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type { IServiceResponse } from '@/types';
2+
3+
//=============================================================================
4+
// /get
5+
//=============================================================================
6+
export type IGetQuestionsPayload = {
7+
// Filters
8+
questionName?: string;
9+
difficulty?: string;
10+
topic?: Array<string>;
11+
// Pagination
12+
pageNum?: number; // Default 0
13+
recordsPerPage?: number; // Default 20
14+
};
15+
16+
export type IGetQuestionsResponse = IServiceResponse<{
17+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
18+
questions: Array<{
19+
// TODO: Add schema from DB
20+
/**
21+
* - name
22+
* - number
23+
* - difficulty
24+
* - topic
25+
* - attempted?: May need joining with users table, or not
26+
*/
27+
}>;
28+
isLastPage?: boolean;
29+
}>;
30+
31+
//=============================================================================
32+
// /details
33+
//=============================================================================
34+
export type IGetQuestionPayload = {
35+
questionNum: number;
36+
};
37+
38+
export type IGetQuestionResponse = IServiceResponse<{
39+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
40+
question: {
41+
// TODO: Add schema from db
42+
/**
43+
* - name
44+
* - number
45+
* - description
46+
* - difficulty
47+
* - topic
48+
* - submissionHistory?: TBC
49+
*/
50+
};
51+
}>;
52+
53+
//=============================================================================
54+
// /random (For matching)
55+
//=============================================================================
56+
export type IGetRandomQuestionPayload = {
57+
userId: number;
58+
difficulty?: string;
59+
topic?: Array<string>;
60+
};
61+
62+
export type IGetRandomQuestionResponse = IServiceResponse<{
63+
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
64+
question: {
65+
// TODO: Add schema from db
66+
/**
67+
* - name
68+
* - number
69+
* - description
70+
* - difficulty
71+
* - topic
72+
* - submissionHistory?: TBC
73+
*/
74+
};
75+
}>;

0 commit comments

Comments
 (0)