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

Fix/back refactoring #121

Closed
wants to merge 3 commits into from
Closed
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 @@ -53,13 +53,12 @@ class EmojiService(
}
val emojiArrayDeque = ArrayDeque<EmojiDto>()
var emojiList = listOf<EmojiDto>()
if (emojiIdList != null && emojiIdList.size != 0) {
if (emojiIdList.size != 0) {
for (emojiId in emojiIdList) {
val emoji = emojiDao.getEmoji(emojiId) ?: continue
emojiArrayDeque.addFirst(emoji)
}
// pagination
// FIXME: remind - CreatedEmojiList should already be sorted by created_at
val start = min((index - 1) * count, emojiArrayDeque.size - 1)
val end = min(index * count, emojiArrayDeque.size)
emojiList = emojiArrayDeque.toList().subList(start, end)
Expand Down Expand Up @@ -88,9 +87,9 @@ class EmojiService(
if (emojiDao.existsEmoji(emojiId).not())
throw CustomHttp404(EMOJI_NOT_FOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
if (user.created_emojis?.contains(emojiId) == true)
if (user.created_emojis.contains(emojiId))
throw CustomHttp403(USER_CREATED)
if (user.saved_emojis?.contains(emojiId) == true)
if (user.saved_emojis.contains(emojiId))
throw CustomHttp403(USER_ALREADY_SAVED)
emojiDao.numSavedChange(emojiId, 1)
userDao.insertId(username, emojiId, SAVED_EMOJIS.string)
Expand All @@ -100,9 +99,9 @@ class EmojiService(
if (emojiDao.existsEmoji(emojiId).not())
throw CustomHttp404(EMOJI_NOT_FOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
if (user.created_emojis?.contains(emojiId) == true)
if (user.created_emojis.contains(emojiId))
throw CustomHttp403(USER_CREATED)
if (user.saved_emojis == null || !user.saved_emojis!!.contains(emojiId))
if (!user.saved_emojis.contains(emojiId))
throw CustomHttp403(USER_ALREADY_UNSAVED)
emojiDao.numSavedChange(emojiId, -1)
userDao.deleteId(username, emojiId, SAVED_EMOJIS.string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ data class UserDto (
var email: String = "",
var username: String = "",
var password: String = "",
var created_emojis: MutableList<String>? = mutableListOf(),
var saved_emojis: MutableList<String>? = mutableListOf(),
var created_posts: MutableList<String>? = mutableListOf(),
var created_emojis: MutableList<String> = mutableListOf(),
var saved_emojis: MutableList<String> = mutableListOf(),
var created_posts: MutableList<String> = mutableListOf(),
) {
data class AuthToken(
val accessToken: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,37 @@ class UserService(
reactionDao.deleteReaction(reaction.id)
}
// delete all posts(and posts' reactions) created by user
if (postIds != null) {
for (postId in postIds) {
val post = postDao.getPost(postId) ?: continue
if (username != post.created_by) continue
val reactionWithEmojiUnicodes = post.reactions
for (reactionWithEmojiUnicode in reactionWithEmojiUnicodes) {
val reaction = reactionDao.getReaction(reactionWithEmojiUnicode.id) ?: continue
if (postId != reaction.post_id) continue
reactionDao.deleteReaction(reactionWithEmojiUnicode.id)
}
postDao.deletePost(postId)
for (postId in postIds) {
val post = postDao.getPost(postId) ?: continue
if (username != post.created_by) continue
val reactionWithEmojiUnicodes = post.reactions
for (reactionWithEmojiUnicode in reactionWithEmojiUnicodes) {
val reaction = reactionDao.getReaction(reactionWithEmojiUnicode.id) ?: continue
if (postId != reaction.post_id) continue
reactionDao.deleteReaction(reactionWithEmojiUnicode.id)
}
postDao.deletePost(postId)
}
// delete all emojis(and reactions(and reaction id in posts) using these emojis) created by user
if (createdEmojiIds != null) {
for (emojiId in createdEmojiIds) {
val emoji = emojiDao.getEmoji(emojiId) ?: continue
if (username != emoji.created_by) continue
val fileBlobName = username + "_" + emoji.created_at + ".mp4"
val thumbnailBlobName = username + "_" + emoji.created_at + ".jpeg"
emojiDao.deleteFileInStorage(fileBlobName)
emojiDao.deleteFileInStorage(thumbnailBlobName)
val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID.string)
for (reaction in reactions) {
postDao.deleteReaction(reaction.post_id, reaction.id)
reactionDao.deleteReaction(reaction.id)
}
userDao.deleteAllSavedEmojiId(emojiId)
emojiDao.deleteEmoji(emojiId)
for (emojiId in createdEmojiIds) {
val emoji = emojiDao.getEmoji(emojiId) ?: continue
if (username != emoji.created_by) continue
val fileBlobName = username + "_" + emoji.created_at + ".mp4"
val thumbnailBlobName = username + "_" + emoji.created_at + ".jpeg"
emojiDao.deleteFileInStorage(fileBlobName)
emojiDao.deleteFileInStorage(thumbnailBlobName)
val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID.string)
for (reaction in reactions) {
postDao.deleteReaction(reaction.post_id, reaction.id)
reactionDao.deleteReaction(reaction.id)
}
userDao.deleteAllSavedEmojiId(emojiId)
emojiDao.deleteEmoji(emojiId)
}
// unsave all emojis saved by user
if (savedEmojiIds != null) {
for (emojiId in savedEmojiIds) {
if (!emojiDao.existsEmoji(emojiId)) continue
emojiDao.numSavedChange(emojiId, -1)
}
for (emojiId in savedEmojiIds) {
if (!emojiDao.existsEmoji(emojiId)) continue
emojiDao.numSavedChange(emojiId, -1)
}
// delete user
return userDao.deleteUser(username)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성
// 각 user는 다른 user들의 첫번째 created emoji를 save함 (자신의 다음 user부터 순서대로)
// 각 user 'i'는 각 user 'j'(자신 포함)의 첫번째 post에 자신의 이모지(첫번째), 저장한 이모지(첫번째)로 reaction
// userSize 만큼 user 생성
() {
{
var userSize: Int
var userList: MutableList<UserDto>

Expand Down Expand Up @@ -60,7 +60,7 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성
video_url = "test_video_url${i}_${j}",
)
)
userList[i].created_emojis!!.add(emojiId)
userList[i].created_emojis.add(emojiId)
}
// 각 user는 postSize 만큼 post 생성
for (j in 0 until postSize) {
Expand All @@ -74,16 +74,16 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성
modified_at = "test_modified_at${i}_${j}",
)
)
userList[i].created_posts!!.add(postId)
userList[i].created_posts.add(postId)
}
}
for (i in 0 until userSize) {
for (j in i + 1 until userSize) {
userList[i].saved_emojis!!.add(emojiList[createdEmojiSize * j].id)
userList[i].saved_emojis.add(emojiList[createdEmojiSize * j].id)
emojiList[createdEmojiSize * j].num_saved++
}
for (j in 0 until i) {
userList[i].saved_emojis!!.add(emojiList[createdEmojiSize * j].id)
userList[i].saved_emojis.add(emojiList[createdEmojiSize * j].id)
emojiList[createdEmojiSize * j].num_saved++
}
}
Expand All @@ -110,7 +110,7 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성
id = reactionIdWithCreatedEmoji,
created_by = userList[i].username,
post_id = postList[postSize * j].id,
emoji_id = userList[i].created_emojis!![0],
emoji_id = userList[i].created_emojis[0],
created_at = "test_created_at${i}_${j}_c"
)
)
Expand All @@ -119,7 +119,7 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성
id = reactionIdWithSavedEmoji,
created_by = userList[i].username,
post_id = postList[postSize * j].id,
emoji_id = userList[i].saved_emojis!![0],
emoji_id = userList[i].saved_emojis[0],
created_at = "test_created_at${i}_${j}_s"
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ internal class EmojiDaoTest {
FirebaseApp.initializeApp(options)
}
testDB = FirestoreClient.getFirestore()
// Initialization of firestore Database Emojis
val emojiDocuments = testDB.collection(EMOJI_COLLECTION_NAME.string).get().get().documents
for (document in emojiDocuments) {
val emoji = document.toObject(EmojiDto::class.java)
testDB.collection(EMOJI_COLLECTION_NAME.string).document(emoji.id).delete()
}
for (emoji in testDto.emojiList) {
testDB.collection(EMOJI_COLLECTION_NAME.string).document(emoji.id).set(emoji)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ internal class EmojiServiceTest {
)
verify(userDao, times(2)).getUser(username)
verify(userDao, times(1)).getUser(wrongUsername)
for (emojiId in user.created_emojis!!) {
for (emojiId in user.created_emojis) {
verify(emojiDao, times(1)).getEmoji(emojiId)
}
for (emojiId in user.saved_emojis!!) {
for (emojiId in user.saved_emojis) {
verify(emojiDao, times(1)).getEmoji(emojiId)
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@ internal class EmojiServiceTest {
// given
val user = testDto.userList[0]
val username = user.username
val emojiId = user.created_emojis!![0]
val emojiId = user.created_emojis[0]
Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true)
Mockito.`when`(userDao.getUser(username)).thenReturn(user)

Expand All @@ -259,7 +259,7 @@ internal class EmojiServiceTest {
// given
val user = testDto.userList[0]
val username = user.username
val emojiId = user.saved_emojis!![0]
val emojiId = user.saved_emojis[0]
Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true)
Mockito.`when`(userDao.getUser(username)).thenReturn(user)

Expand All @@ -280,7 +280,7 @@ internal class EmojiServiceTest {
// given
val user = testDto.userList[0]
val username = user.username
val emojiId = user.saved_emojis!![0]
val emojiId = user.saved_emojis[0]
Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true)
Mockito.`when`(userDao.getUser(username)).thenReturn(user)

Expand Down Expand Up @@ -337,7 +337,7 @@ internal class EmojiServiceTest {
// given
val user = testDto.userList[0]
val username = user.username
val emojiId = user.created_emojis!![0]
val emojiId = user.created_emojis[0]
Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true)
Mockito.`when`(userDao.getUser(username)).thenReturn(user)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ internal class PostDaoTest {
FirebaseApp.initializeApp(options)
}
testDB = FirestoreClient.getFirestore()
// Initialization of firestore Database Posts
val postDocuments = testDB.collection(POST_COLLECTION_NAME.string).get().get().documents
for (document in postDocuments) {
val post = document.toObject(PostDto::class.java)
testDB.collection(POST_COLLECTION_NAME.string).document(post.id).delete()
}
for (post in testDto.postList) {
testDB.collection(POST_COLLECTION_NAME.string).document(post.id).set(post)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ internal class ReactionDaoTest {
FirebaseApp.initializeApp(options)
}
testDB = FirestoreClient.getFirestore()
// Initialization of firestore Database Posts
val reactionDocuments = testDB.collection(REACTION_COLLECTION_NAME.string).get().get().documents
for (document in reactionDocuments) {
val reaction = document.toObject(ReactionDto::class.java)
testDB.collection(REACTION_COLLECTION_NAME.string).document(reaction.id).delete()
}
for (reaction in testDto.reactionList) {
testDB.collection(REACTION_COLLECTION_NAME.string).document(reaction.id).set(reaction)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ internal class UserControllerTest @Autowired constructor(
.andExpect(jsonPath("$.email").value(user.email))
.andExpect(jsonPath("$.username").value(user.username))
.andExpect(jsonPath("$.password").value(user.password))
.andExpect(jsonPath("$.created_emojis.length()", equalTo(user.created_emojis!!.size)))
.andExpect(jsonPath("$.saved_emojis.length()", equalTo(user.saved_emojis!!.size)))
.andExpect(jsonPath("$.created_posts.length()", equalTo(user.created_posts!!.size)))
.andExpect(jsonPath("$.created_emojis.length()", equalTo(user.created_emojis.size)))
.andExpect(jsonPath("$.saved_emojis.length()", equalTo(user.saved_emojis.size)))
.andExpect(jsonPath("$.created_posts.length()", equalTo(user.created_posts.size)))
verify(userService, times(1)).getMe(username)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ internal class UserDaoTest {
FirebaseApp.initializeApp(options)
}
testDB = FirestoreClient.getFirestore()
// Initialization of firestore database Users
val userDocuments = testDB.collection(USER_COLLECTION_NAME.string).get().get().documents
for (document in userDocuments) {
val user = document.toObject(UserDto::class.java)
testDB.collection(USER_COLLECTION_NAME.string).document(user.username).delete()
}
for (user in testDto.userList) {
testDB.collection(USER_COLLECTION_NAME.string).document(user.username).set(user)
}
}
}

Expand Down Expand Up @@ -186,7 +195,7 @@ internal class UserDaoTest {
// then
var result = userDao.getUser(username)
var a = 1
while (!result!!.created_posts!!.contains(postId) && a <= 5) {
while (!result!!.created_posts.contains(postId) && a <= 5) {
result = userDao.getUser(username)
a++
}
Expand All @@ -196,7 +205,7 @@ internal class UserDaoTest {
userDao.deleteId(username, postId, CREATED_POSTS.string)
result = userDao.getUser(username)
var b = 1
while (result!!.created_posts!!.contains(postId) && b <= 5) {
while (result!!.created_posts.contains(postId) && b <= 5) {
result = userDao.getUser(username)
b++
}
Expand All @@ -207,7 +216,7 @@ internal class UserDaoTest {
fun deleteId() {
// given
val username = userList[1].username
val postId = userList[1].created_posts!![1]
val postId = userList[1].created_posts[1]
Mockito.`when`(db.collection(USER_COLLECTION_NAME.string))
.thenReturn(testDB.collection(USER_COLLECTION_NAME.string))

Expand All @@ -217,7 +226,7 @@ internal class UserDaoTest {
// then
var result = userDao.getUser(username)
var a = 1
while (result!!.created_posts!!.size != 1 && a <= 5) {
while (result!!.created_posts.size != 1 && a <= 5) {
result = userDao.getUser(username)
a++
}
Expand All @@ -227,7 +236,7 @@ internal class UserDaoTest {
userDao.insertId(username, postId, CREATED_POSTS.string)
result = userDao.getUser(username)
var b = 1
while (!result!!.created_posts!!.contains(postId) && b <= 5) {
while (!result!!.created_posts.contains(postId) && b <= 5) {
result = userDao.getUser(username)
b++
}
Expand All @@ -238,7 +247,7 @@ internal class UserDaoTest {
fun deleteAllSavedEmojiId() {
// given
val username = userList[0].username
val emojiId = userList[0].saved_emojis!![0]
val emojiId = userList[0].saved_emojis[0]
Mockito.`when`(db.collection(USER_COLLECTION_NAME.string))
.thenReturn(testDB.collection(USER_COLLECTION_NAME.string))

Expand All @@ -248,21 +257,21 @@ internal class UserDaoTest {
// then
var result = userDao.getUser(username)
var a = 1
while (result!!.saved_emojis!!.contains(emojiId) && a <= 5) {
while (result!!.saved_emojis.contains(emojiId) && a <= 5) {
result = userDao.getUser(username)
a++
}
assertEquals(result.saved_emojis!!.contains(emojiId), false)
assertEquals(result.saved_emojis.contains(emojiId), false)

// after work
userDao.insertId(username, emojiId, SAVED_EMOJIS.string)
result = userDao.getUser(username)
var b = 1
while (!result!!.saved_emojis!!.contains(emojiId) && b <= 5) {
while (!result!!.saved_emojis.contains(emojiId) && b <= 5) {
userDao.insertId(username, emojiId, SAVED_EMOJIS.string)
result = userDao.getUser(username)
b++
}
assertEquals(result.saved_emojis!!.contains(emojiId), true)
assertEquals(result.saved_emojis.contains(emojiId), true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ internal class UserServiceTest {
Mockito.`when`(emojiDao.existsEmoji(emoji.id)).thenReturn(true)
}
for (userDto in testDto.userList) {
val firstEmojiId = userDto.created_emojis!![0]
val firstEmojiId = userDto.created_emojis[0]
val reactions = mutableListOf<ReactionDto>()
for (reaction in testDto.reactionList) {
if (reaction.emoji_id == firstEmojiId) {
Expand Down
Loading