Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
gs0428 committed Aug 19, 2024
2 parents 1447050 + afb00d6 commit 125b77d
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 76 deletions.
12 changes: 12 additions & 0 deletions config/response.status.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,16 @@ export const status = {
code: "COMMON011",
message: "이미지를 삭제하는데 실패했습니다.",
},
NOT_MY_POST: {
status: StatusCodes.BAD_REQUEST,
isSuccess: false,
code: "POST001",
message: "본인의 공지글에 대해서만 조회가 가능합니다.",
},
NOT_FOUND_POST: {
status: StatusCodes.NOT_FOUND,
isSuccess: false,
code: "POST002",
message: "존재하지 않는 공지글 ID 입니다.",
},
};
15 changes: 15 additions & 0 deletions domains/admin/admin.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
userRequestService,
getRoomsService,
getPostListService,
getPostService,
} from "./admin.service.js";

export const createRoomsController = async (req, res, next) => {
Expand Down Expand Up @@ -67,6 +68,20 @@ export const createPostController = async (req, res, next) => {
}
};

export const getPostController = async (req, res, next) => {
try {
const result = await getPostService(req.params.postId, req.user.userId);
if (result === -1) {
return res.status(404).json(response(status.NOT_FOUND_POST));
} else if (result === -2) {
return res.status(400).json(response(status.NOT_MY_POST));
}
res.status(200).json(response(status.SUCCESS, result));
} catch (error) {
next(error);
}
};

