Skip to content
Open
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
9 changes: 9 additions & 0 deletions config/response.status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export type ResponseType =
| "UNAUTHORIZED"
| "THERE_IS_NO_CONTENT_IN_COMMENT"
| "DATA_INSERTED_SQL_ERROR"
| "DATA_NOT_FOUND"
| "REQUEST_BODY_INVALID"
| "FORBIDDEN"
| "GENERATING_PRESIGNED_URL_ERROR"
Expand Down Expand Up @@ -219,6 +220,14 @@ export const status: Record<ResponseType, ResponseWithStatus> = {
message: "there is an error in the SQL syntax for inserting data.",
},
},
DATA_NOT_FOUND: {
status: StatusCodes.BAD_REQUEST,
body: {
isSuccess: false,
code: "404",
message: "ν•΄λ‹Ή 데이터가 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.",
},
},
UNAUTHORIZED: {
status: StatusCodes.BAD_REQUEST,
body: { isSuccess: false, code: "404", message: "unauthorized" },
Expand Down
86 changes: 48 additions & 38 deletions src/features/board/post/post.controller.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
import { Request, Response } from "express";
import { postService } from "./post.service";

export const postController = {
post: async (req: Request, res: Response) => {
const user_id = res.locals.id;
let result;
if (req.body.post_type === "blog") {
result = await postService.postBlog(req.body, user_id);
} else {
result = await postService.postMvp(req.body, user_id);
}
res.json(result);
},
export const postController = {
post: async (req: Request, res: Response) => {
const user_id = res.locals.id;
let result;
if (req.body.post_type === "blog") {
result = await postService.postBlog(req.body, user_id);
} else {
result = await postService.postMvp(req.body, user_id);
}
res.json(result);
},

get: async (req: Request, res: Response) => {
const user_id = res.locals.id;
const post_id = parseInt(req.params.post_id);
const post_type = req.query.post_type;
let result;

get: async (req: Request, res: Response) => {
const user_id = res.locals.id;
const post_id = parseInt(req.params.post_id);
const result = await postService.getType(post_id, user_id);
res.json(result);
},
patch: async (req: Request, res: Response) => {
const post_id = parseInt(req.params.post_id);
const user_id = res.locals.id;
let result;
if (req.body.type === "blog") {
result = await postService.patchBlog(req.body, post_id, user_id);
} else {
result = await postService.patchMvp(req.body, post_id, user_id);
}
res.json(result);
},
delete: async (req: Request, res: Response) => {
const post_id = parseInt(req.params.post_id);
const user_id = res.locals.id;
let result;
if (req.body.type === "blog") {
result = await postService.deleteBlog(post_id, user_id);
} else {
result = await postService.deleteMvp(post_id, user_id);
}
res.json(result);
if (post_type === "blog") {
result = await postService.getBlog(post_id, user_id);
} else if (post_type === "mvp") {
result = await postService.getMvp(post_id, user_id);
} else {
return res.status(400).json({ error: "μœ νš¨ν•˜μ§€ μ•Šμ€ typeμž…λ‹ˆλ‹€." });
}
res.json(result);
},

patch: async (req: Request, res: Response) => {
const post_id = parseInt(req.params.post_id);
const user_id = res.locals.id;
let result;
if (req.body.type === "blog") {
result = await postService.patchBlog(req.body, post_id, user_id);
} else {
result = await postService.patchMvp(req.body, post_id, user_id);
}
res.json(result);
},
delete: async (req: Request, res: Response) => {
const post_id = parseInt(req.params.post_id);
const user_id = res.locals.id;
let result;
if (req.body.type === "blog") {
result = await postService.deleteBlog(post_id, user_id);
} else {
result = await postService.deleteMvp(post_id, user_id);
}
res.json(result);
},
};
224 changes: 131 additions & 93 deletions src/features/board/post/post.service.ts
Original file line number Diff line number Diff line change
@@ -1,100 +1,138 @@
import { AppConfig } from "aws-sdk";
import { ApiError } from "../../../../config/error";
import { BaseApiResponse } from "../../../../config/response";
import { status } from "../../../../config/response.status";
import { postDao } from "../../../models/board/post/post.dao";
import { mvpDto, blogDto, getBlogDto, CommentList, ReplyList, matchDto, getBlogDto_ } from "../../../models/board/post/post.dto";
import {
mvpDto,
blogDto,
getBlogDto,
CommentList,
ReplyList,
matchDto,
getBlogDto_,
} from "../../../models/board/post/post.dto";

export const postService = {
getType: async (post_id: number, user_id: string) => {
const type = await postDao.getType(post_id);
let result;
if (type === "blog") {
result = await postDao.getBlog(post_id, user_id);
console.log(result);
} else {
result = await postDao.getMvp(post_id, user_id);
console.log(result);
}
const res0 = result[0] as getBlogDto_;
const res1 = result[1] as { img_url: string };
const res2 = result[2] as CommentList;
const res3 = result[3] as ReplyList;
const res4 = result[4] as { like_count: number };
const res5 = result[5] as { ex: number };
const match_id = res0.match_id;
const matchInfo = await postDao.getMatch(match_id) as matchDto;
const imgUrl = JSON.parse(res1.img_url);
const body: BaseApiResponse<getBlogDto> = {
...status.SUCCESS.body,
result: {
post_type: "blog",
...res0,
img_urls: imgUrl.img_urls as string[],
match_info: matchInfo,
comment_list: [
...res2
],
reply_list: [
...res3
],
like_count: res4.like_count,
has_liked: Boolean(res5.ex)
}
};
return body;
},
postBlog: async (req: blogDto, user_id: string) => {
const result = await postDao.postBlog(req, user_id);
const imgArr = { imgUrls: req.imgUrls };
const imgInfoArr = JSON.stringify(imgArr);
await postDao.postImg(imgInfoArr, result, req.post_type);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result
};
return body;
},
postMvp: async (req: mvpDto, user_id: string) => {
const result = await postDao.postMvp(req, user_id);
const imgArr = { imgUrls: req.imgUrls };
const imgInfoArr = JSON.stringify(imgArr);
await postDao.postImg(imgInfoArr, result, req.post_type);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result
};
return body;
},
patchBlog: async (req: blogDto, post_id: number, user_id: string) => {
const result = await postDao.patchBlog(req, post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result
};
return body;
},
patchMvp: async (req: mvpDto, post_id: number, user_id: string) => {
const result = await postDao.patchMvp(req, post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result
};
return body;
},
deleteBlog: async (post_id: number, user_id: string) => {
console.log(post_id, user_id);
const result = await postDao.deleteBlog(post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result
};
return body;
},
deleteMvp: async (post_id: number, user_id: string) => {
const result = await postDao.deleteMvp(post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result
};
return body;
getBlog: async (post_id: number, user_id: string) => {
try {
const result = await postDao.getBlog(post_id, user_id);

if (!result) {
throw new ApiError(status.NOT_FOUND);
}
return result;
} catch (error) {
console.log(error);
throw new ApiError(status.DATA_NOT_FOUND);
}
},

getMvp: async (post_id: number, user_id: string) => {
try {
const result = await postDao.getMvp(post_id, user_id);

if (!result) {
throw new ApiError(status.NOT_FOUND);
}
return result;
} catch (error) {
console.log(error);
throw new ApiError(status.DATA_NOT_FOUND);
}
},

// getType: async (post_id: number, user_id: string) => {
// const type = await postDao.getType(post_id);
// let result;
// if (type === "blog") {
// result = await postDao.getBlog(post_id, user_id);
// console.log(result);
// } else {
// result = await postDao.getMvp(post_id, user_id);
// console.log(result);
// }
// const res0 = result[0] as getBlogDto_;
// const res1 = result[1] as { img_url: string };
// const res2 = result[2] as CommentList;
// const res3 = result[3] as ReplyList;
// const res4 = result[4] as { like_count: number };
// const res5 = result[5] as { ex: number };
// const match_id = res0.match_id;
// const matchInfo = await postDao.getMatch(match_id) as matchDto;
// const imgUrl = JSON.parse(res1.img_url);
// const body: BaseApiResponse<getBlogDto> = {
// ...status.SUCCESS.body,
// result: {
// post_type: "blog",
// ...res0,
// img_urls: imgUrl.img_urls as string[],
// match_info: matchInfo,
// comment_list: [
// ...res2
// ],
// reply_list: [
// ...res3
// ],
// like_count: res4.like_count,
// has_liked: Boolean(res5.ex)
// }
// };
// return body;
// },
postBlog: async (req: blogDto, user_id: string) => {
const result = await postDao.postBlog(req, user_id);
const imgArr = { imgUrls: req.imgUrls };
const imgInfoArr = JSON.stringify(imgArr);
await postDao.postImg(imgInfoArr, result, req.post_type);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result,
};
return body;
},
postMvp: async (req: mvpDto, user_id: string) => {
const result = await postDao.postMvp(req, user_id);
const imgArr = { imgUrls: req.imgUrls };
const imgInfoArr = JSON.stringify(imgArr);
await postDao.postImg(imgInfoArr, result, req.post_type);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result,
};
return body;
},
patchBlog: async (req: blogDto, post_id: number, user_id: string) => {
const result = await postDao.patchBlog(req, post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result,
};
return body;
},
patchMvp: async (req: mvpDto, post_id: number, user_id: string) => {
const result = await postDao.patchMvp(req, post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result,
};
return body;
},
deleteBlog: async (post_id: number, user_id: string) => {
console.log(post_id, user_id);
const result = await postDao.deleteBlog(post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result,
};
return body;
},
deleteMvp: async (post_id: number, user_id: string) => {
const result = await postDao.deleteMvp(post_id, user_id);
const body: BaseApiResponse<number> = {
...status.SUCCESS.body,
result: result,
};
return body;
},
};
Loading