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

๐Ÿ„ :: (Meogo-14)refactor #50

Merged
merged 7 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("io.jsonwebtoken:jjwt:0.9.1")
implementation("org.springdoc:springdoc-openapi-ui:1.6.9")
implementation("com.amazonaws:aws-java-sdk-s3:1.12.281")
implementation("com.amazonaws:aws-java-sdk-s3:1.12.529")
implementation("com.google.firebase:firebase-admin:8.1.0")
implementation("javax.servlet:javax.servlet-api:4.0.1")
compileOnly("org.projectlombok:lombok")
runtimeOnly("com.mysql:mysql-connector-j")
annotationProcessor("org.projectlombok:lombok")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.meogo.domain.user.facade.UserFacade
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

@Service
class CreateCommentService(
private val commentRepository: CommentRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class CommentService(
class QueryCommentService(
private val commentRepository: CommentRepository
) {
@Transactional(readOnly = true)
fun getCommentResponses(basicComments: List<Comment>): List<CommentResponse> {
fun execute(basicComments: List<Comment>): List<CommentResponse> {
return basicComments.map { basicComment ->
val replies = commentRepository.findAllByComment(basicComment)
CommentResponse(basicComment, replies.map { CommentResponse(it) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.meogo.domain.post.service

import org.meogo.domain.comment.domain.CommentRepository
import org.meogo.domain.comment.presentation.dto.response.CommentListResponse
import org.meogo.domain.comment.service.CommentService
import org.meogo.domain.comment.service.QueryCommentService
import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.presentation.dto.response.PostDetailResponse
import org.springframework.stereotype.Service
Expand All @@ -12,15 +12,15 @@ import org.springframework.transaction.annotation.Transactional
class QueryPostDetailService(
private val postRepository: PostRepository,
private val commentRepository: CommentRepository,
private val commentService: CommentService
private val queryCommentService: QueryCommentService
) {
@Transactional(readOnly = true)
fun execute(postId: Long): PostDetailResponse {
val post = postRepository.findById(postId)

val basicComments = commentRepository.findAllByPost(post)

val replies = commentService.getCommentResponses(basicComments)
val replies = queryCommentService.execute(basicComments)

val contentListResponse = CommentListResponse(
count = replies.size + basicComments.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.meogo.domain.question.service

import org.meogo.domain.comment.domain.CommentRepository
import org.meogo.domain.comment.presentation.dto.response.CommentListResponse
import org.meogo.domain.comment.service.CommentService
import org.meogo.domain.comment.service.QueryCommentService
import org.meogo.domain.question.domain.QuestionRepository
import org.meogo.domain.question.presentation.dto.response.QuestionDetailResponse
import org.springframework.stereotype.Service
Expand All @@ -12,15 +12,15 @@ import org.springframework.transaction.annotation.Transactional
class QueryDetailQuestionService(
private val questionRepository: QuestionRepository,
private val commentRepository: CommentRepository,
private val commentService: CommentService
private val queryCommentService: QueryCommentService
) {
@Transactional(readOnly = true)
fun execute(questionId: Long): QuestionDetailResponse {
val question = questionRepository.findById(questionId)

val basicComments = commentRepository.findAllByQuestion(question)

val replies = commentService.getCommentResponses(basicComments)
val replies = queryCommentService.execute(basicComments)

val contentListResponse = CommentListResponse(
count = replies.size + basicComments.size,
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/org/meogo/domain/user/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import javax.persistence.EnumType
import javax.persistence.Enumerated

@Entity
class User(
id: UUID? = null,
data class User(
override val id: UUID? = null,

@Column(nullable = false, length = 4)
var name: String,
Expand All @@ -25,7 +25,10 @@ class User(
var profile: String,

@Enumerated(EnumType.STRING)
val role: UserRole
val role: UserRole,

@Column(name = "device_token", nullable = false)
val deviceToken: String? = null
meltapplee marked this conversation as resolved.
Show resolved Hide resolved
) : BaseUUIDEntity(id) {
fun updateProfile(name: String, enrolledSchool: Int, profile: String): User {
this.name = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.meogo.domain.user.presentation.dto.request.UserSignUpRequest
import org.meogo.global.jwt.JwtTokenProvider
import org.meogo.global.jwt.dto.TokenResponse
import org.meogo.global.s3.FileUtil
import org.meogo.global.s3.Path
import org.springframework.beans.factory.annotation.Value
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
Expand All @@ -23,15 +22,13 @@ class UserSignUpService(

) {
fun execute(request: UserSignUpRequest): TokenResponse {
val image = fileUtil.generateObjectUrl(defaultImage, Path.USER)

val user = User(
name = request.name,
accountId = request.accountId,
password = passwordEncoder.encode(request.password),
enrolledSchool = request.enrolledSchool?.toInt(),
role = UserRole.USER,
profile = image
profile = defaultImage
)
userRepository.save(user)

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/meogo/global/base/BaseUUIDEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ abstract class BaseUUIDEntity(
columnDefinition = "BINARY(16)",
nullable = false
)
val id: UUID?
open val id: UUID?
Copy link

@coderabbitai coderabbitai bot Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ’ก Codebase verification

id ํ”„๋กœํผํ‹ฐ ์žฌ์ •์˜๋กœ ์ธํ•œ ๋ฌธ์ œ ๋ฐœ๊ฒฌ

User.kt ํŒŒ์ผ์—์„œ id ํ”„๋กœํผํ‹ฐ๋ฅผ override val id: UUID? = null๋กœ ์žฌ์ •์˜ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. JPA ์—”ํ‹ฐํ‹ฐ์˜ ID๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ null์ผ ์ˆ˜ ์—†์œผ๋ฏ€๋กœ, ์ด๋กœ ์ธํ•ด ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • id ํ”„๋กœํผํ‹ฐ๋ฅผ nullableํ•˜์ง€ ์•Š๋„๋ก ๋ณ€๊ฒฝํ•  ๊ฒƒ
  • id์˜ ์ดˆ๊ธฐํ™”๋ฅผ ์ ์ ˆํžˆ ์ฒ˜๋ฆฌํ•˜์—ฌ null ๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š๋„๋ก ํ•  ๊ฒƒ
๐Ÿ”— Analysis chain

id ํ”„๋กœํผํ‹ฐ๋ฅผ open์œผ๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒƒ์— ๋Œ€ํ•œ ๊ณ ๋ ค์‚ฌํ•ญ

id ํ”„๋กœํผํ‹ฐ๋ฅผ open์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์ด๋ฅผ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์–ด ์œ ์—ฐ์„ฑ์ด ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด๋กœ ์ธํ•ด ๋ช‡ ๊ฐ€์ง€ ์ž ์žฌ์ ์ธ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

  1. ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ๋ถ€์ ์ ˆํ•˜๊ฒŒ ๊ด€๋ฆฌํ•  ๊ฒฝ์šฐ ID์˜ ๊ณ ์œ ์„ฑ์ด ์†์ƒ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์—”ํ‹ฐํ‹ฐ ์ƒ์„ฑ ํ›„ ID๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด JPA์—์„œ ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๋™์ž‘์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๋ณ€๊ฒฝ์˜ ์˜ํ–ฅ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด ๋‹ค์Œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•ด์ฃผ์„ธ์š”:

์ด ๋ณ€๊ฒฝ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๋ฌธ์„œํ™”๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด:

/**
 * ๊ธฐ๋ณธ UUID ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค.
 * 
 * @property id ์—”ํ‹ฐํ‹ฐ์˜ ๊ณ ์œ  ์‹๋ณ„์ž. ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,
 * ID์˜ ๊ณ ์œ ์„ฑ๊ณผ JPA์˜ ์ •์ƒ ๋™์ž‘์„ ๋ณด์žฅํ•˜๊ธฐ ์œ„ํ•ด ์‹ ์ค‘ํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
 */
abstract class BaseUUIDEntity(
    // ... ๊ธฐ์กด ์ฝ”๋“œ ...
)
๐Ÿ Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: BaseUUIDEntity๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋“ค์„ ์ฐพ์•„ id ํ”„๋กœํผํ‹ฐ ์žฌ์ •์˜ ์—ฌ๋ถ€ ํ™•์ธ

echo "BaseUUIDEntity๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค๋“ค:"
rg --type kotlin "class .+ : BaseUUIDEntity" -g '!BaseUUIDEntity.kt'

echo "\nid ํ”„๋กœํผํ‹ฐ๋ฅผ ์žฌ์ •์˜ํ•˜๋Š” ํด๋ž˜์Šค๋“ค:"
rg --type kotlin "override val id" -g '!BaseUUIDEntity.kt'

Length of output: 337

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–ด๋–ป๊ฒŒ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„๊นŒ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meltapplee, BaseUUIDEntity์˜ id ํ”„๋กœํผํ‹ฐ๋ฅผ nullableํ•˜์ง€ ์•Š๋„๋ก ์ˆ˜์ •ํ•˜๊ณ , ํ•˜์œ„ ํด๋ž˜์Šค์—์„œ id๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด JPA ์—”ํ‹ฐํ‹ฐ์˜ ID๊ฐ€ ํ•ญ์ƒ null์ด ์•„๋‹Œ ๊ฐ’์œผ๋กœ ๊ด€๋ฆฌ๋˜์–ด ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ˆ˜์ •๋œ BaseUUIDEntity๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

@MappedSuperclass
abstract class BaseUUIDEntity(
    @Id
    @GeneratedValue(generator = "uuid2")
    @Column(
        columnDefinition = "BINARY(16)",
        nullable = false
    )
    val id: UUID
)

๊ทธ๋ฆฌ๊ณ  User ํด๋ž˜์Šค์—์„œ๋Š” id ํ”„๋กœํผํ‹ฐ๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š๊ณ , BaseUUIDEntity๋ฅผ ๊ทธ๋Œ€๋กœ ์ƒ์†๋ฐ›๋„๋ก ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ํ˜„์žฌ User ํด๋ž˜์Šค์—์„œ id๋ฅผ ์žฌ์ •์˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”:

@Entity
class User(
    // ๊ธฐ์กด ์ฝ”๋“œ...
) : BaseUUIDEntity(
    // `id` ํ”„๋กœํผํ‹ฐ๋ฅผ ์žฌ์ •์˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
)

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด JPA๊ฐ€ ์ž๋™์œผ๋กœ id๋ฅผ ๊ด€๋ฆฌํ•˜๋ฉฐ, id๊ฐ€ null์ธ ์ƒํƒœ๋กœ ๋‚จ์•„์žˆ๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User ์—”ํ‹ฐํ‹ฐ์—์„œ id๋ฅผ ์–ด๋Š๋ฐฉ์‹์œผ๋กœ ์ •์˜ํ•ด์•ผํ•ด?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meltapplee, User ์—”ํ‹ฐํ‹ฐ์—์„œ๋Š” ๋ณ„๋„๋กœ id ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•˜์ง€ ์•Š๊ณ  BaseUUIDEntity๋ฅผ ์ƒ์†๋ฐ›์•„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ User ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

@Entity
class User(
    // ๋‹ค๋ฅธ ํ”„๋กœํผํ‹ฐ๋“ค...
) : BaseUUIDEntity() {
    // ํด๋ž˜์Šค ๋‚ด์šฉ...
}

BaseUUIDEntity์—์„œ ์ด๋ฏธ id ํ”„๋กœํผํ‹ฐ๋ฅผ ์ •์˜ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ, User ์—”ํ‹ฐํ‹ฐ์—์„œ id๋ฅผ ์žฌ์ •์˜ํ•˜๊ฑฐ๋‚˜ ์ดˆ๊ธฐํ™”ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด JPA๊ฐ€ ์ž๋™์œผ๋กœ id ๊ฐ’์„ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜์—ฌ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์„ ์œ ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

)
Loading