From 54020dc9d5de02590446f7a45c32f2b330acabf9 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 18:41:33 +0100 Subject: [PATCH 01/15] add public to create --- server/src/routes/board.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/server/src/routes/board.ts b/server/src/routes/board.ts index a5eb2d2..53e020b 100644 --- a/server/src/routes/board.ts +++ b/server/src/routes/board.ts @@ -2,6 +2,7 @@ import express from "express"; import * as dotenv from "dotenv"; import path from "path"; import { Deta } from "deta"; +import argon2 from "argon2"; import { ITask, IBoard } from "../interfaces/interfaces"; dotenv.config({ path: path.resolve(__dirname, "../.env") }); @@ -14,10 +15,15 @@ const router = express.Router(); router.post("/create", async (req, res) => { try { + let passwordHash = ""; const boardDataSet: IBoard = req.body as IBoard; if (typeof boardDataSet.title !== "string" || typeof boardDataSet.owner !== "string") { throw new Error("Invalid 'title' or 'owner' in the request."); } + + if (boardDataSet.public) { + passwordHash = await argon2.hash(boardDataSet.password); + } const key = boardDataSet.title.trim() + boardDataSet.owner.trim(); @@ -33,7 +39,9 @@ router.post("/create", async (req, res) => { key: key, title: boardDataSet.title, owner: boardDataSet.owner, - tasks: tasks + tasks: tasks, + public: boardDataSet.public, + password: passwordHash }; const newtaskDataSet = await boardSets.insert(JSON.parse(JSON.stringify(boardDataSetJSON))); @@ -47,4 +55,21 @@ router.post("/create", async (req, res) => { } }); + +router.get("/getPublic", async (req, res) => { + try { + const fetchedBoards = await boardSets.fetch({ public: true }); + if (fetchedBoards == null) { + res.status(409).json({ + error: "This task does not exist." + }); + return false; + } else { + res.status(201).json({ fetchedBoards }); + } + } catch (err) { + res.status(503).json({ error: err.message }); + } +}); + export default router; From bf4720e6af9c3cb222b506cfe3e65a9e49b2ac93 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 18:41:46 +0100 Subject: [PATCH 02/15] add public to board interface --- server/src/interfaces/interfaces.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/src/interfaces/interfaces.ts b/server/src/interfaces/interfaces.ts index 8f86b93..a6dee20 100644 --- a/server/src/interfaces/interfaces.ts +++ b/server/src/interfaces/interfaces.ts @@ -23,4 +23,6 @@ export interface IBoard { owner: string; title: string; tasks: ITask[]; + public: boolean; + password?: string; } From 9abf991f211ac7d94d855a12d26abcfe5319ef64 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 18:53:49 +0100 Subject: [PATCH 03/15] add public boards endpoint Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/board.ts | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/server/src/routes/board.ts b/server/src/routes/board.ts index 53e020b..16b4a84 100644 --- a/server/src/routes/board.ts +++ b/server/src/routes/board.ts @@ -3,13 +3,14 @@ import * as dotenv from "dotenv"; import path from "path"; import { Deta } from "deta"; import argon2 from "argon2"; -import { ITask, IBoard } from "../interfaces/interfaces"; +import { ITask, IBoard, IAddPublicBoard } from "../interfaces/interfaces"; dotenv.config({ path: path.resolve(__dirname, "../.env") }); // deta setup const projectKey: string = process.env.DETA_PROJECT_KEY; const deta = Deta(projectKey); const boardSets = deta.Base("board"); +const publicBoards = deta.Base("publicBoards"); const router = express.Router(); @@ -20,7 +21,7 @@ router.post("/create", async (req, res) => { if (typeof boardDataSet.title !== "string" || typeof boardDataSet.owner !== "string") { throw new Error("Invalid 'title' or 'owner' in the request."); } - + if (boardDataSet.public) { passwordHash = await argon2.hash(boardDataSet.password); } @@ -72,4 +73,31 @@ router.get("/getPublic", async (req, res) => { } }); + +router.post("/addPublicBoard", async (req, res) => { + try { + const addPublicBoardData: IAddPublicBoard = req.body as IAddPublicBoard; + + + if (!(await boardSets.get(addPublicBoardData.title))) { + throw new Error("This board does not exist"); + } + + const addPublicBoardDataJSON = { + key: addPublicBoardData.owner + addPublicBoardData.title, + }; + + await boardSets.insert(JSON.parse(JSON.stringify(addPublicBoardDataJSON))); + + res.status(201).json({ + title: addPublicBoardData.title, + owner: addPublicBoardData.owner, + success: true + }); + } catch (err) { + res.status(503).json({ error: err.message }); + } +}); + + export default router; From de9d7b0956b9faac8f51d34a0994921580f87cc1 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 19:01:06 +0100 Subject: [PATCH 04/15] fix error with adding public board Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/board.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/server/src/routes/board.ts b/server/src/routes/board.ts index 16b4a84..346b643 100644 --- a/server/src/routes/board.ts +++ b/server/src/routes/board.ts @@ -4,6 +4,7 @@ import path from "path"; import { Deta } from "deta"; import argon2 from "argon2"; import { ITask, IBoard, IAddPublicBoard } from "../interfaces/interfaces"; +import { title } from "process"; dotenv.config({ path: path.resolve(__dirname, "../.env") }); // deta setup @@ -83,11 +84,17 @@ router.post("/addPublicBoard", async (req, res) => { throw new Error("This board does not exist"); } + if (await publicBoards.get(addPublicBoardData.owner + addPublicBoardData.title) !== null) { + throw new Error("You already added this board to your public boards."); + } + const addPublicBoardDataJSON = { key: addPublicBoardData.owner + addPublicBoardData.title, + owner: addPublicBoardData.owner, + title: addPublicBoardData.title }; - await boardSets.insert(JSON.parse(JSON.stringify(addPublicBoardDataJSON))); + await publicBoards.insert(JSON.parse(JSON.stringify(addPublicBoardDataJSON))); res.status(201).json({ title: addPublicBoardData.title, From f87c5f15836db7081f7297f047fa5e1ccef1a49e Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 19:31:02 +0100 Subject: [PATCH 05/15] add interface to add public board --- server/src/interfaces/interfaces.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/server/src/interfaces/interfaces.ts b/server/src/interfaces/interfaces.ts index a6dee20..d132198 100644 --- a/server/src/interfaces/interfaces.ts +++ b/server/src/interfaces/interfaces.ts @@ -16,8 +16,7 @@ export interface ITask { title: string; definition: string; owner: string; - board: string; -} +} export interface IBoard { owner: string; @@ -26,3 +25,8 @@ export interface IBoard { public: boolean; password?: string; } + +export interface IAddPublicBoard { + title: string; + owner: string; +} \ No newline at end of file From e5349a18a4b632864f4ebce2d73f722cc93734f6 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 19:33:11 +0100 Subject: [PATCH 06/15] add endpoint to get all board from an user Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/board.ts | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/server/src/routes/board.ts b/server/src/routes/board.ts index 346b643..72aa40c 100644 --- a/server/src/routes/board.ts +++ b/server/src/routes/board.ts @@ -107,4 +107,32 @@ router.post("/addPublicBoard", async (req, res) => { }); +router.get("/getAll/:username", async (req, res) => { + try { + const username = req.params.username; + const fetchedBoards = await boardSets.fetch({ owner: username }); + const fetchedPublicBoards: any = await publicBoards.fetch({ owner: username }); + + for (let i = 0; i < fetchedPublicBoards.count; i++) { + const board = await boardSets.get(fetchedPublicBoards.items[i].key.replace(fetchedPublicBoards.items[i].owner, '')); + delete board.password; + fetchedBoards.items.push(board); + } + for(let i = 0; i < fetchedBoards.count; i++) { + if(fetchedBoards.items[i].public) { + delete fetchedBoards.items[i].password; + } + } + if (fetchedBoards == null || publicBoards == null) { + res.status(409).json({ + error: "No boards yet." + }); + return false; + } else { + res.status(201).json({ fetchedBoards }); + } + } catch (err) { + res.status(503).json({ error: err.message }); + } +}); export default router; From ec4dc8c519382e33d070fd0b3b7b4813058865e0 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 20:00:54 +0100 Subject: [PATCH 07/15] add password check when getting all public boards Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/board.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/server/src/routes/board.ts b/server/src/routes/board.ts index 72aa40c..a2ed3cb 100644 --- a/server/src/routes/board.ts +++ b/server/src/routes/board.ts @@ -3,8 +3,7 @@ import * as dotenv from "dotenv"; import path from "path"; import { Deta } from "deta"; import argon2 from "argon2"; -import { ITask, IBoard, IAddPublicBoard } from "../interfaces/interfaces"; -import { title } from "process"; +import { ITask, IBoard, IAddPublicBoard, IGetPublicBoards } from "../interfaces/interfaces"; dotenv.config({ path: path.resolve(__dirname, "../.env") }); // deta setup @@ -107,16 +106,25 @@ router.post("/addPublicBoard", async (req, res) => { }); -router.get("/getAll/:username", async (req, res) => { +router.get("/getAll/", async (req, res) => { try { - const username = req.params.username; + const parameters: IGetPublicBoards = req.body as IGetPublicBoards; + const username = parameters.username; const fetchedBoards = await boardSets.fetch({ owner: username }); const fetchedPublicBoards: any = await publicBoards.fetch({ owner: username }); - for (let i = 0; i < fetchedPublicBoards.count; i++) { - const board = await boardSets.get(fetchedPublicBoards.items[i].key.replace(fetchedPublicBoards.items[i].owner, '')); - delete board.password; - fetchedBoards.items.push(board); + for (let fetchedPublicBoard of fetchedPublicBoards.items) { + if (parameters.passwords != null) { + for (let password of parameters.passwords) { + const board = await boardSets.get(fetchedPublicBoard.key.replace(fetchedPublicBoard.owner, '')); + if (password.title === board.key) { + if (await argon2.verify(board.password as string, password.password)) { + delete board.password; + fetchedBoards.items.push(board); + } + } + } + } } for(let i = 0; i < fetchedBoards.count; i++) { if(fetchedBoards.items[i].public) { From d882de631b9467d78844031a9647e042945c0444 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 20:01:20 +0100 Subject: [PATCH 08/15] add IPasswordBoard interface Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/interfaces/interfaces.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/src/interfaces/interfaces.ts b/server/src/interfaces/interfaces.ts index d132198..552c0e9 100644 --- a/server/src/interfaces/interfaces.ts +++ b/server/src/interfaces/interfaces.ts @@ -29,4 +29,13 @@ export interface IBoard { export interface IAddPublicBoard { title: string; owner: string; +} + +export interface IGetPublicBoards { + username: string; + passwords: IPasswordBoard[]; +} +export interface IPasswordBoard { + title: string; + password: string; } \ No newline at end of file From 8e1f1da5e12b2e16fdae263aed2e21b04a06b1da Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 20:36:49 +0100 Subject: [PATCH 09/15] add endpoint to create task Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/task.ts | 117 +++++--------------------------------- 1 file changed, 13 insertions(+), 104 deletions(-) diff --git a/server/src/routes/task.ts b/server/src/routes/task.ts index dfc4798..7b3cfa8 100644 --- a/server/src/routes/task.ts +++ b/server/src/routes/task.ts @@ -8,7 +8,6 @@ dotenv.config({ path: path.resolve(__dirname, "../.env") }); // deta setup const projectKey: string = process.env.DETA_PROJECT_KEY; const deta = Deta(projectKey); -const taskSets = deta.Base("tasks"); const boardSets = deta.Base("board"); const router = express.Router(); @@ -18,124 +17,34 @@ router.post("/create/:board", async (req, res) => { const taskDataSet: ITask = req.body as ITask; const key = taskDataSet.title.trim() + taskDataSet.owner.trim(); const boardTitle = req.params.board; - if (!taskDataSet.title || !taskDataSet.owner) { + if (!taskDataSet.title || !taskDataSet.owner ) { throw new Error("Both 'title' and 'owner' are required fields."); } - const existingTask = await taskSets.get(key); - const existingBoard = await taskSets.get(boardTitle); - - if (existingTask) { - throw new Error("This task name exists already. Please try to edit this!"); - } else if (existingBoard) { - throw new Error("The board of the to-be-created task does not exist."); - } - const taskDataSetJSON = { - key: key, title: taskDataSet.title, - definition: taskDataSet.definition + description: taskDataSet.description, + dueDate: taskDataSet.dueDate, + owner: taskDataSet.owner }; - const newTaskDataSet = await taskSets.insert(taskDataSetJSON); - - const taskBoardArray = await boardSets.get(req.params.board); - var updatedArray = []; - updatedArray = Array.isArray(taskBoardArray) ? [taskDataSet, ...taskBoardArray] : []; - const boardKey = await boardSets.get(key); - const stringifiedKey = JSON.stringify(boardKey); - boardSets.update(JSON.parse(JSON.stringify(updatedArray)), stringifiedKey); + const existing = await boardSets.get(boardTitle); + const currentTasks = existing.tasks; + (currentTasks as any[]).push(taskDataSetJSON); + await boardSets.delete(boardTitle); + const newtaskDataSet = await boardSets.insert(existing); res.status(201).json({ title: taskDataSet.title, - definition: taskDataSet.definition + description: taskDataSet.description, + dueDate: taskDataSet.dueDate, + owner: taskDataSet.owner, + success: true }); } catch (err) { res.status(503).json({ error: err.message }); } }); -router.get("/get/:board/:id", async (req, res) => { - try { - const { id, board } = req.params; - - if (!id || !board) { - throw new Error("Both 'id' and 'board' parameters are required."); - } - - const fetchedtaskSets = await taskSets.fetch({ id, board }); - if (fetchedtaskSets == null) { - res.status(409).json({ - error: "This task does not exist." - }); - return false; - } else { - res.status(201).json({ fetchedtaskSets }); - } - } catch (err) { - res.status(503).json({ error: err.message }); - } -}); - -router.get("/getTaskAll/:board", async (req, res) => { - try { - const fetchedtaskSets = await taskSets.get(req.params.board); - if (fetchedtaskSets == null) { - res.status(409).json({ - error: "This task does not exist." - }); - return false; - } else { - res.status(201).json({ fetchedtaskSets }); - } - } catch (err) { - res.status(503).json({ error: err.message }); - } -}); - -router.delete("/deleteTask/:board/:id", async (req, res) => { - try { - const { id, board } = req.params; - if (!id || !board) { - throw new Error("Both 'id' and 'board' parameters are required."); - } - const fetchedtaskSets = await taskSets.fetch({ id, board }); - if (fetchedtaskSets != null) { - await taskSets.delete(id); - res.status(200).json({ message: "Deleted task", id: req.params.id, sucess: true }); - } else { - res.status(409).json({ - error: "This task does not exist." - }); - return false; - } - } catch (err) { - res.status(503).json({ error: err.message }); - } -}); - -router.post("/updateTask/:board/:id", async (req, res) => { - try { - const taskSetsData: ITask = req.body as ITask; - const { id, board } = req.params; - if (!(await taskSets.fetch({ id, board }))) { - throw new Error("This task does not exist."); - } - - const taskSetsDataJson = { - title: taskSetsData.title, - definition: taskSetsData.definition - }; - - await taskSets.insert(taskSetsDataJson); - - res.status(201).json({ - title: taskSetsData.title, - definition: taskSetsData.definition - }); - } catch (err) { - res.status(503).json({ error: err.message }); - } -}); export default router; From 5a750c46dc4128167027bfb3cfad0315c4843342 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 20:37:11 +0100 Subject: [PATCH 10/15] update interfaces Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/interfaces/interfaces.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/src/interfaces/interfaces.ts b/server/src/interfaces/interfaces.ts index 552c0e9..6572150 100644 --- a/server/src/interfaces/interfaces.ts +++ b/server/src/interfaces/interfaces.ts @@ -14,7 +14,8 @@ export interface IJWTData { export interface ITask { title: string; - definition: string; + description: string; + dueDate: string; owner: string; } From 163ed8a5da586ceca168eb87545bb1bdfb9475cc Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 20:47:18 +0100 Subject: [PATCH 11/15] add endpoint to delete task Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/task.ts | 48 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/server/src/routes/task.ts b/server/src/routes/task.ts index 7b3cfa8..e28b132 100644 --- a/server/src/routes/task.ts +++ b/server/src/routes/task.ts @@ -47,4 +47,52 @@ router.post("/create/:board", async (req, res) => { }); +router.post("/delete/:board/", async (req, res) => { + try { + const taskDataSet: ITask = req.body as ITask; + const key = taskDataSet.title.trim() + taskDataSet.owner.trim(); + const boardTitle = req.params.board; + if (!taskDataSet.title || !taskDataSet.owner ) { + throw new Error("Both 'title' and 'owner' are required fields."); + } + + const taskDataSetJSON = { + title: taskDataSet.title, + description: taskDataSet.description, + dueDate: taskDataSet.dueDate, + owner: taskDataSet.owner + }; + + const existing = await boardSets.get(boardTitle); + const currentTasks = existing.tasks; + let taskFound = false; + existing.tasks = (currentTasks as any[]).filter(task => { + const isMatch = task.title === taskDataSetJSON.title + && task.owner === taskDataSetJSON.owner + && task.description === taskDataSetJSON.description + && task.dueDate === taskDataSetJSON.dueDate; + if (isMatch) { + taskFound = true; + } + return !isMatch; + }); + + if (!taskFound) { + throw new Error("Task not found."); + } + + await boardSets.delete(boardTitle); + const newtaskDataSet = await boardSets.insert(existing); + res.status(201).json({ + title: taskDataSet.title, + description: taskDataSet.description, + dueDate: taskDataSet.dueDate, + owner: taskDataSet.owner, + success: true + }); + } catch (err) { + res.status(503).json({ error: err.message }); + } +}); + export default router; From 3643a0ab287eae5c2a727a83f73cdd9be034b3d8 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 21:29:04 +0100 Subject: [PATCH 12/15] add interface for task update Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff Date: Wed, 20 Dec 2023 21:29:40 +0100 Subject: [PATCH 13/15] add endpoint to update task Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff { } }); +router.post("/update/:board/", async (req, res) => { + try { + const taskDataSet: ITaskUpdate = req.body as ITaskUpdate; + const key = taskDataSet.title.trim() + taskDataSet.owner.trim(); + const boardTitle = req.params.board; + if (!taskDataSet.title || !taskDataSet.owner ) { + throw new Error("Both 'title' and 'owner' are required fields."); + } + + const taskDataSetJSON = { + title: taskDataSet.title, + description: taskDataSet.description, + dueDate: taskDataSet.dueDate, + owner: taskDataSet.owner + }; + + const updatedTaskDataSetJSON = { + title: taskDataSet.newTitle, + description: taskDataSet.newDescription, + dueDate: taskDataSet.newDueDate, + owner: taskDataSet.owner + }; + + const existing = await boardSets.get(boardTitle); + const currentTasks = existing.tasks; + + // delete old task + let taskFound = false; + existing.tasks = (currentTasks as any[]).filter(task => { + const isMatch = task.title === taskDataSetJSON.title + && task.owner === taskDataSetJSON.owner + && task.description === taskDataSetJSON.description + && task.dueDate === taskDataSetJSON.dueDate; + if (isMatch) { + taskFound = true; + } + return !isMatch; + }); + + if (!taskFound) { + throw new Error("Task not found."); + } + + existing.tasks.push(updatedTaskDataSetJSON); + + await boardSets.delete(boardTitle); + const newtaskDataSet = await boardSets.insert(existing); + + res.status(201).json({ + title: taskDataSet.newTitle, + description: taskDataSet.newDescription, + dueDate: taskDataSet.newDueDate, + owner: taskDataSet.owner, + success: true + }); + } catch (err) { + res.status(503).json({ error: err.message }); + } +}); export default router; From 3fa2a46ff1ba4c120e07766867c74c4bddc24024 Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 21:30:06 +0100 Subject: [PATCH 14/15] forgot to add package.json --- server/package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/server/package.json b/server/package.json index a3252c5..1aecacb 100644 --- a/server/package.json +++ b/server/package.json @@ -15,12 +15,14 @@ "deta": "^1.1.0", "dotenv": "^16.0.3", "express": "^4.18.1", + "jsonwebtoken": "^9.0.2", "nodemailer": "^6.9.7", "outlook-nodemailer-transport": "^1.4.1" }, "devDependencies": { "@types/cors": "^2.8.13", "@types/express": "^4.17.13", + "@types/jsonwebtoken": "^9.0.5", "@types/node": "14", "@types/nodemailer": "^6.4.14", "@typescript-eslint/eslint-plugin": "^5.47.0", From 29175b08a356d00569da4596038f9d98f7a4500e Mon Sep 17 00:00:00 2001 From: sanqro Date: Wed, 20 Dec 2023 22:04:08 +0100 Subject: [PATCH 15/15] add endpoint to get specific board Co-authored-by: Niels Brunokowski <77078828+Neox420@users.noreply.github.com> Co-authored-by: RelxOff --- server/src/routes/board.ts | 62 ++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/server/src/routes/board.ts b/server/src/routes/board.ts index a2ed3cb..01457bd 100644 --- a/server/src/routes/board.ts +++ b/server/src/routes/board.ts @@ -30,8 +30,6 @@ router.post("/create", async (req, res) => { if (await boardSets.get(boardDataSet.title)) { throw new Error("This board name exists already. Please try to edit this!"); - } else if (await boardSets.get(key)) { - throw new Error("This task name exists already. Please try to edit this!"); } const tasks: ITask[] = []; @@ -56,34 +54,15 @@ router.post("/create", async (req, res) => { } }); - -router.get("/getPublic", async (req, res) => { - try { - const fetchedBoards = await boardSets.fetch({ public: true }); - if (fetchedBoards == null) { - res.status(409).json({ - error: "This task does not exist." - }); - return false; - } else { - res.status(201).json({ fetchedBoards }); - } - } catch (err) { - res.status(503).json({ error: err.message }); - } -}); - - router.post("/addPublicBoard", async (req, res) => { try { const addPublicBoardData: IAddPublicBoard = req.body as IAddPublicBoard; - if (!(await boardSets.get(addPublicBoardData.title))) { throw new Error("This board does not exist"); } - if (await publicBoards.get(addPublicBoardData.owner + addPublicBoardData.title) !== null) { + if ((await publicBoards.get(addPublicBoardData.owner + addPublicBoardData.title)) !== null) { throw new Error("You already added this board to your public boards."); } @@ -93,7 +72,7 @@ router.post("/addPublicBoard", async (req, res) => { title: addPublicBoardData.title }; - await publicBoards.insert(JSON.parse(JSON.stringify(addPublicBoardDataJSON))); + await publicBoards.insert(JSON.parse(JSON.stringify(addPublicBoardDataJSON))); res.status(201).json({ title: addPublicBoardData.title, @@ -105,8 +84,7 @@ router.post("/addPublicBoard", async (req, res) => { } }); - -router.get("/getAll/", async (req, res) => { +router.get("/getAll", async (req, res) => { try { const parameters: IGetPublicBoards = req.body as IGetPublicBoards; const username = parameters.username; @@ -116,18 +94,20 @@ router.get("/getAll/", async (req, res) => { for (let fetchedPublicBoard of fetchedPublicBoards.items) { if (parameters.passwords != null) { for (let password of parameters.passwords) { - const board = await boardSets.get(fetchedPublicBoard.key.replace(fetchedPublicBoard.owner, '')); + const board = await boardSets.get( + fetchedPublicBoard.key.replace(fetchedPublicBoard.owner, "") + ); if (password.title === board.key) { if (await argon2.verify(board.password as string, password.password)) { delete board.password; fetchedBoards.items.push(board); - } + } } } } } - for(let i = 0; i < fetchedBoards.count; i++) { - if(fetchedBoards.items[i].public) { + for (let i = 0; i < fetchedBoards.count; i++) { + if (fetchedBoards.items[i].public) { delete fetchedBoards.items[i].password; } } @@ -143,4 +123,28 @@ router.get("/getAll/", async (req, res) => { res.status(503).json({ error: err.message }); } }); + +router.get("/get/:board/:password", async (req, res) => { + try { + const board = req.params.board; + const password = req.params.password; + const fetchedBoard = await boardSets.get(board); + + if ( fetchedBoard == null) { + throw new Error("This board does not exist."); + } + if (fetchedBoard.public) { + console.log(fetchedBoard.password); + if (await argon2.verify(fetchedBoard.password as string, password)) { + delete fetchedBoard.password; + res.status(201).json({ fetchedBoard }); + } else { + throw new Error("Wrong credentials."); + } + } + } catch (err) { + res.status(503).json({ error: err.message }); + } +}); + export default router;