Skip to content

Commit

Permalink
Merge pull request #229 from UMC-README/feat/#227
Browse files Browse the repository at this point in the history
[feat] 공지글 상세 조회 API 구현
  • Loading branch information
gs0428 authored Aug 19, 2024
2 parents 7e35bd3 + 95b01e3 commit afb00d6
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 7 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
20 changes: 18 additions & 2 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,6 +182,21 @@ export const createPostDao = async (body, userId) => {
}
};

export const getPostDAO = async (postId, userId) => {
const conn = await pool.getConnection();
try {
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);
}
};

export const updatePostDao = async (body, postId) => {
const conn = await pool.getConnection();
try {
Expand Down
17 changes: 17 additions & 0 deletions domains/admin/admin.dto.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ 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,
};
};

export const getRoomsDTO = (roomData) => ({
roomImage: roomData.room_image,
adminNickname: roomData.admin_nickname,
Expand Down
17 changes: 16 additions & 1 deletion domains/admin/admin.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import {
getRoomsDao,
getPostListDao,
getSubmitListDao,
getPostDAO,
} from "./admin.dao.js";
import { createShortUUID } from "./uuid.js";
import {
createRoomsDTO,
createPostDTO,
getRoomsDTO,
postListDTO,
getPostDTO,
} from "./admin.dto.js";

export const createRoomsService = async (body, userId) => {
Expand Down Expand Up @@ -95,6 +97,19 @@ export const createPostService = async (body, userId) => {
}
};

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

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가 필요합니다.");
Expand Down Expand Up @@ -179,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
9 changes: 9 additions & 0 deletions domains/admin/admin.sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ 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
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
98 changes: 95 additions & 3 deletions swagger/admin.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,98 @@ paths:
type: string
description: 퀴즈 답
/admin/post/{postId}:
get:
tags:
- admin
summary: 공지글 상세 조회
description: 공지글 수정을 위한 상세 조회를 합니다.
operationId: getPost
consumes:
- application/json
security:
- bearerAuth: []
parameters:
- in: path
name: postId
required: true
schema:
type: integer
example: 1
description: postId
responses:
"200":
description: 공지글 상세 조회 성공!
content:
application/json:
schema:
type: object
properties:
isSuccess:
type: boolean
example: true
code:
type: integer
example: 200
message:
type: string
example: "success!"
result:
type: object
properties:
title:
type: string
description: 공지글 제목
content:
type: string
description: 공지글 본문
imgURLs:
type: string
description: 공지글 이미지
startDate:
type: string
description: 시작 기한
endDate:
type: string
description: 마감 기한
question:
type: string
description: 퀴즈/미션 질문
quizAnswer:
type: string
description: 퀴즈 정답
"400":
description: 공지글 상세 조회 실패
content:
application/json:
schema:
type: object
properties:
isSuccess:
type: boolean
example: false
code:
type: string
example: "POST001"
message:
type: string
example: "존재하지 않는 공지글 ID 입니다."
"404":
description: 공지글 상세 조회 실패
content:
application/json:
schema:
type: object
properties:
isSuccess:
type: boolean
example: false
code:
type: string
example: "POST002"
message:
type: string
example: "본인의 공지글에 대해서만 조회가 가능합니다."

patch:
tags:
- admin
Expand Down Expand Up @@ -380,7 +472,7 @@ paths:
description: 퀴즈/미션 질문
quizAnswer:
type: string
description: 퀴즈/미션 질문
description: 퀴즈/미션 질문
addImgURLs:
type: array
items:
Expand Down Expand Up @@ -411,7 +503,7 @@ paths:
result:
type: object
properties:

delete:
tags:
- admin
Expand Down Expand Up @@ -882,4 +974,4 @@ paths:
example: "success!"
result:
type: string
example: "요청 수행에 성공하였습니다."
example: "요청 수행에 성공하였습니다."
1 change: 0 additions & 1 deletion utils/kakao.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export const getKakaoUser = async (token) => {
const response = await fetch("https://kapi.kakao.com/v2/user/me", {
method: "GET",
headers: {
// Authorization: `Bearer ${token}`,
Authorization: `Bearer ${token}`,
"Content-type": "application/x-www-form-urlencoded;charset=utf-8",
},
Expand Down

0 comments on commit afb00d6

Please sign in to comment.