export const updatePostController = async (req, res, next) => {
try {
const result = await updatePostService(req.body, req.params.postId);
Expand Down
57 changes: 29 additions & 28 deletions domains/admin/admin.dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getRoomSQL,
getAlluserRoomSQL,
decreaseUnreadCountOneBySubmitId,
getPostSQL,
} from "./admin.sql.js";

import { updateUnreadCountByRoom } from "../room/room.sql.js";
Expand All @@ -57,8 +58,8 @@ export const createRoomsDao = async (body, userId, roomInviteUrl) => {
await conn.query(userRoomSQL, [userId, roomId, body.admin_nickname]);

conn.release();
return{
roomId: roomId,
return {
roomId: roomId,
roomImage: body.room_image,
adminNickname: body.admin_nickname,
roomName: body.room_name,
Expand Down Expand Up @@ -181,46 +182,46 @@ export const createPostDao = async (body, userId) => {
}
};

export const updatePostDao = async ({ postData, imgURLs, imgToDelete }, postId) => {
export const getPostDAO = async (postId, userId) => {
const conn = await pool.getConnection();
try {
await conn.beginTransaction();
const startDate = new Date(`20${postData.start_date}`);
const endDate = new Date(`20${postData.end_date}`);
const [post] = await conn.query(getPostSQL, postId);
if (!post.length) return -1;
if (post[0].admin_id !== userId) return -2;
conn.release();
return post[0];
} catch (error) {
conn.release();
console.error("admin 공지글 조회하기 에러:", error);
throw new BaseError(status.INTERNAL_SERVER_ERROR);
}
};

if (isInvalidDate(postData.start_date, postData.end_date)) return -1;
if (startDate < getNow()) return -2;
if (endDate <= startDate) return -3;
export const updatePostDao = async (body, postId) => {
const conn = await pool.getConnection();
try {
await conn.beginTransaction();

await conn.query(updatePostSQL, [
postData.title,
postData.content,
postData.start_date,
postData.end_date,
postData.question,
postId,
body.postTitle,
body.postContent,
body.endDate,
body.question,
body.quizAnswer,
postId
]);
// 추가할 이미지
for (const url of imgURLs) {
for (const url of body.addImgURLs) {
await conn.query(createPostImgSQL, [url, postId]);
}
// 삭제할 이미지
if (imgToDelete.length > 0) {
for (const url of imgToDelete) {
if (body.deleteImgURLs) {
for (const url of body.deleteImgURLs) {
await conn.query(deletePostImgSQL, [postId, url]);
}
}

await conn.commit();
return {
postTitle: postData.title,
postContent: postData.content,
addImgURLs: imgURLs, // 추가할 이미지
deleteImgURLs: imgToDelete, // 삭제할 이미지
startDate: postData.start_date,
endDate: postData.end_date,
question: postData.question,
};
return true;
} catch (error) {
await conn.rollback();
console.error("공지글 수정하기 에러:", error);
Expand Down
11 changes: 11 additions & 0 deletions domains/admin/admin.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export const createPostDTO = (createPostData) => {
};
};

export const getPostDTO = (postData) => ({
type: postData.type,
title: postData.title,
content: postData.content,
imgURLs: postData.imgURLs,
startDate: postData.start_date,
endDate: postData.end_date,
question: postData.question,
quizAnswer: postData.quiz_answer,
});

export const updatePostDTO = (updatePostData) => {
return {
...updatePostData,
Expand Down
29 changes: 20 additions & 9 deletions domains/admin/admin.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import {
getRoomsDao,
getPostListDao,
getSubmitListDao,
getPostDAO,
} from "./admin.dao.js";
import { createShortUUID } from "./uuid.js";
import {
createRoomsDTO,
createPostDTO,
updatePostDTO,
getRoomsDTO,
postListDTO,
getPostDTO,
} from "./admin.dto.js";

export const createRoomsService = async (body, userId) => {
Expand Down Expand Up @@ -96,17 +97,27 @@ export const createPostService = async (body, userId) => {
}
};

export const updatePostService = async (body, postId) => {
export const getPostService = async (postId, userId) => {
try {
const postData = await updatePostDao(body, postId);
const postData = await getPostDAO(postId, userId);

if (postData == -1) throw new BaseError(status.WRONG_DATE_FORMAT);
if (postData == -2) throw new BaseError(status.WRONG_STARTDATE_COMPARE);
if (postData == -3) throw new BaseError(status.WRONG_ENDDATE_COMPARE);
if (typeof postData === "number") return postData;

return getPostDTO(postData);
} catch (error) {
console.error("공지글 수정하기 에러:", error);
throw error;
}
};

export const updatePostService = async (body, postId) => {
try {
if (!postId) throw new Error("수정할 공지방의 ID가 필요합니다.");
await updatePostDao(body, postId);

await cancelImposePenaltyByPostDAO(postId);
await reserveImposePenaltyByPostDAO(postId, `20${postData.endDate}`);
return updatePostDTO(postData);
await reserveImposePenaltyByPostDAO(postId, `20${body.endDate}`);
return "공지글 수정에 성공하였습니다.";
} catch (error) {
console.error("공지글 수정하기 에러:", error);
throw error;
Expand Down Expand Up @@ -183,7 +194,7 @@ export const userInviteService = async (roomId) => {

export const deleteUserService = async (body) => {
try {
if(!body.userId) throw new Error("강퇴할 User의 ID가 필요합니다.");
if (!body.userId) throw new Error("강퇴할 User의 ID가 필요합니다.");
const result = await deleteUserDao(body);
if (result == -1) throw new Error("공지방에 유저가 없습니다.");
return result;
Expand Down
11 changes: 10 additions & 1 deletion domains/admin/admin.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ export const createPostImgSQL = `
INSERT INTO \`post-image\` (URL, post_id) VALUES(?,?);
`;

// admin 공지글 조회
export const getPostSQL = `
SELECT p.title, p.content, p.type, p.start_date, p.end_date, p.question, p.quiz_answer, r.admin_id
FROM post p
JOIN room r
ON r.id = p.room_id
WHERE p.id = ?;
`;

// 공지글 수정 & 이미지 삭제
export const updatePostSQL = `
UPDATE post
SET title = ?, content = ?, start_date = ?, end_date = ?, question = ?
SET title = ?, content = ?, end_date = ?, question = ? , quiz_answer = ?
WHERE id = ?;
`;
export const deletePostImgSQL = `
Expand Down
7 changes: 6 additions & 1 deletion domains/room/room.dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ import {
} from "./room.sql.js";
import { getUserById } from "../user/user.sql.js";

import dotenv from "dotenv";
dotenv.config();

export const fixPostDAO = async (data) => {
try {
const conn = await pool.getConnection();
Expand Down Expand Up @@ -443,7 +446,9 @@ export const postRoomEntranceDAO = async (roomId, userId, userNickname) => {
}
console.log(roomId, userId, userNickname);

await conn.query(createRoomEntranceSQL, [userId, roomId, userNickname]);
const defaultProfileImage = process.env.DEFAULT_PROFILE_IMAGE;

await conn.query(createRoomEntranceSQL, [userId, roomId, userNickname, defaultProfileImage]);
await conn.query(initializeSubmitWhenUserJoinsRoomSQL, [userId, roomId]);
await conn.query(updateUnreadCountByRoom, roomId);

Expand Down
2 changes: 1 addition & 1 deletion domains/room/room.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export const checkRoomPasswordSQL = `

//공지방에 유저를 입장 등록
export const createRoomEntranceSQL = `
INSERT INTO \`user-room\` (user_id, room_id, nickname) VALUES (?, ?, ?)
INSERT INTO \`user-room\` (user_id, room_id, nickname, profile_image) VALUES (?, ?, ?, ?)
`;

//공지방 내 공지글 제목, 내용 대상으로 검색 (커서 존재)
Expand Down
6 changes: 6 additions & 0 deletions domains/user/user.dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ import {
getPenaltyCount,
} from "./user.sql.js";

import dotenv from "dotenv";
dotenv.config();

export const insertUser = async (data) => {
try {
const conn = await pool.getConnection();

const defaultProfileImage = process.env.DEFAULT_PROFILE_IMAGE;

const result = await conn.query(userSignUpSQL, [
data.name,
data.nickname,
data.email,
data.password,
defaultProfileImage,
]);

conn.release();
Expand Down
2 changes: 1 addition & 1 deletion domains/user/user.sql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// 회원가입
export const userSignUpSQL = `
INSERT INTO user (name, nickname, email, password) VALUES (?, ?, ?, ?)
INSERT INTO user (name, nickname, email, password, profile_image) VALUES (?, ?, ?, ?, ?)
`;

// ID 값으로 User 찾기
Expand Down
2 changes: 2 additions & 0 deletions routes/admin.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getRoomsController,
getPostListController,
getSubmitListController,
getPostController,
} from "../domains/admin/admin.controller.js";

export const adminRouter = express.Router();
Expand All @@ -26,6 +27,7 @@ adminRouter.patch("/rooms/:roomId", tokenAuth, expressAsyncHandler(updateRoomsCo
adminRouter.get("/rooms/:roomId", tokenAuth, expressAsyncHandler(getRoomsController));
adminRouter.delete("/rooms/:roomId", tokenAuth, expressAsyncHandler(deleteRoomsController));
adminRouter.post("/post", tokenAuth, expressAsyncHandler(createPostController));
adminRouter.get("/post/:postId", tokenAuth, expressAsyncHandler(getPostController));
adminRouter.patch("/post/:postId", tokenAuth, expressAsyncHandler(updatePostController));
adminRouter.delete("/post/:postId", tokenAuth, expressAsyncHandler(deletePostController));
adminRouter.get("/post/:postId/unread", tokenAuth, expressAsyncHandler(unreadUserListController));
Expand Down
Loading

0 comments on commit 125b77d

Please sign in to comment.