diff --git a/controller/board.controller.js b/controller/board.controller.js index ff462ab..04761b4 100644 --- a/controller/board.controller.js +++ b/controller/board.controller.js @@ -1,5 +1,6 @@ import * as boardRepository from '../data/board.js'; -import * as userRepository from '../data/auth.js'; +import * as togetherRepository from '../data/together.js'; +import * as togetherController from './together.controller.js'; //게시글 생성 export async function createPost(req, res) { @@ -36,15 +37,19 @@ export async function deletePost(req, res){ export async function updatePost (req, res) { const id = req.params.id; const {title, contents, image, eventId, attendMembers} = req.body; + //제목이 없을시 에러 + if(title == '') + return res.status(400).json({message: 'title not found'}); + const updateId = await boardRepository.findByPostId(id); if(!updateId) {//해당 게시글이 없다면 - return res.sendStatus(404); + return res.status(404).json({message: 'Post not found'}); } if(updateId.writerId !== req.userId){ - return res.sendStatus(403); //로그인됐지만 권한없을때 + return res.status(401).json({message: 'UnAuthorization User'}); } - console.log("tkim"); - const updated = await boardRepository.updatePost(id, title, contents, image, eventId, attendMembers); - res.status(200).json(updated); + const updated = await boardRepository.updatePost({id, title, contents, image, eventId, attendMembers}); + res.status(200).json({updated}); } + diff --git a/controller/together.controller.js b/controller/together.controller.js index 8c44442..cb21afd 100644 --- a/controller/together.controller.js +++ b/controller/together.controller.js @@ -1,6 +1,7 @@ import * as togetherRepository from '../data/together.js'; import * as userRepository from '../data/auth.js'; +//이벤트 생성 export async function createEvent(req, res) { const { title, description } = req.body; const user = await userRepository.findById(req.userId); @@ -14,6 +15,7 @@ export async function createEvent(req, res) { res.status(201).json({event}); } +//이벤트 삭제 export async function deleteEvent(req, res){ const id = req.params.id; const deleteId = await togetherRepository.findByEventId(id); @@ -21,37 +23,38 @@ export async function deleteEvent(req, res){ const createUser = user.id; if(!deleteId) //삭제할 친바가 없다면 - return res.status(404).json({message: 'Event not found'}); - - //권한 + return res.status(404).json({message: 'Event not found1'}); + //권한 console.log(deleteId); if(deleteId.createdId !== createUser) { console.log(`?? ${deleteId.createdId} user=${createUser}`); return res.status(401).json({message: 'UnAuthorization User'}); - } await togetherRepository.deleteEvent(id); res.sendStatus(204); } +//전체 이벤트 조회 export async function getEventList(req, res){ const EventList = await togetherRepository.getEventList(); res.status(200).json({EventList}); } -//상세조회 , 유저객체정보를 배열로 넘겨달라 +//이벤트 상세조회 , 유저객체정보를 배열로 넘겨달라 export async function getEvent(req, res){ + console.log("오잉?"); const id = req.params.id; const event = await togetherRepository.findByEventId(id); if(!event) //조회할 친바가 없다면 - return res.status(404).json({message: 'Event not found'}); + return res.status(404).json({message: 'Event not found2'}); const teamList = await getTeamList(id); res.status(200).json({event, teamList}); } +//이벤트 참석 export async function register(req, res){ const user = await userRepository.findById(req.userId);//토큰으로 받아온 아이디 const eventId = req.body.eventId; @@ -61,7 +64,7 @@ export async function register(req, res){ const attend = await togetherRepository.register(user.id, eventId); res.status(201).json({attend}); } - +//이벤트 참석해제 export async function unregister(req, res){ const user = await userRepository.findById(req.userId);//토큰으로 받아온 아이디 const eventId = req.params.id;//event id @@ -119,4 +122,17 @@ export async function matching(req, res) { //팀 셔플 function shuffle(array){ array.sort(()=> Math.random() - 0.5); +} + +//게시글 작성을 위한 정보 조회 (모든 이벤트, 팀리스트) +export async function getEventInfo (req, res) { + let eventList = await togetherRepository.getEventList(); + console.log("hi"); + for(let i = 0; i < eventList.length; i++) + { + const team = await getTeamList(eventList[i].id); + eventList[i].teamList = team; + } + console.log(eventList); + res.status(200).json(eventList); } \ No newline at end of file diff --git a/data/auth.js b/data/auth.js index 55f3047..220aa81 100644 --- a/data/auth.js +++ b/data/auth.js @@ -12,6 +12,12 @@ export async function findByintraId(intraId) { .then((result) => result[0][0]); } +export async function findByEmail(email) { + return db + .execute('SELECT * FROM users WHERE email=?',[email]) + .then((result) => result[0][0]); +} + export async function createUser(user) { const {intraId, password, email, url} = user; return db diff --git a/data/board.js b/data/board.js index 2799959..5410f06 100644 --- a/data/board.js +++ b/data/board.js @@ -1,11 +1,5 @@ import { db } from '../db/database.js'; -export async function getEventList(){ - return db - .execute('SELECT ev.id, ev.title, ev.description at.teamId FROM event_info as ev JOIN attendance_info as at ON ev.id=at.id ') - .then((result)=>console.log(result[0])); -} - export async function findByPostId(id) { return db .execute('SELECT * FROM board WHERE id=?',[id]) @@ -27,8 +21,7 @@ export async function createPost(post) { export async function updatePost(post) { const {id, title, contents, image, eventId, attendMembers} = post; - - return db.execute('UPDATE board SET title=? contents=? image=? eventId=? attendMembers=? WHERE id=?', + return db.execute('UPDATE board SET title=? ,contents=? ,image=? ,eventId=? ,attendMembers=? WHERE id=?', [title, contents, image, eventId, attendMembers, id]) .then(()=> findByPostId(id)); } \ No newline at end of file diff --git a/routes/auth.js b/routes/auth.js index 140468f..6852a3a 100644 --- a/routes/auth.js +++ b/routes/auth.js @@ -32,9 +32,6 @@ const validateCredential = [ //회원가입 유효성 검사 const validateSignup= [ ...validateCredential, - //body('nickName') - // .notEmpty() - // .withMessage('nickName is missing'), body('email') .isEmail() .normalizeEmail() diff --git a/routes/board.js b/routes/board.js index f4555b7..ad630e7 100644 --- a/routes/board.js +++ b/routes/board.js @@ -10,9 +10,6 @@ const router = express.Router(); //게시글 생성 router.post('/', isAuth ,boardController.createPost); -////게시글 생성을 위한 조회 -//router.get('/write', isAuth ,boardController.getPostInfo); - //게시글 삭제 router.delete('/:id', isAuth ,boardController.deletePost);