From cf729a0053b1d4c0d3ab7b26a6856d8ba0c131ce Mon Sep 17 00:00:00 2001 From: Rubytubi Date: Tue, 13 Aug 2024 16:06:51 +0900 Subject: [PATCH 1/2] m --- src/features/board/post/post.controller.ts | 76 +++++++++++----------- 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/src/features/board/post/post.controller.ts b/src/features/board/post/post.controller.ts index 852956a..c50c5a6 100644 --- a/src/features/board/post/post.controller.ts +++ b/src/features/board/post/post.controller.ts @@ -1,44 +1,44 @@ 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 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); + 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); + }, }; From 9c7c04983a1a6d4d7d1798a5154cf875b4838de9 Mon Sep 17 00:00:00 2001 From: Rubytubi Date: Tue, 13 Aug 2024 20:33:06 +0900 Subject: [PATCH 2/2] =?UTF-8?q?blog,=20mvp=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/response.status.ts | 9 + src/features/board/post/post.controller.ts | 12 +- src/features/board/post/post.service.ts | 224 ++++++++------ src/models/board/post/post.dao.ts | 341 +++++++++++---------- src/models/board/post/post.dto.ts | 200 +++++++----- src/models/board/post/post.sql.ts | 225 +++++++++----- 6 files changed, 599 insertions(+), 412 deletions(-) diff --git a/config/response.status.ts b/config/response.status.ts index 39144c0..052e78d 100644 --- a/config/response.status.ts +++ b/config/response.status.ts @@ -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" @@ -219,6 +220,14 @@ export const status: Record = { 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" }, diff --git a/src/features/board/post/post.controller.ts b/src/features/board/post/post.controller.ts index c50c5a6..32075cd 100644 --- a/src/features/board/post/post.controller.ts +++ b/src/features/board/post/post.controller.ts @@ -16,9 +16,19 @@ export const postController = { 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); + const post_type = req.query.post_type; + let 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; diff --git a/src/features/board/post/post.service.ts b/src/features/board/post/post.service.ts index 0dbe610..beac7a1 100644 --- a/src/features/board/post/post.service.ts +++ b/src/features/board/post/post.service.ts @@ -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 = { - ...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 = { - ...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 = { - ...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 = { - ...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 = { - ...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 = { - ...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 = { - ...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 = { + // ...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 = { + ...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 = { + ...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 = { + ...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 = { + ...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 = { + ...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 = { + ...status.SUCCESS.body, + result: result, + }; + return body; + }, }; diff --git a/src/models/board/post/post.dao.ts b/src/models/board/post/post.dao.ts index c91d777..e147f15 100644 --- a/src/models/board/post/post.dao.ts +++ b/src/models/board/post/post.dao.ts @@ -3,174 +3,197 @@ import { getPool } from "../../../../config/db.pool"; import { blogDto, mvpDto } from "./post.dto"; import { ApiError } from "../../../../config/error"; import { status } from "../../../../config/response.status"; -import { postSql, tempPostSql } from "./post.sql"; +import { postSql } from "./post.sql"; import { Json } from "aws-sdk/clients/robomaker"; - export const postDao = { - getType: async (post_id: number) => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(tempPostSql.getType, [ post_id ]); - connection.release(); - return result[0].type; - } catch { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - getMatch: async (match_id: number) => { - const connection = await getPool().getConnection(); - try { - const [ [ match ] ] = await connection.query(tempPostSql.getMatch, [ match_id ]); - connection.release(); - return match; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - getBlog: async (post_id: number, user_id: string) => { + getBlog: async (post_id: number, user_id: string) => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.getBlogSql, + [user_id, post_id] + ); + connection.release(); + return result; + } catch (error) { + console.log(error); + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, - const connection = await getPool().getConnection(); - try { - const [ [ blog ] ] = await connection.query(tempPostSql.getBlog, [ post_id, user_id ]); - const [ [ imgUrls ] ] = await connection.query(tempPostSql.getImgUrls, [ post_id ]); - const [ comment ] = await connection.query(tempPostSql.getComment, [ post_id ]); - const [ reply ] = await connection.query(tempPostSql.getRepyl, [ post_id ]); - const [ [ likeCount ] ] = await connection.query(tempPostSql.getLikeCount, [ post_id ]); - const [ [ hasLike ] ] = await connection.query(tempPostSql.getHasliked, - [ post_id, user_id ]); - connection.release(); - return [ blog, imgUrls, comment, reply, likeCount, hasLike ]; - } catch (error) { - console.log(error); - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - getMvp: async (post_id: number, user_id: string) => { - const connection = await getPool().getConnection(); - try { - const [ mvp ] = await connection.query(tempPostSql.getMvp, [ post_id, user_id ]); - const [ imgUrls ] = await connection.query(tempPostSql.getImgUrls, [ post_id ]); - const [ comment ] = await connection.query(tempPostSql.getComment, [ post_id ]); - const [ reply ] = await connection.query(tempPostSql.getRepyl, [ post_id ]); - const [ likeCount ] = await connection.query(tempPostSql.getLikeCount, [ post_id ]); - const [ hasLike ] = await connection.query(tempPostSql.getHasliked, [ post_id, user_id ]); + getMvp: async (post_id: number, user_id: string) => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.getMvpSql, + [user_id, post_id] + ); + connection.release(); + return result; + } catch (error) { + console.log(error); + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, + // getType: async (post_id: number) => { + // const connection = await getPool().getConnection(); + // try { + // const [ result ] = await connection.query(tempPostSql.getType, [ post_id ]); + // connection.release(); + // return result[0].type; + // } catch { + // throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + // } + // }, + // getMatch: async (match_id: number) => { + // const connection = await getPool().getConnection(); + // try { + // const [ [ match ] ] = await connection.query(tempPostSql.getMatch, [ match_id ]); + // connection.release(); + // return match; + // } catch (error) { + // throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + // } + // }, + // getBlog: async (post_id: number, user_id: string) => { - connection.release(); - return [ mvp, imgUrls, comment, reply, likeCount, hasLike ]; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, + // const connection = await getPool().getConnection(); + // try { + // const [ [ blog ] ] = await connection.query(tempPostSql.getBlog, [ post_id, user_id ]); + // const [ [ imgUrls ] ] = await connection.query(tempPostSql.getImgUrls, [ post_id ]); + // const [ comment ] = await connection.query(tempPostSql.getComment, [ post_id ]); + // const [ reply ] = await connection.query(tempPostSql.getRepyl, [ post_id ]); + // const [ [ likeCount ] ] = await connection.query(tempPostSql.getLikeCount, [ post_id ]); + // const [ [ hasLike ] ] = await connection.query(tempPostSql.getHasliked, + // [ post_id, user_id ]); + // connection.release(); + // return [ blog, imgUrls, comment, reply, likeCount, hasLike ]; + // } catch (error) { + // console.log(error); + // throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + // } + // }, + // getMvp: async (post_id: number, user_id: string) => { + // const connection = await getPool().getConnection(); + // try { + // const [ mvp ] = await connection.query(tempPostSql.getMvp, [ post_id, user_id ]); + // const [ imgUrls ] = await connection.query(tempPostSql.getImgUrls, [ post_id ]); + // const [ comment ] = await connection.query(tempPostSql.getComment, [ post_id ]); + // const [ reply ] = await connection.query(tempPostSql.getRepyl, [ post_id ]); + // const [ likeCount ] = await connection.query(tempPostSql.getLikeCount, [ post_id ]); + // const [ hasLike ] = await connection.query(tempPostSql.getHasliked, [ post_id, user_id ]); - postBlog: async (req: blogDto, user_id: string): Promise => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.postBlog, [ - req.title, - req.body, - req.imgUrls[0], - user_id, - req.match_id - ]); - connection.release(); - return result.insertId; - } catch (error) { - console.log(error); - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - postMvp: async (req: mvpDto, user_id: string): Promise => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.postMvp, [ - req.playerId, - req.playerRecord, - req.imgUrls[0], - user_id, - req.match_id - ]); - connection.release(); - return result.insertId; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, + // connection.release(); + // return [ mvp, imgUrls, comment, reply, likeCount, hasLike ]; + // } catch (error) { + // throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + // } + // }, + + postBlog: async (req: blogDto, user_id: string): Promise => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.postBlog, + [req.title, req.body, req.imgUrls[0], user_id, req.match_id] + ); + connection.release(); + return result.insertId; + } catch (error) { + console.log(error); + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, + postMvp: async (req: mvpDto, user_id: string): Promise => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.postMvp, + [req.playerId, req.playerRecord, req.imgUrls[0], user_id, req.match_id] + ); + connection.release(); + return result.insertId; + } catch (error) { + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, - postImg: async (imgSet: Json, post_id: number, post_type: string) => { - const connection = await getPool().getConnection(); + postImg: async (imgSet: Json, post_id: number, post_type: string) => { + const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.postImg, [ - imgSet, - post_id, - post_type - ]); - connection.release(); - return result.insertId; - } catch (error) { - console.log(error); - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, + try { + const [result] = await connection.query( + postSql.postImg, + [imgSet, post_id, post_type] + ); + connection.release(); + return result.insertId; + } catch (error) { + console.log(error); + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, - patchBlog: async (req: blogDto, post_id: number, user_id: string): Promise => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.patchBlog, [ - req.title, - req.body, - req.imgUrls[0], - post_id, - user_id - ]); - connection.release(); - return result.insertId; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - patchMvp: async (req: mvpDto, post_id: number, user_id: string): Promise => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.patchMvp, [ - req.playerId, - req.playerRecord, - req.imgUrls[0], - post_id, - user_id - ]); - connection.release(); - return result.insertId; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - deleteBlog: async (post_id: number, user_id: string): Promise => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.deleteBlog, [ - post_id, - user_id - ]); - connection.release(); - return result.insertId; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } - }, - deleteMvp: async (post_id: number, user_id: string): Promise => { - const connection = await getPool().getConnection(); - try { - const [ result ] = await connection.query(postSql.deleteMvp, [ - post_id, - user_id - ]); - connection.release(); - return result.insertId; - } catch (error) { - throw new ApiError(status.DATA_INSERTED_SQL_ERROR); - } + patchBlog: async ( + req: blogDto, + post_id: number, + user_id: string + ): Promise => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.patchBlog, + [req.title, req.body, req.imgUrls[0], post_id, user_id] + ); + connection.release(); + return result.insertId; + } catch (error) { + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, + patchMvp: async ( + req: mvpDto, + post_id: number, + user_id: string + ): Promise => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.patchMvp, + [req.playerId, req.playerRecord, req.imgUrls[0], post_id, user_id] + ); + connection.release(); + return result.insertId; + } catch (error) { + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, + deleteBlog: async (post_id: number, user_id: string): Promise => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.deleteBlog, + [post_id, user_id] + ); + connection.release(); + return result.insertId; + } catch (error) { + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); + } + }, + deleteMvp: async (post_id: number, user_id: string): Promise => { + const connection = await getPool().getConnection(); + try { + const [result] = await connection.query( + postSql.deleteMvp, + [post_id, user_id] + ); + connection.release(); + return result.insertId; + } catch (error) { + throw new ApiError(status.DATA_INSERTED_SQL_ERROR); } + }, }; diff --git a/src/models/board/post/post.dto.ts b/src/models/board/post/post.dto.ts index 54cf614..cfe95e1 100644 --- a/src/models/board/post/post.dto.ts +++ b/src/models/board/post/post.dto.ts @@ -1,107 +1,147 @@ export interface blogDto { - post_type: "blog", - title: string, - body: string, - imgUrls: string[], - match_id: number + post_type: "blog"; + title: string; + body: string; + imgUrls: string[]; + match_id: number; } export interface mvpDto { - - post_type: "mvp", - playerId: number, - playerRecord: string, - imgUrls: string[], - match_id: number + post_type: "mvp"; + playerId: number; + playerRecord: string; + imgUrls: string[]; + match_id: number; } export interface getBlogDto { - post_type: "blog", - title: string, - body: string, - create_at: Date, - updated_at: Date, - author: string, - user_id: string, - match_id: number - img_urls: string[], - match_info: { - match_date: Date, - home_team_icon_flag: string, - away_team_icon_flag: string, - home_team_score: number, - away_team_score: number - }, - comment_list: { - comment_id: string, - comment_user_id: string, - comment_user_name: string, - comment_body: string, - comment_date: Date - }[], - reply_list: { - reply_id: string, - reply_user_id: string, - reply_user_name: string, - reply_body: string, - reply_date: Date, - commented_id: string - }[], - like_count: number, - has_liked: boolean + post_type: "blog"; + title: string; + body: string; + create_at: Date; + updated_at: Date; + user_id: string; + user_name: string; + user_icon_url: string; + match_id: number; + img_urls: string[]; + like_count: number; + has_liked: boolean; + isMine: boolean; + match_info: { + match_date: Date; + home_team_icon_flag: string; + away_team_icon_flag: string; + home_team_score: number; + away_team_score: number; + }; + comment_list: { + comment_id: string; + comment_user_id: string; + comment_user_name: string; + comment_user_icon_url: string; + comment_body: string; + comment_date: Date; + comment_isMine: boolean; + }[]; + reply_list: { + reply_id: string; + reply_user_id: string; + reply_user_name: string; + reply_user_icon_url: string; + reply_body: string; + reply_date: Date; + commented_id: string; + reply_isMine: boolean; + }[]; } - +export interface getMvpDto { + post_type: "mvp"; + player_id: number; + player_url: string; + player_record: string; + create_at: Date; + updated_at: Date; + user_id: string; + user_name: string; + user_icon_url: string; + match_id: number; + img_urls: string[]; + like_count: number; + has_liked: boolean; + isMine: boolean; + match_info: { + match_date: Date; + home_team_icon_flag: string; + away_team_icon_flag: string; + home_team_score: number; + away_team_score: number; + }; + comment_list: { + comment_id: string; + comment_user_id: string; + comment_user_name: string; + comment_user_icon_url: string; + comment_body: string; + comment_date: Date; + comment_isMine: boolean; + }[]; + reply_list: { + reply_id: string; + reply_user_id: string; + reply_user_name: string; + reply_user_icon_url: string; + reply_body: string; + reply_date: Date; + commented_id: string; + reply_isMine: boolean; + }[]; +} export interface getBlogDto_ { - title: string, - body: string, - create_at: Date, - updated_at: Date, - author: string, - user_id: string, - match_id: number + title: string; + body: string; + create_at: Date; + updated_at: Date; + author: string; + user_id: string; + match_id: number; } export interface matchDto { - match_date: Date, - home_team_icon_flag: string, - away_team_icon_flag: string, - home_team_score: number, - away_team_score: number + match_date: Date; + home_team_icon_flag: string; + away_team_icon_flag: string; + home_team_score: number; + away_team_score: number; } export interface Comment { - comment_id: string, - comment_user_id: string, - comment_user_name: string, - comment_body: string, - comment_date: Date + comment_id: string; + comment_user_id: string; + comment_user_name: string; + comment_body: string; + comment_date: Date; } export interface CommentList extends Iterable { - [index: number]: Comment, - length: number, - [Symbol.iterator](): Iterator + [index: number]: Comment; + length: number; + [Symbol.iterator](): Iterator; } export interface Reply { - reply_id: string, - reply_user_id: string, - reply_user_name: string, - reply_body: string, - reply_date: Date, - commented_id: string + reply_id: string; + reply_user_id: string; + reply_user_name: string; + reply_body: string; + reply_date: Date; + commented_id: string; } export interface ReplyList extends Iterable { - [index: number]: Reply, - length: number, - [Symbol.iterator](): Iterator + [index: number]: Reply; + length: number; + [Symbol.iterator](): Iterator; } - - - - - - diff --git a/src/models/board/post/post.sql.ts b/src/models/board/post/post.sql.ts index eda8632..a586d88 100644 --- a/src/models/board/post/post.sql.ts +++ b/src/models/board/post/post.sql.ts @@ -1,8 +1,75 @@ export const postSql = { - postBlog: "INSERT INTO blog(title, body, thumbnail_url, user_id, match_info) VALUES (?, ?, ?, ?, ?)", - postImg: "INSERT INTO image (url, post_id, post_type) VALUES (?, ?, ?)", - postMvp: "INSERT INTO mvp (player_id, player_record, thumbnail_url, user_id, match_info) VALUES (?, ?, ?, ?, ?)", - patchBlog: ` + getBlogSql: ` + SELECT + b.title, + b.body, + p.created_at, + p.updated_at, + u.name AS author, + p.user_id, + b.match_info, + c.comment_id, + c.comment_user, + c.comment_user_name, + c.comment_body, + c.comment_date, + r.reply_id, + r.reply_user_id, + r.reply_user_name, + r.reply_body, + r.reply_date, + img.img_url, + (SELECT COUNT(*) FROM post_like WHERE post_id = p.id) AS like_count, + (SELECT EXISTS (SELECT 1 FROM post_like WHERE post_id = p.id AND user_id = ?) AS ex) AS has_liked + FROM global_post_id p + LEFT JOIN blog b ON p.id = b.id AND p.type = 'blog' + LEFT JOIN user u ON p.user_id = u.id + LEFT JOIN comment c ON p.id = c.post_id + LEFT JOIN user cu ON c.user_id = cu.id + LEFT JOIN reply r ON c.id = r.comment_id + LEFT JOIN user ru ON r.user_id = ru.id + LEFT JOIN image img ON p.id = img.post_id + WHERE p.id = ? AND p.type = 'blog' + `, + + getMvpSql: ` + SELECT + m.player_name, + m.player_record, + p.created_at, + p.updated_at, + u.name AS author, + p.user_id, + b.match_info, + c.comment_id, + c.comment_user, + c.comment_user_name, + c.comment_body, + c.comment_date, + r.reply_id, + r.reply_user_id, + r.reply_user_name, + r.reply_body, + r.reply_date, + img.img_url, + (SELECT COUNT(*) FROM post_like WHERE post_id = p.id) AS like_count, + (SELECT EXISTS (SELECT 1 FROM post_like WHERE post_id = p.id AND user_id = ?) AS ex) AS has_liked + FROM global_post_id p + LEFT JOIN mvp m ON p.id = m.id AND p.type = 'mvp' + LEFT JOIN user u ON p.user_id = u.id + LEFT JOIN comment c ON p.id = c.post_id + LEFT JOIN user cu ON c.user_id = cu.id + LEFT JOIN reply r ON c.id = r.comment_id + LEFT JOIN user ru ON r.user_id = ru.id + LEFT JOIN image img ON p.id = img.post_id + WHERE p.id = ? AND p.type = 'mvp' + `, + postBlog: + "INSERT INTO blog(title, body, thumbnail_url, user_id, match_info) VALUES (?, ?, ?, ?, ?)", + postImg: "INSERT INTO image (url, post_id, post_type) VALUES (?, ?, ?)", + postMvp: + "INSERT INTO mvp (player_id, player_record, thumbnail_url, user_id, match_info) VALUES (?, ?, ?, ?, ?)", + patchBlog: ` UPDATE blog SET title = ?, @@ -10,7 +77,7 @@ export const postSql = { thumbnail_url = ? WHERE id = ? and user_id = ?`, - patchMvp: ` + patchMvp: ` UPDATE mvp SET player_id = ?, @@ -18,81 +85,81 @@ export const postSql = { thumbnail_url = ? WHERE id = ? and user_id = ?`, - deleteBlog: "DELETE FROM blog WHERE id = ? and user_id = ?", - deleteMvp: "DELETE FROM mvp WHERE id = ? and user_id = ?" - + deleteBlog: "DELETE FROM blog WHERE id = ? and user_id = ?", + deleteMvp: "DELETE FROM mvp WHERE id = ? and user_id = ?", }; -export const tempPostSql = { - getType: "SELECT type from global_post_id WHERE id = ?", - getMvp: ` - SELECT - p.name as player_name, - m.player_record as player_record, - m.created_at as created_at, - m.updated_at as updated_at, - u.name as author, - m.user_id as user_id, - m.match_info as match_info - FROM mvp m join user u on m.user_id = u.id join player p on m.player_id = p.id - where m.id = ?`, - getBlog: ` - SELECT - b.title as title, - b.body as body, - b.created_at as created_at, - b.updated_at as updated_at, - u.name as author, - b.user_id as user_id, - b.match_info as match_id - FROM blog b join user u on b.user_id = u.id - where b.id = ?`, - getImgUrls: ` - SELECT - image.url AS img_url - FROM image - WHERE post_id = ?`, - getMatch: - `SELECT - m.match_date, - m.id as match_id, - m.home_team_id as home_team_id, - m.away_team_id as away_team_id, - t1.name as home_team_name, - t2.name as away_team_name, - t1.icon_flag as home_team_icon_flag, - t2.icon_flag as away_team_icon_flag, - m.home_team_score as home_team_score, - m.away_team_score as away_team_score - FROM matchinfo m - JOIN team t1 ON m.home_team_id = t1.id - JOIN team t2 ON m.away_team_id = t2.id - WHERE m.id = ? `, - getComment: ` - SELECT - c.id as comment_id, - u.id as comment_user_id, - u.name as comment_user_name, - c.body as comment_body, - c.created_at as comment_date - FROM comment c join user u on c.user_id = u.id - WHERE c.post_id = ?`, - getRepyl: ` - SELECT - r.id as reply_id, - u.id as reply_user_id, - u.name as reply_user_name, - r.body as reply_body, - r.created_at as reply_date, - r.comment_id as commented_id - FROM reply r join user u on r.user_id = u.id - WHERE r.post_id = ?`, - getLikeCount: ` - SELECT COUNT(*) as like_count - FROM post_like - WHERE post_id =?`, - getHasliked: ` - SELECT EXISTS (SELECT 1 FROM post_like WHERE post_id =? AND user_id =?) as ex` +//export const getPostSql = {}; +// export const tempPostSql = { +// getType: "SELECT type from global_post_id WHERE id = ?", +// getMvp: ` +// SELECT +// p.name as player_name, +// m.player_record as player_record, +// m.created_at as created_at, +// m.updated_at as updated_at, +// u.name as author, +// m.user_id as user_id, +// m.match_info as match_info +// FROM mvp m join user u on m.user_id = u.id join player p on m.player_id = p.id +// where m.id = ?`, +// getBlog: ` +// SELECT +// b.title as title, +// b.body as body, +// b.created_at as created_at, +// b.updated_at as updated_at, +// u.name as author, +// b.user_id as user_id, +// b.match_info as match_id +// FROM blog b join user u on b.user_id = u.id +// where b.id = ?`, +// getImgUrls: ` +// SELECT +// image.url AS img_url +// FROM image +// WHERE post_id = ?`, +// getMatch: +// `SELECT +// m.match_date, +// m.id as match_id, +// m.home_team_id as home_team_id, +// m.away_team_id as away_team_id, +// t1.name as home_team_name, +// t2.name as away_team_name, +// t1.icon_flag as home_team_icon_flag, +// t2.icon_flag as away_team_icon_flag, +// m.home_team_score as home_team_score, +// m.away_team_score as away_team_score +// FROM matchinfo m +// JOIN team t1 ON m.home_team_id = t1.id +// JOIN team t2 ON m.away_team_id = t2.id +// WHERE m.id = ? `, +// getComment: ` +// SELECT +// c.id as comment_id, +// u.id as comment_user_id, +// u.name as comment_user_name, +// c.body as comment_body, +// c.created_at as comment_date +// FROM comment c join user u on c.user_id = u.id +// WHERE c.post_id = ?`, +// getRepyl: ` +// SELECT +// r.id as reply_id, +// u.id as reply_user_id, +// u.name as reply_user_name, +// r.body as reply_body, +// r.created_at as reply_date, +// r.comment_id as commented_id +// FROM reply r join user u on r.user_id = u.id +// WHERE r.post_id = ?`, +// getLikeCount: ` +// SELECT COUNT(*) as like_count +// FROM post_like +// WHERE post_id =?`, +// getHasliked: ` +// SELECT EXISTS (SELECT 1 FROM post_like WHERE post_id =? AND user_id =?) as ex` -}; +// };