Skip to content

Commit

Permalink
Merge pull request #96 from snuhcs-course/feat/back-error_message
Browse files Browse the repository at this point in the history
Feat/back error message
  • Loading branch information
dawitfamanu authored Dec 4, 2023
2 parents 9ba63da + f299e95 commit 8ef4b62
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import com.goliath.emojihub.springboot.domain.reaction.dao.ReactionDao
import com.goliath.emojihub.springboot.domain.user.dao.UserDao
import com.goliath.emojihub.springboot.global.exception.CustomHttp400
import com.goliath.emojihub.springboot.global.exception.CustomHttp403
import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INDEX_OUT_OF_BOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.COUNT_OUT_OF_BOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_CREATED
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_SAVED
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_UNSAVED
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.EMOJI_DELETE_FORBIDDEN
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.EMOJI_NOT_FOUND
import com.goliath.emojihub.springboot.global.util.getDateTimeNow
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
Expand All @@ -29,18 +37,18 @@ class EmojiService(

fun getEmojis(sortByDate: Int, index: Int, count: Int): List<EmojiDto> {
// index는 양의 정수여야 함
if (index <= 0) throw CustomHttp400("Index should be positive integer.")
if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND)
// count는 0보다 커야 함
if (count <= 0) throw CustomHttp400("Count should be positive integer.")
if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND)
return emojiDao.getEmojis(sortByDate, index, count)
}

