diff --git a/backend/src/data/articleService.js b/backend/src/data/articleService.js new file mode 100644 index 0000000000000000000000000000000000000000..fa931f8bbd191afa29c020bbbaeab3c558f2b6fd --- /dev/null +++ b/backend/src/data/articleService.js @@ -0,0 +1,23 @@ +import Article from '../models/article.js'; + +const articleService = { + async createArticle(articleData) { + try { + const article = new Article(articleData); + await article.save(); + return article; + } catch (err) { + throw err; + } + }, + + async findArticleById(articleId) { + try { + + } catch (err) { + throw err; + } + } +}; + +export default articleService; \ No newline at end of file diff --git a/backend/src/data/userService.js b/backend/src/data/userService.js new file mode 100644 index 0000000000000000000000000000000000000000..ecc98789aa78dcede82649ef959c43c71a8aa362 --- /dev/null +++ b/backend/src/data/userService.js @@ -0,0 +1,24 @@ +import User from '../models/User.js'; + +const userService = { + async createUser(userData) { + try { + const user = new User(userData); + await user.save(); + return user; + } catch (err) { + throw err; + } + }, + + async findUserByEmail(email) { + try { + const user = await User.findOne({ email }); + return user; + } catch (err) { + throw err; + } + }, +}; + +export default userService; \ No newline at end of file diff --git a/backend/src/db.js b/backend/src/db.js index 608eb413c169e605f0a0f7cfaabe0d082872212b..539799b6345742235f017623c2629e8249975aa2 100644 --- a/backend/src/db.js +++ b/backend/src/db.js @@ -1,110 +1,91 @@ -import dotenv from "dotenv"; -import mongoose from "mongoose"; +import dotenv from 'dotenv'; +import mongoose from 'mongoose'; dotenv.config(); -const GoogleProviderSchema = new mongoose.Schema({ - id: { - type: String, - required: true, - //unique: true, - }, - profileUrl: { - type: String, - }, -}); +// const GoogleProviderSchema = new mongoose.Schema({ +// id: { +// type: String, +// required: true, +// //unique: true, +// }, +// profileUrl: { +// type: String, +// }, +// }); -const UserSchema = new mongoose.Schema({ - user_id: { - type: String, - required: true, - //unique: true, - }, - nickname: { - type: String, - }, - email: { - type: String, - //unique: true, - }, - google: { - type: GoogleProviderSchema, - } -}); +// const UserSchema = new mongoose.Schema({ +// nickname: { +// type: String, +// }, +// email: { +// type: String, +// //unique: true, +// }, +// google: { +// type: GoogleProviderSchema, +// } +// }); -const UserModel = mongoose.model("User", UserSchema); +// const UserModel = mongoose.model("User", UserSchema); -const CommentSchema = new mongoose.Schema({ - /* - commentId: { - type: String, - required: true, - unique: false, - }, - */ - content: { - type: String, - required: true, - }, - author: { - type: UserSchema, - required: true, - }, - createdAt: { - type: Date, - default: Date.now, - }, -}); -CommentSchema.index( { commentId: 1 } , { sparse: true } ) +// 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, - }, - /* - articleId: { - type: String, - unique: 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, - }, -}); +// 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); +// ArticleSchema.index({articleId:1}, { sparse: true }) +// const ArticleModel = mongoose.model("Article", ArticleSchema); const connectDB = async () => { const url = process.env.MONGODB_URI; - let db; try { - db = await mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }); + await mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }); + console.log('Database connected'); } catch (error) { - console.error(error); + console.error('Database connection failed',error); } - return db; -} +}; -export default { connectDB, UserModel, ArticleModel } \ No newline at end of file +export default connectDB; \ No newline at end of file diff --git a/backend/src/index.js b/backend/src/index.js index abd93ceca8509972dd14f82e033078a016ddbcb3..8f1972b089d37c158d451f954290e4fa0fcf0bbc 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -1,16 +1,18 @@ -import express from "express" -import cors from 'cors' -import path from 'path' -import process from 'process' +import express from 'express'; +import cors from 'cors'; +import path from 'path'; +import process from 'process'; import moment from 'moment'; import cookieParser from 'cookie-parser'; import session from 'express-session'; -import db from './db.js' +import connectDB from './db.js'; +import userService from './data/userService.js'; +import articleService from './data/articleService.js'; const app = express(); const PORT = 8080; -db.connectDB(); +connectDB(); app.use(express.static(path.join(process.cwd(), '../frontend/build'))); @@ -62,23 +64,20 @@ app.post('/login', async (req, res) => { TODO: 토큰의 무결성 체크 토큰이 이상이 없다면, 로그인/회원가입 로직을 수행 후 jwt 쿠키를 보낸다. */ - const expires = moment().add('60','m').toDate() + const expires = moment().add('60','m').toDate(); // 정보가 없다면 회원 가입 (강제?) - const user = await db.UserModel.find({ user_id: req.body.email }); - if (!user.length) { // 유저가 없다면 회원 가입 후 세션 생성 - - let userProfilePicture = req.body.picture || null - const userModel = new db.UserModel({ - user_id: req.body.email, - nickname: req.body.name, - email: req.body.email, - google: { - id: req.body.sub, - profileUrl: userProfilePicture, - }, - }); - await userModel.save(); + const user = await userService.findUserByEmail(req.body.email); + if (!user) { // 유저가 없다면 회원 가입 후 세션 생성 + let userProfilePicture = req.body.picture || null; + await userService.createUser({ + nickname: req.body.name, + email: req.body.email, + google: { + id: req.body.sub, + profileUrl: userProfilePicture, + }, + }); console.log('new user saved!') } console.log('login done') @@ -127,16 +126,15 @@ app.post("/post", async (req, res) => { const inputContent = req.body.content const inputImage = [] const useremail = req.session.sessionid.email - const author = await db.UserModel.find({ user_id: useremail }); - const articleModel = new db.ArticleModel({ + const author = await userService.findUserByEmail(useremail); + await articleService.createArticle({ title: inputTitle, content: inputContent, imageUrls: inputImage, author: author[0], comments: [], likes: [] - }); - await articleModel.save(); + }); console.log('saved.') res.send(); }); diff --git a/backend/src/models/article.js b/backend/src/models/article.js new file mode 100644 index 0000000000000000000000000000000000000000..e655c574a86300c5ff3b0d1f4151e0be4f58734b --- /dev/null +++ b/backend/src/models/article.js @@ -0,0 +1,53 @@ +import mongoose from 'mongoose'; +import UserSchema from './User.js'; + +const CommentSchema = new mongoose.Schema({ + content: { + type: String, + required: true, + }, + author: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', + required: true, + }, + createdAt: { + type: Date, + default: Date.now, + }, +}); + +const ArticleSchema = new mongoose.Schema({ + title: { + type: String, + required: true, + }, + content: { + type: String, + required: true, + }, + imageUrls: { + type: [String], + }, + author: { + type: mongoose.Schema.Types.ObjectId, + ref: 'User', + required: true, + }, + comments: { + type: [CommentSchema], + unique: false, + }, + likes: [{ + type: mongoose.Schema.Types.ObjectId, + ref: 'User', + }], + createdAt: { + type: Date, + default: Date.now, + }, +}); + +const Article = mongoose.model("Article", ArticleSchema); + +export default Article; \ No newline at end of file diff --git a/backend/src/models/user.js b/backend/src/models/user.js new file mode 100644 index 0000000000000000000000000000000000000000..44581fd28351ec47a30c5677539491b8ed153e99 --- /dev/null +++ b/backend/src/models/user.js @@ -0,0 +1,29 @@ +import mongoose from 'mongoose'; + +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 User = mongoose.model("User", UserSchema); + +export default User; \ No newline at end of file