diff --git a/backend/src/models/article.js b/backend/src/article/article.js similarity index 96% rename from backend/src/models/article.js rename to backend/src/article/article.js index 9beb7ba49e5da1024e09d5e021fd8ac6f16e7214..4a233f5aa3e3fc910a77e3700ba77bc3d8f02d20 100644 --- a/backend/src/models/article.js +++ b/backend/src/article/article.js @@ -1,5 +1,5 @@ import mongoose from 'mongoose'; -import UserSchema from './User.js'; +import UserSchema from '../user/user.js'; const CommentSchema = new mongoose.Schema({ content: { diff --git a/backend/src/article/articleController.js b/backend/src/article/articleController.js new file mode 100644 index 0000000000000000000000000000000000000000..921590e90436eb3385e253208f5a9fe76a4f1807 --- /dev/null +++ b/backend/src/article/articleController.js @@ -0,0 +1,89 @@ +import express from 'express'; +import multer from 'multer'; +import path from 'path' +import moment from 'moment' + +import userService from '../user/userService.js'; +import articleService from './articleService.js'; + + + +const __dirname = path.resolve(); + +export const router = express.Router(); + +const upload = multer({ + storage: multer.diskStorage({ // ���ν븳怨듦컙 �뺣낫 : �섎뱶�붿뒪�ъ뿉 ���� + destination(req, file, done) { // ���� �꾩튂 + done(null, 'public/uploads/'); // uploads�쇰뒗 �대뜑 �덉뿉 ���� + }, + filename(req, file, done) { // �뚯씪紐낆쓣 �대뼡 �대쫫�쇰줈 �щ┫吏� + const ext = path.extname(file.originalname); // �뚯씪�� �뺤옣�� + done(null, path.basename(file.originalname, ext) + Date.now() + ext); // �뚯씪�대쫫 + �좎쭨 + �뺤옣�� �대쫫�쇰줈 ���� + } + }), + limits: { fileSize: 5 * 1024 * 1024 } // 5硫붽�濡� �⑸웾 �쒗븳 +}); + +router.post("/", upload.array("img"), async function (req, res, next) { + if (!req.session.sessionid) { + console.log("No session error"); + return; + } + console.log("POST /article"); + + const inputTitle = req.body.title + const inputContent = req.body.content + const inputImage = req.files.map(el => el.path); + const useremail = req.session.sessionid.email + const author = await userService.findUserByEmail(useremail); + const inputkeyword = req.body.keyword + const inputlat = req.body.latitude + const inputlng = req.body.longitude + + await articleService.createArticle({ + title: inputTitle, + content: inputContent, + imageUrls: inputImage, + author: author, + keyword: inputkeyword, + latitude: inputlat, + longitude: inputlng, + comments: [], + likes: [], + }); + + console.log('saved.') + res.send(); +}); + + +router.get("/", async (req, res) => { + console.log(path.join(process.cwd(), '/public')) + if (!req.session.sessionid) { + console.log("No session"); + } + const articles = await articleService.findAllArticle(); + articles.forEach((article) => { + article.imageUrls.forEach( + (urls) => { + try { + //res.sendFile(path.join(__dirname, urls)); + } + catch (err) { + console.log(err) + } + }); + }); + res.send(JSON.stringify(articles)); +}); + +router.get("/:id", async (req, res) => { + if (!req.session.sessionid) { + console.log("No session"); + } + const articles = await articleService.findArticleById(req.params.id); + res.send(JSON.stringify(articles)); +}); + +export default router; diff --git a/backend/src/data/articleService.js b/backend/src/article/articleService.js similarity index 97% rename from backend/src/data/articleService.js rename to backend/src/article/articleService.js index 6d1eebbe4e95903e34cc176b754cae3117aba1af..ff1cd667691b26dd4d574a854541bb9df5a10e2b 100644 --- a/backend/src/data/articleService.js +++ b/backend/src/article/articleService.js @@ -1,4 +1,4 @@ -import Article from '../models/article.js'; +import Article from './article.js'; const articleService = { async createArticle(articleData) { diff --git a/backend/src/auth.js b/backend/src/auth/authController.js similarity index 94% rename from backend/src/auth.js rename to backend/src/auth/authController.js index fdac48089d6ab2c6cbf1640458562323b3de65a7..4c63e5f4cb1e9ed6026f001c2f57a67077d991db 100644 --- a/backend/src/auth.js +++ b/backend/src/auth/authController.js @@ -1,6 +1,6 @@ import express from 'express'; import moment from 'moment'; -import userService from './data/userService.js'; +import userService from '../user/userService.js'; export const router = express.Router(); @@ -52,7 +52,7 @@ router.get("/logout", (req, res) => { } }); -router.get("/check", (req, res) => { +router.get("/session", (req, res) => { if(req.session.sessionid){ res.send(true); } diff --git a/backend/src/db.js b/backend/src/db.js index 539799b6345742235f017623c2629e8249975aa2..24e8546bd71aca65f942b29c436357f63e5adca3 100644 --- a/backend/src/db.js +++ b/backend/src/db.js @@ -3,81 +3,6 @@ import mongoose from 'mongoose'; dotenv.config(); -// const GoogleProviderSchema = new mongoose.Schema({ -// id: { -// type: String, -// required: true, -// //unique: true, -// }, -// profileUrl: { -// type: String, -// }, -// }); - -// const UserSchema = new mongoose.Schema({ -// nickname: { -// type: String, -// }, -// email: { -// type: String, -// //unique: true, -// }, -// google: { -// type: GoogleProviderSchema, -// } -// }); - - -// const UserModel = mongoose.model("User", UserSchema); - -// const CommentSchema = new mongoose.Schema({ -// content: { -// type: String, -// required: true, -// }, -// author: { -// type: UserSchema, -// required: true, -// }, -// createdAt: { -// type: Date, -// default: Date.now, -// }, -// }); -// CommentSchema.index( { commentId: 1 } , { sparse: true } ) - -// const ArticleSchema = new mongoose.Schema({ -// title: { -// type: String, -// required: true, -// }, -// content: { -// type: String, -// required: true, -// }, -// imageUrls: { -// type: [String], -// }, -// author: { -// type: UserSchema, -// required: true, -// }, -// comments: { -// type: [CommentSchema], -// unique: false, -// }, -// likes: { -// type: [UserSchema], -// }, -// createdAt: { -// type: Date, -// default: Date.now, -// }, -// }); - -// ArticleSchema.index({articleId:1}, { sparse: true }) -// const ArticleModel = mongoose.model("Article", ArticleSchema); - const connectDB = async () => { const url = process.env.MONGODB_URI; try { diff --git a/backend/src/index.js b/backend/src/index.js index 0e7f8b724572e3ecea8abbdcc05978a75c49110c..e6d328579bd181d56c56a6747b2e9aa4925aec1d 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -6,8 +6,8 @@ import cookieParser from 'cookie-parser'; import session from 'express-session'; -import auth from './auth.js'; -import post from './post.js'; +import auth from './auth/authController.js'; +import article from './article/articleController.js'; import connectDB from './db.js'; @@ -51,4 +51,4 @@ app.listen(PORT, () => { app.use('/auth', auth); -app.use('/post', post); \ No newline at end of file +app.use('/article', article); \ No newline at end of file diff --git a/backend/src/post.js b/backend/src/post.js deleted file mode 100644 index ef00d01f3bc5cd3f11c0ace69d4b1c7292a68dbe..0000000000000000000000000000000000000000 --- a/backend/src/post.js +++ /dev/null @@ -1,97 +0,0 @@ -import express from 'express'; -import multer from 'multer'; -import path from 'path' -import moment from 'moment' - -import userService from './data/userService.js'; -import articleService from './data/articleService.js'; - - - -const __dirname = path.resolve(); - -export const router = express.Router(); - -const upload = multer({ - storage: multer.diskStorage({ // ���ν븳怨듦컙 �뺣낫 : �섎뱶�붿뒪�ъ뿉 ���� - destination(req, file, done) { // ���� �꾩튂 - done(null, 'public/uploads/'); // uploads�쇰뒗 �대뜑 �덉뿉 ���� - }, - filename(req, file, done) { // �뚯씪紐낆쓣 �대뼡 �대쫫�쇰줈 �щ┫吏� - const ext = path.extname(file.originalname); // �뚯씪�� �뺤옣�� - done(null, path.basename(file.originalname, ext) + Date.now() + ext); // �뚯씪�대쫫 + �좎쭨 + �뺤옣�� �대쫫�쇰줈 ���� - } - }), - limits: { fileSize: 5 * 1024 * 1024 } // 5硫붽�濡� �⑸웾 �쒗븳 - }); - -router.post("/upload", upload.array("img"), async function(req, res, next) { - if(!req.session.sessionid){ - // �몄뀡�� �놁뿁 - } - console.log("�ъ뒪�� '�댁쨾'"); - - const inputTitle = req.body.title - const inputContent = req.body.content - const inputImage = req.files.map(el => el.path); - const useremail = req.session.sessionid.email - const author = await userService.findUserByEmail(useremail); - const inputkeyword = req.body.keyword - const inputlat = req.body.latitude - const inputlng = req.body.longitude - // const currentTime = moment().format('YYYY-MM-DD HH:mm') - - await articleService.createArticle({ - title: inputTitle, - content: inputContent, - imageUrls: inputImage, - author: author, - keyword: inputkeyword, - latitude: inputlat, - longitude: inputlng, - comments: [], - likes: [], - }); - - console.log('saved.') - res.send(); - res.send(); - }); - - -router.get("/loadarticle", async (req, res) => { - console.log(path.join(process.cwd(), '/public')) - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const articles = await articleService.findAllArticle(); - articles.forEach((article) => { - article.imageUrls.forEach( - (urls) => { - try{ - //res.sendFile(path.join(__dirname, urls)); - } - catch(err){ - console.log(err) - } - }); - }); - res.send(JSON.stringify(articles)); -}); - -router.get("/loadarticle/:id", async (req, res) => { - - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const articles = await articleService.findArticleById(req.params.id); - res.send(JSON.stringify(articles)); -}); - -export default router; diff --git a/backend/src/models/user.js b/backend/src/user/user.js similarity index 100% rename from backend/src/models/user.js rename to backend/src/user/user.js diff --git a/backend/src/data/userService.js b/backend/src/user/userService.js similarity index 94% rename from backend/src/data/userService.js rename to backend/src/user/userService.js index e0851bc19dc6a2f315a849de7e258673eb51a174..c3bf5735255a266d49c071675ac40b2e2f6808d3 100644 --- a/backend/src/data/userService.js +++ b/backend/src/user/userService.js @@ -1,4 +1,4 @@ -import User from '../models/User.js'; +import User from './user.js'; const userService = { async createUser(userData) { diff --git a/frontend/src/App.js b/frontend/src/App.js index 5224d06201beb6c3dc7b120871462450edca6e5a..3b4d66c4418c73e24b71c8a7843f030b3efa0225 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -84,7 +84,7 @@ async function requestLogout() { async function requestCheckSession() { const response = await axios({ - url: 'http://localhost:8080/auth/check', // �듭떊�� �밸Ц�� + url: 'http://localhost:8080/auth/session', // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; diff --git a/frontend/src/pages/Main.js b/frontend/src/pages/Main.js index e467d0f562490c050f994606df10ef6e7705a30b..e1dc2fbb0ee07a707c865078b798dc2ea3757cd2 100644 --- a/frontend/src/pages/Main.js +++ b/frontend/src/pages/Main.js @@ -52,7 +52,7 @@ function Main() { async function requestLoadArticle() { const response = await axios({ - url: 'http://localhost:8080/post/loadarticle', // �듭떊�� �밸Ц�� + url: 'http://localhost:8080/article', // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; diff --git a/frontend/src/pages/PostRead.js b/frontend/src/pages/PostRead.js index 96390a3bb24fbee1252f86eb45e341d209bc29a6..188acb07695deb3ea94e99d429dc4f383b3cd474 100644 --- a/frontend/src/pages/PostRead.js +++ b/frontend/src/pages/PostRead.js @@ -81,7 +81,7 @@ function PostRead() { async function requestLoadArticleById(id) { const response = await axios({ - url: `http://localhost:8080/post/loadarticle/${id}`, // �듭떊�� �밸Ц�� + url: `http://localhost:8080/article/${id}`, // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; diff --git a/frontend/src/pages/PostWrite.js b/frontend/src/pages/PostWrite.js index 58259a9dfd09f9caf86baa9cb18c02f3dbdda098..b13e4f37cb9d7c4d8f2a8de498bdac5698f4b337 100644 --- a/frontend/src/pages/PostWrite.js +++ b/frontend/src/pages/PostWrite.js @@ -114,7 +114,7 @@ function PostWrite(){ formData.append("latitude", location.center.lat); formData.append("longitude", location.center.lng); axios - .post("http://localhost:8080/post/upload", formData, + .post("http://localhost:8080/article", formData, { headers: {"Content-Type": "multipart/form-data"} })