TIL ~ 24.04.05

20240108 TIL

wlsds00_ 2024. 1. 8. 21:00

1. 코드카타

 

직사각형 별찍기 / 최대공약수와 최소공배수 / 3진법 뒤집기

38. 직사각형 별찍기 fun main(args: Array) { val (a, b) = readLine()!!.split(' ').map(String::toInt) print(("*".repeat(a) + "\n" ).repeat(b)) } readLine으로 입력받은 a 와 b 의 값을 split(' ') 에서 공백을 기준으로 나눠 문자열

ds-36500.tistory.com

 

2. 뉴스피드 프로젝트

1단계. 프로젝트 아이디어 구상하기
2단계. API 명세 작성하기
3단계. ERD 작성하기
4단계. 와이어프레임 작성하기


까지 먼저 작성한 후 개발에 들어갔다. 어떤식으로 작업할지 회의를 하다가, 패키지 구조나 파일 형태같은게 통일성이 있으면 좋겠다고 의견을 냈는데 내가 패키지 구조와 초안을 작성하게 되어서 완료 후 깃을 통해 공유했다.

원래는 포스트 기능도 crud를 하나씩 나눠서 작업하기로 했는데 전달과정에서 착오가 있었는지 한분이 다 하시게 되어서 나머지 사람들이 코멘트 기능을 하나씩 나누어 맡기로 했다. 내가 맡은 부분은 코멘트 작성이었다.

아무래도 이후 기능들도 내 파트 작성이 완성되어야 구현이 가능하다보니 최대한 빠르게 만드는데 집중했다.

 

CreateCommentRequest / CommentResponse

data class CreateCommentRequest (
    val content: String,
    val userId: Long,
    val postid: Long,
    val userName: String,
)

data class CommentResponse(
    val content: String,
    val userId: Long,
    val userName: String,
)

 

CommentEntity

@Entity
@Table(name = "comments")
class CommentEntity (
    @Column(name = "content", nullable = false)
    var content: String,

    @Column(name = "userid", nullable = false)
    var userId: Long,

    @Column(name = "username", nullable = false)
    var userName: String,

    @ManyToOne
    @JoinColumn(name = "post_id", nullable = false)
    var post: PostEntity,
){
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id: Long = 0
}

fun CommentEntity.toResponse(): CommentResponse {
    return CommentResponse(
        content = content,
        userId = userId,
        userName = userName
    )
}

 

CommentRepository

interface CommentRepository: JpaRepository <CommentEntity, Long> {
}

 

CommentService

interface CommentService {
    fun createComment(postId: Long, request: CreateCommentRequest): CommentResponse
}

 

CommentServiceImpl

@Service
class CommentServiceImpl(
    private val postRepository: PostRepository,
    private val commentRepository: CommentRepository
) : CommentService {

    @Transactional
    override fun createComment(
        postId: Long,
        request: CreateCommentRequest
    ): CommentResponse {
    
        val post = postRepository.findByIdOrNull(postId) 
        		?: throw ModelNotFoundException("Post", postId)
                
        return commentRepository.save(
            CommentEntity(
                content = request.content,
                userId = request.userId,
                userName = request.userName,
                post = post
            )
            
        ).toResponse()
    }
}