Skip to content

Commit

Permalink
Merge pull request #5 from BYTE-Club-CCNY/synchronize
Browse files Browse the repository at this point in the history
Synchronize
  • Loading branch information
LordFarquaadtheCreator authored Jun 13, 2024
2 parents 6f75b29 + 521aed9 commit 6c74603
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 117 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,7 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*


# data file
data.json
114 changes: 0 additions & 114 deletions data.json

This file was deleted.

2 changes: 1 addition & 1 deletion routes/databaseFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Client } from "pg";
* Function to add all items from values to database
* Assumes values array correctly maps to the database schema (no empty values, etc.)
*/
export function queryDatabase(
export async function queryDatabase(
client: Client,
query: string,
values: Array<any>,
Expand Down
7 changes: 5 additions & 2 deletions routes/projectsDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { queryDatabase } from "./databaseFunctions";
import { Client, QueryResult } from "pg";
import validate from "../middlewares/validate";
import getDB from "../db";
import synchronizeLocal from "../utils/synchronize";

const router: Router = Router();

Expand Down Expand Up @@ -66,13 +67,14 @@ async function startServer() {
}
});

router.post("/add", validate, (req: any, res: any) => {
router.post("/add", validate, async (req: any, res: any) => {
const values: Array<any> = Object.values(req.body);
const query = `
INSERT INTO projects (name, "short-desc", "long-desc", team, link, image, "tech-stack", cohort, topic)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`;
try {
queryDatabase(client, query, values);
await queryDatabase(client, query, values);
await synchronizeLocal(client);
return res.status(200).json({ message: "Project added successfully" });
} catch (err: any) {
return res.status(400).json({ message: err.message });
Expand Down Expand Up @@ -111,6 +113,7 @@ async function startServer() {
WHERE name = $${values.length}`;
try {
const result = await queryDatabase(client, query, values);
await synchronizeLocal(client);

if (result.rowCount === 0) {
return res.status(404).json({ message: "Project not found" });
Expand Down
19 changes: 19 additions & 0 deletions utils/synchronize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { writeFile } from 'fs/promises';
import { Client } from 'pg';
import { queryDatabase } from '../routes/databaseFunctions';
import path from 'path';

const FILE_PATH = path.resolve(__dirname, 'data.json');

const synchronizeLocal = async (client: Client) => {
const query = 'SELECT * FROM projects';
try {
const data = await queryDatabase(client, query, []);
await writeFile(FILE_PATH, JSON.stringify(data.rows, null, 2));
}
catch (e: any) {
throw Error(e);
}
}

export default synchronizeLocal;

0 comments on commit 6c74603

Please sign in to comment.