-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 내 댓글 조회 및 삭제 기능 추가 * test: 내 댓글 조회 및 삭제 테스트 추가 * feat: Comment 가져오는 Query 수정 * fix: 변수 Naming 변경
- Loading branch information
Showing
11 changed files
with
246 additions
and
12 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/example/daitssuapi/domain/main/controller/MyPageController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.daitssuapi.domain.main.controller | ||
|
||
import com.example.daitssuapi.common.dto.Response | ||
import com.example.daitssuapi.domain.main.dto.request.CommentDeleteRequest | ||
import com.example.daitssuapi.domain.main.dto.response.CommentResponse | ||
import com.example.daitssuapi.domain.main.service.MyPageService | ||
import org.springframework.web.bind.annotation.* | ||
|
||
@RestController | ||
@RequestMapping("/mypage") | ||
class MyPageController( | ||
private val myPageService: MyPageService | ||
) { | ||
@GetMapping("/{userId}/comments") | ||
fun getComments( | ||
@PathVariable userId: Long, | ||
): Response<List<CommentResponse>> = | ||
Response(data = myPageService.getComments(userId = userId)) | ||
|
||
@PatchMapping("/{userId}/comments") | ||
fun deleteComments( | ||
@PathVariable userId: Long, | ||
@RequestBody request: CommentDeleteRequest | ||
): Response<String> { | ||
myPageService.deleteComments(commentIds = request.commentIds) | ||
|
||
return Response(data = "Delete Ok") | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/example/daitssuapi/domain/main/dto/request/CommentDeleteRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.daitssuapi.domain.main.dto.request | ||
|
||
data class CommentDeleteRequest( | ||
val commentIds: List<Long> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/com/example/daitssuapi/domain/main/service/MyPageService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.daitssuapi.domain.main.service | ||
|
||
import com.example.daitssuapi.common.enums.ErrorCode | ||
import com.example.daitssuapi.common.exception.DefaultException | ||
import com.example.daitssuapi.domain.main.dto.response.CommentResponse | ||
import com.example.daitssuapi.domain.main.model.repository.CommentRepository | ||
import com.example.daitssuapi.domain.main.model.repository.UserRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
class MyPageService( | ||
private val commentRepository: CommentRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
fun getComments(userId: Long): List<CommentResponse> { | ||
userRepository.findByIdOrNull(id = userId) ?: throw DefaultException(errorCode = ErrorCode.USER_NOT_FOUND) | ||
|
||
return commentRepository.findByWriterIdAndIsDeletedFalseOrderByIdDesc(userId = userId).map { | ||
CommentResponse.of(it) | ||
} | ||
} | ||
|
||
@Transactional | ||
fun deleteComments(commentIds: List<Long>) { | ||
commentRepository.findAllById(commentIds).forEach { | ||
it.isDeleted = true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/kotlin/com/example/daitssuapi/domain/main/controller/MyPageControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.example.daitssuapi.domain.main.controller | ||
|
||
import com.example.daitssuapi.common.enums.ErrorCode | ||
import com.example.daitssuapi.domain.main.dto.request.CommentDeleteRequest | ||
import com.example.daitssuapi.domain.main.model.repository.CommentRepository | ||
import com.example.daitssuapi.domain.main.model.repository.UserRepository | ||
import com.example.daitssuapi.utils.ControllerTest | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import org.assertj.core.api.Assertions.* | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.http.MediaType | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers | ||
|
||
@ControllerTest | ||
class MyPageControllerTest( | ||
private val commentRepository: CommentRepository, | ||
private val userRepository: UserRepository | ||
) { | ||
@Autowired | ||
private lateinit var mockMvc: MockMvc | ||
|
||
private val baseUrl = "/mypage" | ||
|
||
@Test | ||
@DisplayName("성공_자신의 userId를 넘겨줄 때_자신의 댓글이 조회된다") | ||
fun successGetComments() { | ||
val userId = commentRepository.findAll()[0].writer.id | ||
val url = "$baseUrl/$userId/comments" | ||
|
||
mockMvc.perform( | ||
MockMvcRequestBuilders.get(url) | ||
).andExpect(MockMvcResultMatchers.status().isOk) | ||
} | ||
|
||
@Test | ||
@DisplayName("성공_자신의 userId를 넘겨줄 때_댓글이 없다면 조회되는 댓글이 없다") | ||
fun successGetCommentsNoComments() { | ||
val userIds = commentRepository.findAll().map { it.writer.id } | ||
val userId = userRepository.findAll().filter { !userIds.contains(it.id) }[0].id | ||
val url = "$baseUrl/$userId/comments" | ||
|
||
mockMvc.perform( | ||
MockMvcRequestBuilders.get(url) | ||
).andExpect(MockMvcResultMatchers.status().isOk) | ||
} | ||
|
||
@Test | ||
@DisplayName("실패_올바르지 않은 userId를 넘겨주면_댓글 조회에 실패한다") | ||
fun failGetCommentsUserNotExist() { | ||
val userId = 0 | ||
val url = "$baseUrl/$userId/comments" | ||
|
||
mockMvc.perform( | ||
MockMvcRequestBuilders.get(url) | ||
).andExpect(MockMvcResultMatchers.status().isBadRequest) | ||
.andExpect(MockMvcResultMatchers.jsonPath("$.code").value(ErrorCode.USER_NOT_FOUND.code)) | ||
} | ||
|
||
@Test | ||
@DisplayName("성공_작성한 댓글을 넘겨줄 때_비활성화 상태가 된다") | ||
fun successDeleteComments() { | ||
val comments = commentRepository.findAll() | ||
val userId = comments[0].writer.id | ||
val commentDeleteRequest = CommentDeleteRequest( | ||
commentIds = comments.filter { it.writer.id == userId }.filter { !it.isDeleted }.map { it.id } | ||
) | ||
val url = "$baseUrl/$userId/comments" | ||
val request = jacksonObjectMapper().writeValueAsString(commentDeleteRequest) | ||
|
||
mockMvc.perform( | ||
MockMvcRequestBuilders.patch(url) | ||
.content(request) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
).andExpect(MockMvcResultMatchers.status().isOk) | ||
|
||
comments.filter { it.writer.id == userId }.forEach { comment -> | ||
assertThat(comment.isDeleted).isTrue() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/test/kotlin/com/example/daitssuapi/domain/main/service/MyPageServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.example.daitssuapi.domain.main.service | ||
|
||
import com.example.daitssuapi.common.exception.DefaultException | ||
import com.example.daitssuapi.domain.main.dto.request.CommentDeleteRequest | ||
import com.example.daitssuapi.domain.main.model.repository.CommentRepository | ||
import com.example.daitssuapi.domain.main.model.repository.UserRepository | ||
import com.example.daitssuapi.utils.IntegrationTest | ||
import org.assertj.core.api.Assertions.* | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
@IntegrationTest | ||
class MyPageServiceTest( | ||
private val commentRepository: CommentRepository, | ||
private val userRepository: UserRepository, | ||
private val myPageService: MyPageService | ||
) { | ||
@Test | ||
@DisplayName("성공_자신의 userId를 넘겨줄 때_자신의 댓글이 조회된다") | ||
fun successGetComments() { | ||
val comments = commentRepository.findAll() | ||
val userId = comments[0].writer.id | ||
|
||
val findComments = myPageService.getComments(userId = userId) | ||
|
||
assertThat(findComments.size).isEqualTo(comments.filter { it.writer.id == userId }.filter { !it.isDeleted }.size) | ||
} | ||
|
||
@Test | ||
@DisplayName("성공_자신의 userId를 넘겨줄 때_댓글이 없다면 조회되는 댓글이 없다") | ||
fun successGetCommentsNoComments() { | ||
val userIds = commentRepository.findAll().map { it.writer.id } | ||
val userId = userRepository.findAll().filter { !userIds.contains(it.id) }[0].id | ||
|
||
val findComments = myPageService.getComments(userId = userId) | ||
|
||
assertThat(findComments.size).isZero | ||
} | ||
|
||
@Test | ||
@DisplayName("실패_올바르지 않은 userId를 넘겨주면_댓글 조회에 실패한다") | ||
fun failGetCommentsUserNotExist() { | ||
val userId = 0L | ||
|
||
assertThrows<DefaultException> { myPageService.getComments(userId = userId) } | ||
} | ||
|
||
@Test | ||
@DisplayName("성공_작성한 댓글을 넘겨줄 때_비활성화 상태가 된다") | ||
fun successDeleteComments() { | ||
val comments = commentRepository.findAll() | ||
val userId = comments[0].writer.id | ||
val request = CommentDeleteRequest( | ||
commentIds = comments.filter { it.writer.id == userId }.map { it.id } | ||
) | ||
|
||
myPageService.deleteComments(commentIds = request.commentIds) | ||
|
||
comments.filter { it.writer.id == userId }.forEach { comment -> | ||
assertThat(comment.isDeleted).isTrue() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters