Skip to content

Commit

Permalink
#30 #31 게시글 생성, 수정, 삭제
Browse files Browse the repository at this point in the history
#30 #31 게시글 생성, 수정, 삭제
이미지빼고..
  • Loading branch information
kth2624 committed May 8, 2022
1 parent 8e39844 commit a69a443
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
17 changes: 11 additions & 6 deletions controller/board.controller.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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});
}


30 changes: 23 additions & 7 deletions controller/together.controller.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -14,44 +15,46 @@ 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);
const user = await userRepository.findById(req.userId);
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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
6 changes: 6 additions & 0 deletions data/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions data/board.js
Original file line number Diff line number Diff line change
@@ -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])
Expand All @@ -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));
}
3 changes: 0 additions & 3 deletions routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ const validateCredential = [
//회원가입 유효성 검사
const validateSignup= [
...validateCredential,
//body('nickName')
// .notEmpty()
// .withMessage('nickName is missing'),
body('email')
.isEmail()
.normalizeEmail()
Expand Down
3 changes: 0 additions & 3 deletions routes/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit a69a443

Please sign in to comment.