Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: feat pagination in getMyCreatedEmojis, getMySavedEmojis, getMyPosts #79

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,20 @@ class EmojiController(private val emojiService: EmojiService) {

@GetMapping("/me/created")
fun getMyCreatedEmojis(
@CurrentUser username: String
@CurrentUser username: String,
@RequestParam(value = "index", defaultValue = 1.toString()) index: Int,
@RequestParam(value = "count", defaultValue = 10.toString()) count: Int,
): ResponseEntity<List<EmojiDto>> {
return ResponseEntity.ok(emojiService.getMyEmojis(username, CREATED_EMOJIS))
return ResponseEntity.ok(emojiService.getMyEmojis(username, CREATED_EMOJIS, index, count))
}

@GetMapping("/me/saved")
fun getMySavedEmojis(
@CurrentUser username: String
@CurrentUser username: String,
@RequestParam(value = "index", defaultValue = 1.toString()) index: Int,
@RequestParam(value = "count", defaultValue = 10.toString()) count: Int,
): ResponseEntity<List<EmojiDto>> {
return ResponseEntity.ok(emojiService.getMyEmojis(username, SAVED_EMOJIS))
return ResponseEntity.ok(emojiService.getMyEmojis(username, SAVED_EMOJIS, index, count))
}

@GetMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.goliath.emojihub.springboot.global.exception.CustomHttp403
import com.goliath.emojihub.springboot.global.util.getDateTimeNow
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.lang.Integer.min

@Service
class EmojiService(
Expand All @@ -34,24 +35,32 @@ class EmojiService(
return emojiDao.getEmojis(sortByDate, index, count)
}

fun getMyEmojis(username: String, field: String): List<EmojiDto> {
fun getMyEmojis(username: String, field: String, index: Int, count: Int): List<EmojiDto> {
// index는 양의 정수여야 함
if (index <= 0) throw CustomHttp400("Index should be positive integer.")
// count는 0보다 커야 함
if (count <= 0) throw CustomHttp400("Count should be positive integer.")
val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.")
val emojiIdList = if (field == CREATED_EMOJIS) {
user.created_emojis
} else {
user.saved_emojis
}
val emojiList = mutableListOf<EmojiDto>()
var emojiList = mutableListOf<EmojiDto>()
if (emojiIdList != null && emojiIdList.size != 0) {
for (emojiId in emojiIdList) {
val emoji = emojiDao.getEmoji(emojiId)
if (emoji != null) {
emojiList.add(emoji)
}
val emoji = emojiDao.getEmoji(emojiId) ?: continue
emojiList.add(emoji)
}
// sort
if (emojiList.size != 0) {
emojiList.sortByDescending { it.created_at }
}
// pagination
emojiList = emojiList.subList(
min((index - 1) * count, emojiList.size - 1),
min(index * count, emojiList.size)
)
}
return emojiList
}
Expand All @@ -61,7 +70,13 @@ class EmojiService(
return emojiDao.getEmoji(emojiId)
}

fun postEmoji(username: String, file: MultipartFile, thumbnail: MultipartFile, emojiUnicode: String, emojiLabel: String) {
fun postEmoji(
username: String,
file: MultipartFile,
thumbnail: MultipartFile,
emojiUnicode: String,
emojiLabel: String
) {
val dateTime = getDateTimeNow()
val emoji = emojiDao.insertEmoji(username, file, thumbnail, emojiUnicode, emojiLabel, dateTime)
userDao.insertId(username, emoji.id, CREATED_EMOJIS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class PostController(private val postService: PostService) {

@GetMapping("/me")
fun getMyPosts(
@CurrentUser username: String
): ResponseEntity<List<PostDto>>{
return ResponseEntity.ok(postService.getMyPosts(username))
@CurrentUser username: String,
@RequestParam(value = "index", defaultValue = 1.toString()) index: Int,
@RequestParam(value = "count", defaultValue = 10.toString()) count: Int
): ResponseEntity<List<PostDto>> {
return ResponseEntity.ok(postService.getMyPosts(username, index, count))
}

@GetMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class PostDao(
return list
}

fun getMyPosts(username: String): List<PostDto> {
fun getMyPosts(username: String, index: Int, count: Int): List<PostDto> {
val list = mutableListOf<PostDto>()
val postsRef = db.collection(POST_COLLECTION_NAME)
val postQuery = postsRef.whereEqualTo("created_by", username)
.orderBy("created_at", Query.Direction.DESCENDING)
val documents = postQuery.get().get().documents
.orderBy(CREATED_AT, Query.Direction.DESCENDING)
val documents: List<QueryDocumentSnapshot> = postQuery.offset((index - 1) * count).limit(count).get().get().documents
for (document in documents) {
list.add(document.toObject(PostDto::class.java))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class PostService(

fun getPosts(index: Int, count: Int): List<PostDto> {
if (index <= 0) throw CustomHttp400("Index should be positive integer.")
// count는 0보다 커야 함
if (count <= 0) throw CustomHttp400("Count should be positive integer.")
return postDao.getPosts(index, count)
}

fun getMyPosts(username: String): List<PostDto> {
fun getMyPosts(username: String, index: Int, count: Int): List<PostDto> {
if (index <= 0) throw CustomHttp400("Index should be positive integer.")
if (count <= 0) throw CustomHttp400("Count should be positive integer.")
if (!userDao.existUser(username)) throw CustomHttp404("User doesn't exist.")
return postDao.getMyPosts(username)
return postDao.getMyPosts(username, index, count)
}

fun getPost(postId: String): PostDto? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,16 @@ internal class EmojiControllerTest @Autowired constructor(
fun getMyCreatedEmojis() {
// given
val username = "custom_username"
Mockito.`when`(emojiService.getMyEmojis(username, CREATED_EMOJIS)).thenReturn(emojiList)
val index = 1
val count = testDto.createdEmojiSize
Mockito.`when`(emojiService.getMyEmojis(username, CREATED_EMOJIS, index, count)).thenReturn(emojiList)

// when
val result = this.mockMvc.perform(get("/api/emoji/me/created"))
val result = this.mockMvc.perform(
get("/api/emoji/me/created")
.param("index", index.toString())
.param("count", count.toString())
)

// then
result.andExpect(status().isOk)
Expand All @@ -97,7 +103,7 @@ internal class EmojiControllerTest @Autowired constructor(
.andExpect(jsonPath("$[0].emoji_label").value(emojiList[0].emoji_label))
.andExpect(jsonPath("$[0].created_at").value(emojiList[0].created_at))
.andExpect(jsonPath("$[0].num_saved").value(emojiList[0].num_saved))
verify(emojiService, times(1)).getMyEmojis(username, CREATED_EMOJIS)
verify(emojiService, times(1)).getMyEmojis(username, CREATED_EMOJIS, index, count)
}

@Test
Expand All @@ -106,10 +112,16 @@ internal class EmojiControllerTest @Autowired constructor(
fun getMySavedEmojis() {
// given
val username = "custom_username"
Mockito.`when`(emojiService.getMyEmojis(username, SAVED_EMOJIS)).thenReturn(emojiList)
val index = 1
val count = testDto.savedEmojiSize
Mockito.`when`(emojiService.getMyEmojis(username, SAVED_EMOJIS, index, count)).thenReturn(emojiList)

// when
val result = this.mockMvc.perform(get("/api/emoji/me/saved"))
val result = this.mockMvc.perform(
get("/api/emoji/me/saved")
.param("index", index.toString())
.param("count", count.toString())
)

// then
result.andExpect(status().isOk)
Expand All @@ -122,7 +134,7 @@ internal class EmojiControllerTest @Autowired constructor(
.andExpect(jsonPath("$[0].emoji_label").value(emojiList[0].emoji_label))
.andExpect(jsonPath("$[0].created_at").value(emojiList[0].created_at))
.andExpect(jsonPath("$[0].num_saved").value(emojiList[0].num_saved))
verify(emojiService, times(1)).getMyEmojis(username, SAVED_EMOJIS)
verify(emojiService, times(1)).getMyEmojis(username, SAVED_EMOJIS, index, count)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,20 @@ internal class EmojiServiceTest {
val user = testDto.userList[0]
val username = user.username
val wrongUsername = "wrong_username"
val index = 1
val countCreated = testDto.createdEmojiSize
val countSaved = testDto.savedEmojiSize
Mockito.`when`(userDao.getUser(username)).thenReturn(user)
Mockito.`when`(userDao.getUser(wrongUsername)).thenReturn(null)
for (emoji in testDto.emojiList) {
Mockito.`when`(emojiDao.getEmoji(emoji.id)).thenReturn(emoji)
}

// when
val createdEmojisResult = emojiService.getMyEmojis(username, CREATED_EMOJIS)
val savedEmojisResult = emojiService.getMyEmojis(username, SAVED_EMOJIS)
val createdEmojisResult = emojiService.getMyEmojis(username, CREATED_EMOJIS, index, countCreated)
val savedEmojisResult = emojiService.getMyEmojis(username, SAVED_EMOJIS, index, countSaved)
val assertThrows = assertThrows(CustomHttp404::class.java) {
emojiService.getMyEmojis(wrongUsername, CREATED_EMOJIS)
emojiService.getMyEmojis(wrongUsername, CREATED_EMOJIS, index, countCreated)
}

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
@WebMvcTest(PostController::class)
internal class PostControllerTest @Autowired constructor(
private val mockMvc: MockMvc
){
) {

@Autowired
private lateinit var objectMapper: ObjectMapper
Expand Down Expand Up @@ -103,16 +103,22 @@ internal class PostControllerTest @Autowired constructor(
// given
val username = "custom_username"
val realUsername = postList[0].created_by
val index = 1
val count = testDto.postSize
val posts = mutableListOf<PostDto>()
for (post in postList) {
if (post.created_by == realUsername) {
posts.add(post)
}
}
given(postService.getMyPosts(username)).willReturn(posts)
given(postService.getMyPosts(username, index, count)).willReturn(posts)

// when
val result = this.mockMvc.perform(get("/api/post/me"))
val result = this.mockMvc.perform(
get("/api/post/me")
.param("index", index.toString())
.param("count", count.toString())
)

// then
result.andExpect(status().isOk)
Expand All @@ -123,7 +129,7 @@ internal class PostControllerTest @Autowired constructor(
.andExpect(jsonPath("$[0].content").value(posts[0].content))
.andExpect(jsonPath("$[0].created_at").value(posts[0].created_at))
.andExpect(jsonPath("$[0].modified_at").value(posts[0].modified_at))
verify(postService, times(1)).getMyPosts(username)
verify(postService, times(1)).getMyPosts(username, index, count)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ internal class PostDaoTest {
fun getMyPosts() {
// given
val username = userList[0].username
val index = 1
val count = testDto.postSize
val postListForUser = mutableListOf<PostDto>()
postListForUser.add(postList[1])
postListForUser.add(postList[0])
Mockito.`when`(db.collection(POST_COLLECTION_NAME))
.thenReturn(testDB.collection(POST_COLLECTION_NAME))

// when
val result = postDao.getMyPosts(username)
val result = postDao.getMyPosts(username, index, count)

// then
assertAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,17 @@ internal class PostServiceTest {
// given
val username = "test_username"
val wrongUsername = "wrong_username"
val index = 1
val count = testDto.postSize
val list = testDto.postList
Mockito.`when`(userDao.existUser(username)).thenReturn(true)
Mockito.`when`(userDao.existUser(wrongUsername)).thenReturn(false)
Mockito.`when`(postDao.getMyPosts(username)).thenReturn(list)
Mockito.`when`(postDao.getMyPosts(username, index, count)).thenReturn(list)

// when
val result = postService.getMyPosts(username)
val result = postService.getMyPosts(username, index, count)
val assertThrows = assertThrows(CustomHttp404::class.java) {
postService.getMyPosts(wrongUsername)
postService.getMyPosts(wrongUsername, index, count)
}

// then
Expand All @@ -111,7 +113,7 @@ internal class PostServiceTest {
)
verify(userDao, times(1)).existUser(username)
verify(userDao, times(1)).existUser(wrongUsername)
verify(postDao, times(1)).getMyPosts(username)
verify(postDao, times(1)).getMyPosts(username, index, count)
}

@Test
Expand Down