From e87fad64de34b29d980f9b605e579285b79792a8 Mon Sep 17 00:00:00 2001 From: BallisticTurtles <36849344+BallisticTurtles@users.noreply.github.com> Date: Sun, 28 Jul 2024 20:39:32 +0900 Subject: [PATCH 1/3] Backend Seminar Homework #2 --- client/src/pages/account.tsx | 12 ++++++++---- seminar/.env.example | 3 ++- seminar/src/middleware/auth.js | 5 ++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/client/src/pages/account.tsx b/client/src/pages/account.tsx index 37341f5..0504fbf 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 [ SUSERID, setSUSERID ] = React.useState(""); + const [ SUSERPW, setSUSERPW ] = 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', { credential: {SUSERID, SUSERPW}}) 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', { credential: {SUSERID, SUSERPW}, amount: amount }); setNTransaction(0); if (!data.success) { window.alert('Transaction Failed:' + data.msg); @@ -40,7 +41,10 @@ const AccountPage = () => {

Account

- Enter API Key: setSAPIKEY(e.target.value)}/> + Enter User ID: setSUSERID(e.target.value)}/> +
+ Enter User Password: setSUSERPW(e.target.value)}/> +
diff --git a/seminar/.env.example b/seminar/.env.example index ab11afa..0d4ea1c 100644 --- a/seminar/.env.example +++ b/seminar/.env.example @@ -1,4 +1,5 @@ PORT=8080 NODE_ENV=DEVELOPMENT -API_KEY= +USER_ID=mightyian03 +USER_PW=password MONGO_URI="mongodb://localhost:27017/todos" \ No newline at end of file diff --git a/seminar/src/middleware/auth.js b/seminar/src/middleware/auth.js index 480f41d..a001978 100644 --- a/seminar/src/middleware/auth.js +++ b/seminar/src/middleware/auth.js @@ -1,9 +1,8 @@ const authMiddleware = (req, res, next) => { - if (req.body.credential === process.env.API_KEY) { + if (req.body.credential.SUSERID === process.env.USER_ID && req.body.credential.SUSERPW === process.env.USER_PW) { console.log("[AUTH-MIDDLEWARE] Authorized User"); next(); - } - else { + } else { console.log("[AUTH-MIDDLEWARE] Not Authorized User"); res.status(401).json({ error: "Not Authorized" }); } From c49bc096da041f4ec9959f9f88e0bccfb24fb190 Mon Sep 17 00:00:00 2001 From: BallisticTurtles <36849344+BallisticTurtles@users.noreply.github.com> Date: Tue, 30 Jul 2024 04:59:09 +0900 Subject: [PATCH 2/3] Backend Seminar Homework #1 --- client/src/pages/css/feed.css | 15 ++++++++++++++- client/src/pages/feed.tsx | 17 ++++++++++++++++- seminar/src/routes/feed.js | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/client/src/pages/css/feed.css b/client/src/pages/css/feed.css index 53580df..dfbf5b1 100644 --- a/client/src/pages/css/feed.css +++ b/client/src/pages/css/feed.css @@ -30,12 +30,25 @@ font-weight: 700; } +.edit-item { + position: absolute; + right: 20px; + top: 12px; + color: green; + font-size: 18px; +} + +.edit-item:hover { + font-weight: bold; + cursor: pointer; +} + .delete-item { position: absolute; right: 15px; top: 12px; color: red; - font-size: 18px; + font-size: 50px; } .delete-item:hover { diff --git a/client/src/pages/feed.tsx b/client/src/pages/feed.tsx index fb70b65..b667a75 100644 --- a/client/src/pages/feed.tsx +++ b/client/src/pages/feed.tsx @@ -10,8 +10,10 @@ 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 [ SNewPostContent, setSNewPostContent ] = React.useState(""); const [ SSearchItem, setSSearchItem ] = React.useState(""); + const [ NEditPostID, setNEditPostID ] = React.useState(0); + const [ SEditPostContent, setSEditPostContent ] = React.useState(""); React.useEffect( () => { let BComponentExited = false; @@ -36,6 +38,15 @@ const FeedPage = (props: {}) => { asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)); } + const editPost = () => { + const asyncFun = async () => { + await axios.post( SAPIBase + '/feed/editFeed', {id: NEditPostID, content: SEditPostContent}); + setNEditPostID(NEditPostID + 1); + setSEditPostContent(""); + } + asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)) + } + const deletePost = (id: string) => { const asyncFun = async () => { // One can set X-HTTP-Method header to DELETE to specify deletion as well @@ -67,6 +78,10 @@ const FeedPage = (props: {}) => {
deletePost(`${val._id}`)}>ⓧ

{ val.title }

{ val.content }

+
+ setSEditPostContent(e.target.value)}/> + +
) }
diff --git a/seminar/src/routes/feed.js b/seminar/src/routes/feed.js index e9cd17d..8634894 100644 --- a/seminar/src/routes/feed.js +++ b/seminar/src/routes/feed.js @@ -44,6 +44,17 @@ class FeedDB { } } + editItem = async ( id, content ) => { + try { + const OEditFiler = { _id: id }; + const res = await FeedModel.updateOne(OEditFiler, content); + return true; + } catch (e) { + console.log(`[Feed-DB] Edit Error: ${ e }`); + return false; + } + } + deleteItem = async ( id ) => { try { const ODeleteFiler = { _id: id }; @@ -81,6 +92,17 @@ router.post('/addFeed', async (req, res) => { } }); +router.post('/editFeed', async (req, res) => { + try { + const { title, content } = req.body + const editResult = await feedDBInst.editItem(id); + 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', async (req, res) => { try { const { id } = req.body; From fd6c9e301138fb4f3fb54ed15d82eee0cb478342 Mon Sep 17 00:00:00 2001 From: BallisticTurtles <36849344+BallisticTurtles@users.noreply.github.com> Date: Tue, 30 Jul 2024 05:25:40 +0900 Subject: [PATCH 3/3] Backend Seminar Homework #3 --- client/src/pages/feed.tsx | 12 +++++++++++- seminar/src/routes/feed.js | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/client/src/pages/feed.tsx b/client/src/pages/feed.tsx index b667a75..e167598 100644 --- a/client/src/pages/feed.tsx +++ b/client/src/pages/feed.tsx @@ -4,7 +4,7 @@ import { SAPIBase } from "../tools/api"; import Header from "../components/header"; import "./css/feed.css"; -interface IAPIResponse { _id: string, title: string, content: string, itemViewCnt: number } +interface IAPIResponse { _id: string, title: string, content: string, itemViewCnt: number, star: binary} const FeedPage = (props: {}) => { const [ LAPIResponse, setLAPIResponse ] = React.useState([]); @@ -47,6 +47,13 @@ const FeedPage = (props: {}) => { asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)) } + const starPost = (id) => { + const asyncFun = async () => { + await axios.post( SAPIBase + '/feed/starFeed', {id: id} ); + } + asyncFun().catch(e => window.alert(`AN ERROR OCCURED! ${e}`)); + } + const deletePost = (id: string) => { const asyncFun = async () => { // One can set X-HTTP-Method header to DELETE to specify deletion as well @@ -82,6 +89,9 @@ const FeedPage = (props: {}) => { setSEditPostContent(e.target.value)}/>
+
+ +
) }
diff --git a/seminar/src/routes/feed.js b/seminar/src/routes/feed.js index 8634894..59ef6f3 100644 --- a/seminar/src/routes/feed.js +++ b/seminar/src/routes/feed.js @@ -55,6 +55,17 @@ class FeedDB { } } + starItem = async ( id, star ) => { + try { + const OStarFiler = { _id: id }; + const res = await FeedModel.updateOne(OStarFiler, star); + return true; + } catch (e) { + console.log(`[Feed-DB] Star Error: ${ e }`); + return false; + } + } + deleteItem = async ( id ) => { try { const ODeleteFiler = { _id: id }; @@ -103,6 +114,17 @@ router.post('/editFeed', async (req, res) => { } }); +router.post('/starFeed', async (req, res) => { + try { + const { title, content } = req.body + const starResult = await feedDBInst.starItem(id, star); + if(!starResult) 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', async (req, res) => { try { const { id } = req.body;