Skip to content

Commit

Permalink
Merge pull request #133 from CartoonIsArt/lhs
Browse files Browse the repository at this point in the history
데이터베이스 검색 기능 구현 및 글/댓글쓰기 안되는 문제 수정
  • Loading branch information
sGOM committed Mar 21, 2021
2 parents a44f22d + 3c26906 commit 09ab74d
Show file tree
Hide file tree
Showing 7 changed files with 549 additions and 408 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -15,7 +15,7 @@
"koa-router": "^7.2.1",
"koa-views": "^7.0.0",
"sqlite3": "^5.0.2",
"typeorm": "next",
"typeorm": "0.2.3",
"typescript": "^4.2.3"
},
"devDependencies": {
Expand Down
228 changes: 103 additions & 125 deletions src/controllers/comment.ts
Expand Up @@ -6,208 +6,186 @@ import User from "../entities/user"
/* 해당 댓글 GET */
export const GetOne = async (ctx, next) => {
const conn: Connection = getConnection()
const { id } = ctx.params

try{
try {
const comment: Comment = await conn
.getRepository(Comment)
.findOne(ctx.params.id, {
relations: [
"author",
"author.profileImage",
"rootDocument",
"likedUsers",
"comments",
"rootComment",
]})

ctx.body = comment
.getRepository(Comment)
.findOne(id, {
relations: [
"author",
"author.profileImage",
"rootDocument",
"likedUsers",
"comments",
"rootComment",
]
})

/* GET 성공 응답 */
ctx.response.status = 200
ctx.body = { comment }
}
catch (e){
catch (e) {
ctx.throw(400, e)
}

/* GET 성공 응답 */
ctx.response.status = 200
}

/* 댓글 POST */
export const Post = async (ctx, next) => {
const conn: Connection = getConnection()
const comment: Comment = new Comment()
const data = ctx.request.body.data
let documentId: number = null
const tokenUser = ctx.state.token.user
const { documentId, commentId, content } = ctx.request.body

try{
/* 세션의 유저와 relation 설정 */
comment.author = ctx.state.token.user

/* commentId를 인자로 전달하면 대댓글 relation 설정 */
if (typeof(data.commentId) === "number") {
const parent: Comment = await conn
.getRepository(Comment)
.findOne(data.commentId, {
relations: ["rootDocument"],
try {
/* 유저와 relation 설정 */
const user: User = await conn
.getRepository(User)
.findOne(tokenUser.id, {
relations: ["profileImage"],
})
comment.author = user

/* 나머지 required 정보 입력 */
comment.content = content
comment.likedUsers = []

if (commentId) { // 대댓글인 경우
const parent: Comment = await conn
.getRepository(Comment)
.findOne(commentId, {
relations: ["rootDocument"],
})
comment.rootDocument = parent.rootDocument
comment.rootComment = parent
documentId = parent.rootDocument.id
}

/* 게시글과 relation 설정 */
const document: Document = await conn
.getRepository(Document)
.findOneOrFail(documentId
? documentId : Number(data.documentId))

comment.rootDocument = document

/* 나머지 required 정보 입력 */
comment.id = data.id
comment.createdAt = data.created_at
comment.content = data.text
else if (documentId) {
const parent: Document = await conn
.getRepository(Document)
.findOne(documentId)
comment.rootDocument = parent
}

/* 댓글 작성자의 댓글 수 1 증가 */
++(comment.author.commentsCount)

comment.likedUsers = []

await conn.manager.save(comment.author)
await conn.manager.save(comment)
}
catch (e){
if (e.message ===
"Cannot read property 'user' of undefined"){
ctx.throw(401, e)
}
ctx.throw(400, e)
}

/* POST 성공 응답 */
ctx.body = comment
ctx.response.status = 200
}

/* 해당 댓글 DELETE */
export const DeleteOne = async (ctx, next) => {
const conn: Connection = getConnection()
const leaver: User = await conn.getRepository(User).findOne(0)

try {
/* DB에서 댓글 불러오기 */
const comment: Comment = await conn
.getRepository(Comment)
.findOne(ctx.params.id, {
relations: [
"author",
"likedUsers",
]})

/* 댓글 작성자의 댓글 수 1 감소 */
--(comment.author.commentsCount)
await conn.manager.save(comment.author)

/* 탈퇴한 유저 relation */
comment.author = leaver
await conn.manager.save(comment)
/* POST 성공 응답 */
ctx.response.status = 200
ctx.body = { comment }
}
catch (e) {
ctx.throw(400, e)
}

/* DELETE 성공 응답 */
ctx.response.status = 204
}

/* 해당 댓글 좋아요 GET */
export const GetLikes = async (ctx, next) => {
const conn: Connection = getConnection()
const { id } = ctx.params

try{
const comment: Comment = await conn
.getRepository(Comment)
.findOne(ctx.params.id, {
relations: [
"likedUsers",
"likedUsers.profileImage",
]})

ctx.body = comment.likedUsers
.getRepository(Comment)
.findOne(id, {
relations: [
"likedUsers",
"likedUsers.profileImage",
]
})

/* GET 성공 응답 */
ctx.response.status = 200
ctx.body = { likedUsers: comment.likedUsers }
}
catch (e){
catch (e) {
ctx.throw(400, e)
}

/* GET 성공 응답 */
ctx.response.status = 200
}

/* 해당 댓글 좋아요 POST */
export const PostLikes = async (ctx, next) => {
const conn: Connection = getConnection()
const { id } = ctx.params
const tokenUser = ctx.state.token.user

try {
/* DB에서 댓글 불러오기 */
const comment: Comment = await conn
.getRepository(Comment)
.findOne(ctx.params.id, {
relations: ["likedUsers"],
})
.getRepository(Comment)
.findOne(id, {
relations: ["likedUsers"],
})

/* 세션 유저 불러오기 */
const user: User = ctx.state.token.user
/* 유저 불러오기 */
const user: User = await conn
.getRepository(User)
.findOne(tokenUser.id, {
relations: ["profileImage"],
})

/* 세션의 유저와 좋아요 relation 설정 */
comment.likedUsers.push(user)
/* 유저와 좋아요 relation 설정 */
++(user.likedCommentsCount)
comment.likedUsers.push(user)

await conn.manager.save(comment)
await conn.manager.save(user)

/* POST 성공 응답 */
ctx.body = comment.likedUsers
ctx.response.status = 200
ctx.body = {
likedUsers: comment.likedUsers,
user,
}
}
catch (e) {
if (e.message ===
"Cannot read property 'user' of undefined"){
ctx.throw(401, e)
}
ctx.throw(400, e)
}
}

/* 해당 댓글 좋아요 DELETE */
export const CalcelLikes = async (ctx, next) => {
const conn: Connection = getConnection()
const { id } = ctx.params
const tokenUser = ctx.state.token.user

try {
/* DB에서 댓글 불러오기 */
const comment: Comment = await conn
.getRepository(Comment)
.findOne(ctx.params.id)
.getRepository(Comment)
.findOne(id, {
relations: ["likedUsers"],
})

/* 세션 유저 불러오기 */
const user: User = ctx.state.token.user
/* 유저 불러오기 */
const user: User = await conn
.getRepository(User)
.findOne(tokenUser.id, {
relations: ["profileImage"],
})

/* 세션의 유저와 좋아요 relation 해제 */
/* 유저와 좋아요 relation 해제 */
await conn
.createQueryBuilder()
.relation(Comment, "likedUsers")
.of(comment)
.remove(user)
.createQueryBuilder()
.relation(Comment, "likedUsers")
.of(comment)
.remove(user)

/* 세션 유저의 댓글 좋아요 수 1 감소 */
/* 유저의 댓글 좋아요 수 1 감소 */
--(user.likedCommentsCount)
await conn.manager.save(user)

/* DELETE 성공 응답 */
ctx.response.status = 200
ctx.body = {
likedUsers: comment.likedUsers.filter(x => x.id != user.id),
user,
}
}
catch (e) {
if (e.message ===
"Cannot read property 'user' of undefined"){
ctx.throw(401, e)
}
ctx.throw(400, e)
}

/* DELETE 성공 응답 */
ctx.response.status = 204
}

0 comments on commit 09ab74d

Please sign in to comment.