Skip to content

Commit eca9631

Browse files
committed
Added functionality to the adminRouter to get all paper data for modification. Need to make an update method within the databasecontroller. Paths set to local for development, perhaps an environment variable can be made for PROD and DEV?
1 parent 67c6966 commit eca9631

File tree

8 files changed

+89
-39
lines changed

8 files changed

+89
-39
lines changed

server/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"devDependencies": {
1616
"@types/axios": "^0.14.4",
1717
"@types/jest": "^29.5.14",
18-
"@types/multer": "^1.4.12",
1918
"@types/jsonwebtoken": "^9.0.7",
19+
"@types/multer": "^1.4.12",
2020
"@types/node": "^22.7.6",
2121
"@types/sqlite3": "^3.1.11",
2222
"jest": "^29.7.0",
@@ -34,8 +34,8 @@
3434
"cors": "^2.8.5",
3535
"dotenv": "^16.4.5",
3636
"express": "^4.21.1",
37-
"multer": "^1.4.5-lts.1",
3837
"jsonwebtoken": "^9.0.2",
38+
"multer": "^1.4.5-lts.1",
3939
"openai": "^4.67.3",
4040
"sqlite": "^5.1.1",
4141
"sqlite3": "^5.1.7"

server/src/database-controller.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { open, Database } from "sqlite";
33
import { DataTable } from "./interfaces/database-interface";
44
import { resolve } from "path";
55
import { rejects } from "assert";
6-
import { GetQuery, InsertData, RadData } from "./types";
6+
import { GetQuery, TableData, RadData } from "./types";
77
import { error } from "console";
88

99
export class DatabaseController {
@@ -99,7 +99,7 @@ export class DatabaseController {
9999
* created in the 'author_paper_join' table for each author.
100100
* Returns: None
101101
*/
102-
async insertPaper(paperData: InsertData): Promise<void> {
102+
async insertPaper(paperData: TableData): Promise<void> {
103103
if (!this.db) {
104104
throw new Error(`Database not initialized`);
105105
}
@@ -127,7 +127,7 @@ export class DatabaseController {
127127
* Returns:
128128
* - number: The ROWID of the paper.
129129
*/
130-
private async createPaper(paperData: InsertData): Promise<number> {
130+
private async createPaper(paperData: TableData): Promise<number> {
131131
if (!this.db) {
132132
throw new Error(`Database not initialized`);
133133
}
@@ -255,7 +255,48 @@ export class DatabaseController {
255255
}
256256
}
257257

258-
async getData(queryData: GetQuery): Promise<RadData[]> {
258+
async getFullData(search: string): Promise<TableData[]> {
259+
if (!this.db) {
260+
throw new Error("Database not initialized");
261+
}
262+
let query = `
263+
SELECT
264+
p.*,
265+
p.ROWID AS id,
266+
GROUP_CONCAT(a.name, ', ') AS author
267+
FROM
268+
paper p
269+
LEFT JOIN
270+
paper_author_join apj ON p.ROWID = apj.paper_id
271+
LEFT JOIN
272+
author a ON apj.author_id = a.ROWID
273+
WHERE
274+
p.paper_name LIKE ?
275+
GROUP BY
276+
p.ROWID
277+
`;
278+
279+
return new Promise<TableData[]>(async (resolve, reject) => {
280+
try {
281+
282+
const result = await this.db.all(query, `${search}%`);
283+
console.log("Query executed successfully:");
284+
285+
// Map rows to RadData format
286+
const radData: TableData[] = result.map((row) => ({
287+
...row,
288+
author: row.author ? row.author.split(", ") : [],
289+
}));
290+
291+
resolve(radData);
292+
} catch (error) {
293+
console.error("Error getting data", error);
294+
throw error;
295+
}
296+
});
297+
}
298+
299+
async getFilteredData(queryData: GetQuery): Promise<RadData[]> {
259300
if (!this.db) {
260301
throw new Error("Database not initialized");
261302
}

server/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const corsOptions = {
3434
origin: "*", // Allow all origins
3535
};
3636

37-
app.use(cors(corsOptions));
37+
app.use(cors());
3838
app.use(bodyParser.json());
3939

4040
async function initializeSystem(): Promise<{dbController: DatabaseController, gptController: GPTController}> {

server/src/routes/admin-router.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import express, { Request, Response, Router, NextFunction } from "express";
22
import { DatabaseController } from "../database-controller";
3-
import { GetQuery, GPTResponse, InsertData, RadData, Testing } from "../types";
3+
import { GetQuery, GPTResponse, TableData, RadData, Testing } from "../types";
44
import axios from "axios";
55
import jwt from "jsonwebtoken";
66
import authenticateJWT from "../auth/jwt-auth";
@@ -25,7 +25,10 @@ export default function adminRouter(
2525
authenticateJWT,
2626
async (req: Request, res: Response) => {
2727
try {
28-
await insertRows(requestFromJSON(req.body), dbController).then(() => {
28+
await insertRows(
29+
insertDataRequestFromJSON(req.body),
30+
dbController,
31+
).then(() => {
2932
// 201: The request was successful, and a new resource was created
3033
res.send(201);
3134
});
@@ -46,7 +49,23 @@ export default function adminRouter(
4649
res.send(responseToJSON(result));
4750
});
4851
} catch (error) {
49-
console.error(``);
52+
console.error(`${error}`);
53+
}
54+
},
55+
);
56+
57+
router.post(
58+
"/getFullPapers",
59+
/*authenticateJWT,*/ (req: Request, res: Response) => {
60+
try {
61+
// More intricate searches can be employed later similar to the table filter
62+
getFullPaperRows(req.body.search, dbController).then(
63+
(result: TableData[]) => {
64+
res.send(JSON.stringify(result, null, 2));
65+
},
66+
);
67+
} catch (error) {
68+
console.error(`${error}`);
5069
}
5170
},
5271
);
@@ -105,7 +124,7 @@ export default function adminRouter(
105124
}
106125

107126
async function insertRows(
108-
insertData: InsertData[],
127+
insertData: TableData[],
109128
dbcontroller: DatabaseController,
110129
): Promise<void> {
111130
for (const paper in insertData) {
@@ -123,7 +142,14 @@ async function parsePapers(
123142
return temp;
124143
}
125144

126-
function requestFromJSON(body: any): InsertData[] {
145+
async function getFullPaperRows(
146+
search: string,
147+
dbController: DatabaseController,
148+
): Promise<TableData[]> {
149+
return await dbController.getFullData(search);
150+
}
151+
152+
function insertDataRequestFromJSON(body: any): TableData[] {
127153
// Ensure body is an array of objects matching the InsertData structure
128154
if (!Array.isArray(body)) {
129155
throw new Error("Invalid body format: expected an array.");
@@ -132,7 +158,7 @@ function requestFromJSON(body: any): InsertData[] {
132158
// Return a list of rows to insert
133159
return body.map((entry) => {
134160
// Return the validated entry as InsertData
135-
return { ...entry } as InsertData;
161+
return { ...entry } as TableData;
136162
});
137163
}
138164

server/src/routes/cascade-router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function getFilteredRows(
4646
getData: GetQuery,
4747
dbcontroller: DatabaseController,
4848
): Promise<RadData[]> {
49-
return dbcontroller.getData(getData);
49+
return dbcontroller.getFilteredData(getData);
5050
}
5151

5252
function requestFromJSON(body: any) {

server/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export type GetQuery = {
1414
data_type?: number;
1515
};
1616

17-
export type InsertData = {
17+
export type TableData = {
18+
id?: number;
1819
paper_name: string;
1920
year: number;
2021
author: string[];

server/test/database-controller.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { DatabaseController } from '../src/database-controller';
22
import sqlite3 from 'sqlite3';
33

4-
import { GetQuery, InsertData, RadData } from '../src/types';
4+
import { GetQuery, TableData, RadData } from '../src/types';
55
import Response from 'express';
66
import { open } from 'sqlite';
77

@@ -37,7 +37,7 @@ beforeAll(async () => {
3737
***********/
3838

3939
test('Insertion of a paper with one author', async () => {
40-
const testPaperData: InsertData = {
40+
const testPaperData: TableData = {
4141
paper_name: 'Tested Radiation Effects',
4242
year: 2024,
4343
author: ['S. Davis'],
@@ -53,7 +53,7 @@ test('Insertion of a paper with one author', async () => {
5353
});
5454

5555
test('Insertion of a paper with multiple authors', async () => {
56-
const testPaperData: InsertData = {
56+
const testPaperData: TableData = {
5757
paper_name: 'Radiation Test Effects On Tested Radiation',
5858
year: 2023,
5959
author: ['John Jacob', 'Lin Lee', 'Dr. Joan Gooding'],

web/src/pages/ModifyPage.tsx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,8 @@ import { PaperData } from "../types/types";
66
import PaperSliver from "../components/paper-sliver";
77
import PaperGallery from "../components/paper-gallery";
88

9-
10-
119
export default function ModifyPage() {
12-
const [papers, setPapers] = useState<PaperData[]>([
13-
{
14-
id: 1,
15-
paper_name:
16-
"SEE In-Flight Data for two Static 32KB Memories on High Earth Orbit",
17-
author: [
18-
"S. Duzellier",
19-
"S. Bourdarie",
20-
"R. Velazco",
21-
"B. Nicolescu",
22-
"R. Ecoffet",
23-
],
24-
},
25-
26-
]);
10+
const [papers, setPapers] = useState<PaperData[]>([]);
2711

2812
const [paperAreaHeight, setPaperAreaHeight] = useState<number>();
2913

@@ -32,7 +16,7 @@ export default function ModifyPage() {
3216

3317
try {
3418
const response = await fetch(
35-
"https://starr-lab-server.usask.ca/api/adminRequest/getFullPapers",
19+
"http://localhost:3000/api/adminRequest/getFullPapers",
3620
{
3721
method: "POST",
3822
headers: {
@@ -66,8 +50,6 @@ export default function ModifyPage() {
6650
fetchPapers(value);
6751
};
6852

69-
70-
7153
return (
7254
<div className="flex flex-col items-center h-full">
7355
<div className="bg-[#F4F4F4] w-[70%]">
@@ -78,7 +60,7 @@ export default function ModifyPage() {
7860
</div>
7961
<div className="">
8062
<div className="overflow-y-scroll max-h-screen">
81-
<PaperGallery papers={papers}/>
63+
<PaperGallery papers={papers} />
8264
</div>
8365
</div>
8466
</div>

0 commit comments

Comments
 (0)