diff --git a/backend/app.js b/backend/app.js index 005909a..7674e58 100644 --- a/backend/app.js +++ b/backend/app.js @@ -11,6 +11,7 @@ require("dotenv").config(); const indexRouter = require('./src/routes/index'); const userRouter = require('./src/routes/user'); const authRouter = require('./src/routes/auth'); +const teamRouter = require('./src/routes/team'); const app = express(); @@ -29,5 +30,6 @@ require('./src/configs/passport')(); app.use('/', indexRouter); app.use('/user', userRouter); app.use('/auth', authRouter); +app.use('/study',teamRouter); module.exports = app; diff --git a/backend/bin/www b/backend/bin/www index 2dab2d1..cae08e3 100644 --- a/backend/bin/www +++ b/backend/bin/www @@ -9,10 +9,9 @@ const debug = require('debug')('backend:server'); const http = require('http'); const mongoose = require('mongoose'); -mongoose.connect(process.env.DATABASE_URL,{ - useNewUrlParser: true, - useUnifiedTopology: true, -}) +require('dotenv').config(); + +mongoose.connect(process.env.DATABASE_URL) .then(res => console.log("MongoDB Connected")) .catch(err => console.error(err)); diff --git a/backend/src/models/user.js b/backend/src/models/user.js index 234ac2d..ded305e 100644 --- a/backend/src/models/user.js +++ b/backend/src/models/user.js @@ -6,8 +6,7 @@ const userSchema = new Schema({ nickName: String, email: String, teamIds: Array, - oauthToken : String, - githubId: String + githubId : String, }); module.exports = mongoose.model('User', userSchema); \ No newline at end of file diff --git a/backend/src/routes/team.js b/backend/src/routes/team.js new file mode 100644 index 0000000..e1b5532 --- /dev/null +++ b/backend/src/routes/team.js @@ -0,0 +1,13 @@ +const {createTeam, deleteTeam, updateTeam, searchTeams} = require("../services/team/team"); +const express = require('express'); +const router = express.Router(); + +router.get('/',searchTeams); + +router.post('/',createTeam); + +router.put('/:teamId',updateTeam); + +router.delete('/:teamId',deleteTeam); + +module.exports = router; \ No newline at end of file diff --git a/backend/src/services/team/team.js b/backend/src/services/team/team.js new file mode 100644 index 0000000..fe5e084 --- /dev/null +++ b/backend/src/services/team/team.js @@ -0,0 +1,88 @@ +const Team = require("../../models/team"); + +const createTeam = async (req, res, next) => { + try{ + const result = await Team.create({...req.body}); + await res.status(201).json({ + code: '2001', + status: '성공 : Team 생성', + message: 'Team이 정상적으로 생성되었습니다.', + id: result["_id"] + }); + } + catch(err){ + console.error(err); + await res.status(500).json({ + code: '5000', + status: '에러 : 서버 에러', + message : '생성 요청 처리 중 서버에서 문제가 발생했습니다.' + }); + } +}; + + +const deleteTeam = async (req, res, next) => { + try{ + const teamId = req.params['teamId']; + await Team.findByIdAndDelete(teamId); + await res.status(200).json({ + code: '2000', + status: '성공 : Team 삭제', + message: 'Team이 정상적으로 삭제되었습니다.' + }); + } + catch(err){ + console.error(err); + await res.status(500).json({ + code: '5000', + status: '에러 : 서버 에러', + message : '삭제 요청 처리 중 서버에서 문제가 발생했습니다.' + }); + } +}; + +const updateTeam = async (req, res, next) => { + try{ + const teamId = req.params['teamId']; + if(req.body['userIds'] !== undefined) throw new Error("userIds는 접근할 수 없습니다."); + await Team.findByIdAndUpdate(teamId,{...req.body}); + await res.status(200).json({ + code: '2000', + status: '성공 : Team 수정', + message: 'Team이 정상적으로 수정되었습니다.' + }); + } + catch(err){ + console.error(err); + await res.status(500).json({ + code: '5000', + status: '에러 : 서버 에러', + message : `${err}` + }); + } +}; + +const searchTeams = async (req, res, next) => { + try{ + let title = req.query["title"]; + if(title === undefined) title = ''; + const teams = await Team.find({title: new RegExp(`${title}`,'i')}).exec(); + + await res.status(200).json({ + code: '2000', + status: '성공 : Team 검색', + message: 'Team이 정상적으로 검색되었습니다.', + study: teams + }); + } + catch(err){ + console.error(err); + await res.status(500).json({ + code: '5000', + status: '에러 : 서버 에러', + message : '검색 요청 처리 중 서버에서 문제가 발생했습니다.' + }); + } +}; + +module.exports = { createTeam, deleteTeam, updateTeam, searchTeams }; \ No newline at end of file