Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/#23 스터디 그룹을 위한 CRUD 기능 구현 #28

Merged
merged 9 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
7 changes: 3 additions & 4 deletions backend/bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
3 changes: 1 addition & 2 deletions backend/src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
13 changes: 13 additions & 0 deletions backend/src/routes/team.js
Original file line number Diff line number Diff line change
@@ -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;
88 changes: 88 additions & 0 deletions backend/src/services/team/team.js
Original file line number Diff line number Diff line change
@@ -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 };