fun getMyEmojis(username: String, field: String, index: Int, count: Int): List<EmojiDto> {
// index는 양의 정수여야 함
if (index <= 0) throw CustomHttp400("Index should be positive integer.")
if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND)
// count는 0보다 커야 함
if (count <= 0) throw CustomHttp400("Count should be positive integer.")
val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.")
if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
val emojiIdList = if (field == CREATED_EMOJIS) {
user.created_emojis
} else {
Expand All @@ -66,7 +74,7 @@ class EmojiService(
}

fun getEmoji(emojiId: String): EmojiDto? {
if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404("Emoji doesn't exist.")
if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404(EMOJI_NOT_FOUND)
return emojiDao.getEmoji(emojiId)
}

Expand All @@ -84,31 +92,31 @@ class EmojiService(

fun saveEmoji(username: String, emojiId: String) {
if (emojiDao.existsEmoji(emojiId).not())
throw CustomHttp404("Emoji doesn't exist.")
val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.")
throw CustomHttp404(EMOJI_NOT_FOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
if (user.created_emojis?.contains(emojiId) == true)
throw CustomHttp403("User created this emoji.")
throw CustomHttp403(USER_CREATED)
if (user.saved_emojis?.contains(emojiId) == true)
throw CustomHttp403("User already saved this emoji.")
throw CustomHttp403(USER_ALREADY_SAVED)
emojiDao.numSavedChange(emojiId, 1)
userDao.insertId(username, emojiId, SAVED_EMOJIS)
}

fun unSaveEmoji(username: String, emojiId: String) {
if (emojiDao.existsEmoji(emojiId).not())
throw CustomHttp404("Emoji doesn't exist.")
val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.")
throw CustomHttp404(EMOJI_NOT_FOUND)
val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND)
if (user.created_emojis?.contains(emojiId) == true)
throw CustomHttp403("User created this emoji.")
throw CustomHttp403(USER_CREATED)
if (user.saved_emojis == null || !user.saved_emojis!!.contains(emojiId))
throw CustomHttp403("User already unsaved this emoji.")
throw CustomHttp403(USER_ALREADY_UNSAVED)
emojiDao.numSavedChange(emojiId, -1)
userDao.deleteId(username, emojiId, SAVED_EMOJIS)
}

fun deleteEmoji(username: String, emojiId: String) {
val emoji = emojiDao.getEmoji(emojiId) ?: throw CustomHttp404("Emoji doesn't exist.")
if (username != emoji.created_by) throw CustomHttp403("You can't delete this emoji.")
val emoji = emojiDao.getEmoji(emojiId) ?: throw CustomHttp404(EMOJI_NOT_FOUND)
if (username != emoji.created_by) throw CustomHttp403(EMOJI_DELETE_FORBIDDEN)
// delete file and thumbnail in DB
val fileBlobName = username + "_" + emoji.created_at + ".mp4"
val thumbnailBlobName = username + "_" + emoji.created_at + ".jpeg"
Expand All @@ -117,7 +125,7 @@ class EmojiService(
// delete all reactions(and reaction id in posts) using this emoji
val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID)
for (reaction in reactions) {
postDao.deleteReactionId(reaction.post_id, reaction.id)
postDao.deleteReaction(reaction.post_id, reaction.id)
reactionDao.deleteReaction(reaction.id)
}
// delete all saved_emoji ids in users
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goliath.emojihub.springboot.domain.post.dao

import com.goliath.emojihub.springboot.domain.post.dto.PostDto
import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode
import com.goliath.emojihub.springboot.global.util.getDateTimeNow
import com.google.cloud.firestore.*
import lombok.extern.slf4j.Slf4j
Expand Down Expand Up @@ -33,7 +34,8 @@ class PostDao(
// 정렬
val postQuery = db.collection(POST_COLLECTION_NAME).orderBy(CREATED_AT, Query.Direction.DESCENDING)
// 페이지네이션
val documents: List<QueryDocumentSnapshot> = postQuery.offset((index - 1) * count).limit(count).get().get().documents
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 All @@ -45,7 +47,8 @@ class PostDao(
val postsRef = db.collection(POST_COLLECTION_NAME)
val postQuery = postsRef.whereEqualTo("created_by", username)
.orderBy(CREATED_AT, Query.Direction.DESCENDING)
val documents: List<QueryDocumentSnapshot> = postQuery.offset((index - 1) * count).limit(count).get().get().documents
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 Expand Up @@ -75,13 +78,23 @@ class PostDao(
db.collection(POST_COLLECTION_NAME).document(postId).delete()
}

fun insertReactionId(postId: String, reactionId: String) {
fun insertReaction(postId: String, reactionId: String, emojiUnicode: String) {
val postRef = db.collection(POST_COLLECTION_NAME).document(postId)
postRef.update(REACTIONS, FieldValue.arrayUnion(reactionId))
val reactionWithEmojiUnicode = ReactionWithEmojiUnicode(
id = reactionId,
emoji_unicode = emojiUnicode
)
postRef.update(REACTIONS, FieldValue.arrayUnion(reactionWithEmojiUnicode))
}

fun deleteReactionId(postId: String, reactionId: String) {
fun deleteReaction(postId: String, reactionId: String) {
val postRef = db.collection(POST_COLLECTION_NAME).document(postId)
postRef.update(REACTIONS, FieldValue.arrayRemove(reactionId))
val post = postRef.get().get().toObject(PostDto::class.java)
for (reaction in post!!.reactions) {
if (reaction.id == reactionId) {
postRef.update(REACTIONS, FieldValue.arrayRemove(reaction))
return
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data class PostDto(
var content: String = "",
var created_at: String = "",
var modified_at: String = "",
var reactions: MutableList<String>? = mutableListOf()
var reactions: MutableList<ReactionWithEmojiUnicode> = mutableListOf(),
) {
constructor(username: String, content: String, dateTime: String) : this() {
val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Expand All @@ -22,4 +22,9 @@ data class PostDto(
created_at = dateTime
modified_at = dateTime
}
}
}

data class ReactionWithEmojiUnicode(
var id: String = "",
var emoji_unicode: String = ""
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import com.goliath.emojihub.springboot.domain.user.dao.UserDao
import com.goliath.emojihub.springboot.global.exception.CustomHttp400
import com.goliath.emojihub.springboot.global.exception.CustomHttp403
import com.goliath.emojihub.springboot.global.exception.CustomHttp404
import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INDEX_OUT_OF_BOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.COUNT_OUT_OF_BOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.POST_NOT_FOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.POST_UPDATE_FORBIDDEN
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.POST_DELETE_FORBIDDEN
import org.springframework.stereotype.Service

@Service
Expand All @@ -17,7 +23,7 @@ class PostService(
) {

companion object {
const val CREATED_POSTS= "created_posts"
const val CREATED_POSTS = "created_posts"
}

fun postPost(username: String, content: String) {
Expand All @@ -26,43 +32,41 @@ class PostService(
}

fun getPosts(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 (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND)
if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND)
return postDao.getPosts(index, count)
}

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.")
if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND)
if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND)
if (!userDao.existUser(username)) throw CustomHttp404(USER_NOT_FOUND)
return postDao.getMyPosts(username, index, count)
}

fun getPost(postId: String): PostDto? {
if (postDao.existPost(postId).not())
throw CustomHttp404("Post doesn't exist.")
throw CustomHttp404(POST_NOT_FOUND)
return postDao.getPost(postId)
}

fun patchPost(username: String, postId: String, content: String) {
val post = postDao.getPost(postId) ?: throw CustomHttp404("Post doesn't exist.")
val post = postDao.getPost(postId) ?: throw CustomHttp404(POST_NOT_FOUND)
if (username != post.created_by)
throw CustomHttp403("You can't update this post.")
throw CustomHttp403(POST_UPDATE_FORBIDDEN)
postDao.updatePost(postId, content)
}

fun deletePost(username: String, postId: String) {
val post = postDao.getPost(postId) ?: throw CustomHttp404("Post doesn't exist.")
val post = postDao.getPost(postId) ?: throw CustomHttp404(POST_NOT_FOUND)
if (username != post.created_by)
throw CustomHttp403("You can't delete this post.")
throw CustomHttp403(POST_DELETE_FORBIDDEN)
// delete post(and post's reactions)
val reactionIds = post.reactions
if (reactionIds != null) {
for (reactionId in reactionIds) {
val reaction = reactionDao.getReaction(reactionId) ?: continue
if (postId != reaction.post_id) continue
reactionDao.deleteReaction(reactionId)
}
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)
}
// delete created_post id in user
userDao.deleteId(username, postId, CREATED_POSTS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto
import com.goliath.emojihub.springboot.domain.user.dao.UserDao
import com.goliath.emojihub.springboot.global.exception.CustomHttp403
import com.goliath.emojihub.springboot.global.exception.CustomHttp404
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_REACT
import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.REACTION_DELETE_FORBIDDEN
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.POST_NOT_FOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.EMOJI_NOT_FOUND
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.REACTION_NOT_FOUND
import org.springframework.stereotype.Service

@Service
Expand All @@ -22,25 +28,25 @@ class ReactionService(


fun getReactionsOfPost(postId: String): List<ReactionDto> {
if (!postDao.existPost(postId)) throw CustomHttp404("Post doesn't exist.")
if (!postDao.existPost(postId)) throw CustomHttp404(POST_NOT_FOUND)
return reactionDao.getReactionsWithField(postId, POST_ID)
}

fun postReaction(username: String, postId: String, emojiId: String) {
if (!userDao.existUser(username)) throw CustomHttp404("User doesn't exist.")
if (!postDao.existPost(postId)) throw CustomHttp404("Post doesn't exist.")
if (!emojiDao.existsEmoji(emojiId)) throw CustomHttp404("Emoji doesn't exist.")
if (reactionDao.existSameReaction(username, postId, emojiId)) throw CustomHttp403("User already react to this post with this emoji.")
if (!userDao.existUser(username)) throw CustomHttp404(USER_NOT_FOUND)
if (!postDao.existPost(postId)) throw CustomHttp404(POST_NOT_FOUND)
val emoji = emojiDao.getEmoji(emojiId) ?: throw CustomHttp404(EMOJI_NOT_FOUND)
if (reactionDao.existSameReaction(username, postId, emojiId)) throw CustomHttp403(USER_ALREADY_REACT)
val reaction = reactionDao.insertReaction(username, postId, emojiId)
postDao.insertReactionId(postId, reaction.id)
postDao.insertReaction(postId, reaction.id, emoji.emoji_unicode)
}

fun deleteReaction(username: String, reactionId: String) {
val reaction = reactionDao.getReaction(reactionId) ?: throw CustomHttp404("Reaction doesn't exist.")
val reaction = reactionDao.getReaction(reactionId) ?: throw CustomHttp404(REACTION_NOT_FOUND)
if (username != reaction.created_by)
throw CustomHttp403("You can't delete this reaction.")
throw CustomHttp403(REACTION_DELETE_FORBIDDEN)
// delete reaction id in post
postDao.deleteReactionId(reaction.post_id, reactionId)
postDao.deleteReaction(reaction.post_id, reactionId)
// delete reaction
reactionDao.deleteReaction(reactionId)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.goliath.emojihub.springboot.domain.user.service
import com.goliath.emojihub.springboot.domain.user.dao.UserDao
import com.goliath.emojihub.springboot.domain.user.model.UserAdapter
import com.goliath.emojihub.springboot.global.exception.CustomHttp404
import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_FROM_TOKEN_NOT_FOUND
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
import org.springframework.stereotype.Service
Expand All @@ -12,7 +13,7 @@ class UserDetailService(
private val userDao: UserDao
): UserDetailsService {
override fun loadUserByUsername(username: String): UserDetails {
val userDto = userDao.getUser(username) ?: throw CustomHttp404("Username from the token doesn't exist.")
val userDto = userDao.getUser(username) ?: throw CustomHttp404(USER_FROM_TOKEN_NOT_FOUND)
return UserAdapter(userDto)
}

Expand Down
Loading

0 comments on commit 8ef4b62

Please sign in to comment.