diff --git a/client/src/pages/account.tsx b/client/src/pages/account.tsx index 37341f5..d2865e1 100644 --- a/client/src/pages/account.tsx +++ b/client/src/pages/account.tsx @@ -5,14 +5,15 @@ import {SAPIBase} from "../tools/api"; import "./css/account.css"; const AccountPage = () => { - const [ SAPIKEY, setSAPIKEY ] = React.useState(""); + const [ ID, setID ] = React.useState(""); + const [ PW, setPW ] = React.useState(""); const [ NBalance, setNBalance ] = React.useState("Not Authorized"); const [ NTransaction, setNTransaction ] = React.useState(0); const getAccountInformation = () => { const asyncFun = async() => { interface IAPIResponse { balance: number }; - const { data } = await axios.post(SAPIBase + '/account/getInfo', { credential: SAPIKEY }); + const { data } = await axios.post(SAPIBase + '/account/getInfo', { id: ID, pw: PW }); setNBalance(data.balance); } asyncFun().catch((e) => window.alert(`AN ERROR OCCURED: ${e}`)); @@ -22,7 +23,7 @@ const AccountPage = () => { const asyncFun = async() => { if (amount === '') return; interface IAPIResponse { success: boolean, balance: number, msg: string }; - const { data } = await axios.post(SAPIBase + '/account/transaction', { credential: SAPIKEY, amount: amount }); + const { data } = await axios.post(SAPIBase + '/account/transaction', { id: ID, pw: PW, amount: amount }); setNTransaction(0); if (!data.success) { window.alert('Transaction Failed:' + data.msg); @@ -40,7 +41,8 @@ const AccountPage = () => {

Account

- Enter API Key: setSAPIKEY(e.target.value)}/> + Enter ID: setID(e.target.value)}/> + Enter PW: setPW(f.target.value)}/>
diff --git a/client/src/pages/css/feed.css b/client/src/pages/css/feed.css index 53580df..627046f 100644 --- a/client/src/pages/css/feed.css +++ b/client/src/pages/css/feed.css @@ -38,7 +38,19 @@ font-size: 18px; } +.edit-item { + position: absolute; + right: 15px; + top: 30px; + color: red; + font-size: 18px; +} + .delete-item:hover { font-weight: bold; cursor: pointer; +} + +.editinput { + margin-bottom: 20px; } \ No newline at end of file diff --git a/client/src/pages/feed.tsx b/client/src/pages/feed.tsx index b64c53b..4ee5a3a 100644 --- a/client/src/pages/feed.tsx +++ b/client/src/pages/feed.tsx @@ -4,13 +4,16 @@ import { SAPIBase } from "../tools/api"; import Header from "../components/header"; import "./css/feed.css"; -interface IAPIResponse { id: number, title: string, content: string } +interface IAPIResponse { id: number, title: string, content: string, joayo: number } + const FeedPage = (props: {}) => { const [ LAPIResponse, setLAPIResponse ] = React.useState([]); const [ NPostCount, setNPostCount ] = React.useState(0); const [ SNewPostTitle, setSNewPostTitle ] = React.useState(""); const [ SNewPostContent, setSNewPostContent ] = React.useState(""); + const [ EditDummy, setEditDummy ] = React.useState(0); + const [ EditContent, setEditContent ] = React.useState(""); React.useEffect( () => { let BComponentExited = false; @@ -23,7 +26,7 @@ const FeedPage = (props: {}) => { }; asyncFun().catch((e) => window.alert(`Error while running API Call: ${e}`)); return () => { BComponentExited = true; } - }, [ NPostCount ]); + }, [ NPostCount, EditDummy]); const createNewPost = () => { const asyncFun = async () => { @@ -44,6 +47,23 @@ const FeedPage = (props: {}) => { asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)); } + const editPost = (id: string, content:string) => { + const asyncFun = async () => { + // One can set X-HTTP-Method header to DELETE to specify deletion as well + await axios.post( SAPIBase + '/feed/editFeed', { id: id, content: content } ); + setEditDummy(EditDummy + 1); + } + asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)); + setEditContent(""); + } + const plusJoayo = (id: string) => { + const asyncFun = async () => { + // One can set X-HTTP-Method header to DELETE to specify deletion as well + await axios.post( SAPIBase + '/feed/joayo', { id: id } ); + setEditDummy(EditDummy+1); + } + asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)); + } return (
@@ -59,7 +79,15 @@ const FeedPage = (props: {}) => {
deletePost(`${val.id}`)}>ⓧ

{ val.title }

+
+ +

{ `${val.joayo}` }

+

{ val.content }

+
+ setEditContent(e.target.value)} placeholder="Wanna Edit?" /> + +
) }
diff --git a/seminar/.env.example b/seminar/.env.example index f84e5b2..fc72051 100644 --- a/seminar/.env.example +++ b/seminar/.env.example @@ -1,3 +1,4 @@ PORT=8080 NODE_ENV=DEVELOPMENT -API_KEY= \ No newline at end of file +API_KEY= +ACCOUNT={"id" : "", "pw": "" } \ No newline at end of file diff --git a/seminar/package-lock.json b/seminar/package-lock.json index 7118cb7..2d0931a 100644 --- a/seminar/package-lock.json +++ b/seminar/package-lock.json @@ -11,7 +11,7 @@ "dependencies": { "cors": "^2.8.5", "dotenv": "^16.0.0", - "ejs": "^3.1.7", + "ejs": "^3.1.10", "express": "^4.17.3" }, "devDependencies": { @@ -510,9 +510,9 @@ "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, "node_modules/ejs": { - "version": "3.1.7", - "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.7.tgz", - "integrity": "sha512-BIar7R6abbUxDA3bfXrO4DSgwo8I+fB5/1zgujl3HLLjwd6+9iOnrT+t3grn2qbk9vOgBubXOFwX2m9axoFaGw==", + "version": "3.1.10", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.10.tgz", + "integrity": "sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA==", "dependencies": { "jake": "^10.8.5" }, @@ -2239,9 +2239,9 @@ "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" }, "ejs": { - "version": "3.1.7", - "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.7.tgz", - "integrity": "sha512-BIar7R6abbUxDA3bfXrO4DSgwo8I+fB5/1zgujl3HLLjwd6+9iOnrT+t3grn2qbk9vOgBubXOFwX2m9axoFaGw==", + "version": "3.1.10", + "resolved": "https://registry.npmjs.org/ejs/-/ejs-3.1.10.tgz", + "integrity": "sha512-UeJmFfOrAQS8OJWPZ4qtgHyWExa088/MtK5UEyoJGFH67cDEXkZSviOiKRCZ4Xij0zxI3JECgYs3oKx+AizQBA==", "requires": { "jake": "^10.8.5" } diff --git a/seminar/package.json b/seminar/package.json index c7b1c5c..129294d 100644 --- a/seminar/package.json +++ b/seminar/package.json @@ -13,7 +13,7 @@ "dependencies": { "cors": "^2.8.5", "dotenv": "^16.0.0", - "ejs": "^3.1.7", + "ejs": "^3.1.10", "express": "^4.17.3" }, "devDependencies": { diff --git a/seminar/src/middleware/auth.js b/seminar/src/middleware/auth.js index 480f41d..2676480 100644 --- a/seminar/src/middleware/auth.js +++ b/seminar/src/middleware/auth.js @@ -1,6 +1,9 @@ const authMiddleware = (req, res, next) => { - if (req.body.credential === process.env.API_KEY) { + const json = process.env.ACCOUNT; + const obj = JSON.parse(json); + if (req.body.id === obj.id && req.body.pw === obj.pw) { console.log("[AUTH-MIDDLEWARE] Authorized User"); + console.log(obj.id); next(); } else { diff --git a/seminar/src/routes/feed.js b/seminar/src/routes/feed.js index f9c19d9..ca0d0aa 100644 --- a/seminar/src/routes/feed.js +++ b/seminar/src/routes/feed.js @@ -9,10 +9,12 @@ class FeedDB { return FeedDB._inst_; } - #id = 1; #itemCount = 1; #LDataDB = [{ id: 0, title: "test1", content: "Example body" }]; + #id = 1; #itemCount = 1; #LDataDB = [{ id: 0, title: "test1", content: "Example body", joayo: 0}]; constructor() { console.log("[Feed-DB] DB Init Completed"); } + + selectItems = ( count ) => { if (count > this.#itemCount) return { success: false, data: "Too many items queried" }; if (count < 0) return { success: false, data: "Invalid count provided" }; @@ -21,11 +23,16 @@ class FeedDB { insertItem = ( item ) => { const { title, content } = item; - this.#LDataDB.push({ id: this.#id, title, content }); + this.#LDataDB.push({ id: this.#id, title, content, joayo: 0 }); this.#id++; this.#itemCount++; return true; } + editItem = ( id, content ) => { + this.#LDataDB.find(x => x.id == parseInt(id)).content = content; + return true; + } + deleteItem = ( id ) => { let BItemDeleted = false; this.#LDataDB = this.#LDataDB.filter((value) => { @@ -36,6 +43,11 @@ class FeedDB { if (BItemDeleted) id--; return BItemDeleted; } + + joayo = ( id ) => { + this.#LDataDB.find(x => x.id == parseInt(id)).joayo += 1; + return true; + } } const feedDBInst = FeedDB.getInst(); @@ -62,6 +74,18 @@ router.post('/addFeed', (req, res) => { } }); +router.post('/editFeed', (req, res) => { + + try { + const { id, content } = req.body; + const editResult = feedDBInst.editItem(id, content); + if (!editResult) return res.status(500).json({ error: dbRes.data }) + else return res.status(200).json({ isOK: true }); + } catch (e) { + return res.status(500).json({ error: e }); + } + }); + router.post('/deleteFeed', (req, res) => { try { const { id } = req.body; @@ -73,4 +97,15 @@ router.post('/deleteFeed', (req, res) => { } }) + +router.post('/joayo', (req, res) => { + try { + const { id } = req.body; + const joayoResult = feedDBInst.joayo(id); + if (!joayoResult) return res.status(500).json({ error: "Joayo Failed.." }) + else return res.status(200).json({ isOK: true }); + } catch (e) { + return res.status(500).json({ error: e }); + } +}) module.exports = router; \ No newline at end of file