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

[5기 김용상, 임수진] Spring Boot JPA 게시판 구현 미션 제출합니다. #268

Open
wants to merge 11 commits into
base: suzzingv
Choose a base branch
from

Conversation

suzzingV
Copy link

📌 과제 설명

Spring Data JPA를 사용한 게시판

👩‍💻 요구 사항과 구현 내용

SpringDataJPA 를 설정한다.

  • datasource : mysql

엔티티 구성

  • 회원(User)
  • id (PK) (auto increment)
  • name
  • age
  • hobby
  • created_at
  • 게시글(Post)
  • id (PK) (auto increment)
  • title
  • content
  • created_at
  • 회원과 게시글에 대한 연관관계 설정.
  • 회원과 게시글은 1:N 단방향 관계.
  • 게시글 Repository 구현 (PostRepository)
  • 회원 Repository 구현 (UserRepository)

API 구현

  • 회원 등록 (POST "/api/users")
  • 회원 조회
  • 페이징 조회 (GET "/api/users")
  • 단건 조회 (GET "/api/users/{id}")
  • 회원 수정 (POST "/api/users/{id}")
  • 게시글 작성 (POST "/api/posts")
  • 게시글 조회
  • 페이징 조회 (GET "/api/posts")
  • 단건 조회 (GET "/api/posts/{id}")
  • 게시글 수정 (POST "/api/posts/{id}")
  • REST-DOCS를 이용한 문서화

✅ PR 포인트 & 궁금한 점

User와 Post가 1:N 단방향 관계입니다. 양방향으로 설정하면 연관관계 편의 메서드랑 , 1:N중 1쪽에서 너무 많은 역할을 갖게될 것 같아서 단방향으로 만들었습니다. 이 방식이 적절한지, 현업에서 양방향을 많이 사용하는지 궁금합니다.

Copy link

@SeokRae SeokRae left a comment

Choose a reason for hiding this comment

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

안녕하세요. 용상님, 수진님 멘토 알입니다.

JPA 과제 진행해주셨군요.! 고생하셨습니다.
과제의 요구사항과 학습한 내용, 어떤 것들을 고민했는지 추가해주시면 더 좋을 것 같습니다.

✅ PR 포인트 & 궁금한 점

양방향? 단방향?

테이블 설계는 고민이 많이 필요로 하는 부분 중에 하나입니다.
우선 제가 경험한 회사의 기준에서는 FK 기능을 사용하지 않았습니다.
물론 어떤 회사들은 사용한다고 가끔 블로그 보긴 했지만, 제약 조건으로 인해서 이를 사용하지 않는 경우가 더 많은 것 같습니다.

  1. CRUD의 제한
  2. 부모 테이블 -> 자식 테이블 트랜잭션 관리 영향 전파
  3. 데이터 마이그레이션 및 백업 시 casecade 문제

다른 리뷰에서도 이야기 했던 내용대로 위 사례가 있을 수 있습니다.

양방향, 단방향과 관련된 글을 찾다보니 계속 타고타고 들어가게 되는 것 같네요.
개인적인 생각에는 다대일 단방향이 가장 단순한 모습이 아닐까 싶습니다.
물론 이 생각의 전제조건은 테이블 설계가 우선되고 JPA 엔티티를 맞춰가는 순서입니다.
혹시나 중간 매핑 테이블과 관련된 많은 필드와 로직이 들어간다면, 또 다른 이야기를 해야할 수도 있습니다.!

Comment on lines +39 to +73
ext {
snippetsDir = file('build/generated-snippets')
}

asciidoctor {
inputs.dir snippetsDir
configurations 'asciidoctorExt'
dependsOn test
}

asciidoctor.doFirst {
delete file("src/main/resources/static/docs")
}

bootJar {
dependsOn asciidoctor
from("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
}

task copyDocument(type: Copy) {
dependsOn asciidoctor
from file("$buildDir/docs/asciidoc")
into file("src/main/resources/static/docs")
}

build {
dependsOn copyDocument
}

test {
useJUnitPlatform()
outputs.dir snippetsDir
}
Copy link

Choose a reason for hiding this comment

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

restdocs 관련된 설정들이 보여지는데, 어떤 것들인지 소개 해주실수 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

plugins {
id 'org.asciidoctor.jvm.convert' version '3.3.2'
}
: AsciiDoc 파일을 다른 형식으로 변환하는 기능을 제공하는 기능입니다.

ext {
snippetsDir = file('build/generated-snippets')
}
: Gradle 빌드 스크립트에서 ext 블록을 사용하여 snippetsDir라는 확장 속성을 정의하는 부분입니다. 코드 스니펫, 테스트 결과 또는 문서의 일부 등이 여기에 저장될 수 있습니다.

asciidoctor {
inputs.dir snippetsDir
configurations 'asciidoctorExt'
dependsOn test
}
: asciidoctor는 AsciiDoctor 플러그인의 설정 블록을 지칭합니다.

inputs.dir snippetsDir: 여기서 snippetsDir은 앞서 정의한 디렉토리 경로를 참조하며, AsciiDoctor에게 입력으로 제공될 디렉토리를 설정합니다. AsciiDoctor 변환 작업에 이 디렉토리의 내용이 사용됩니다.

configurations 'asciidoctorExt': 구성(configuration)을 설정합니다. AsciiDoctor 플러그인의 추가적인 확장 기능을 위한 설정입니다.

dependsOn test: 현재 설정된 작업이 테스트 작업(test)에 종속되어 있다는 것을 나타냅니다.

asciidoctor.doFirst {
delete file("src/main/resources/static/docs")
}
: doFirst는 특정 작업이 실행되기 전에 수행할 작업을 정의하는 Gradle의 메서드입니다. delete file("src/main/resources/static/docs")는 src/main/resources/static/docs 경로에 있는 파일 또는 디렉토리를 삭제하는 명령어입니다. 이 코드는 AsciiDoctor 변환 작업을 실행하기 전에 해당 경로에 있는 파일이나 디렉토리를 삭제하도록 합니다.

bootJar {
dependsOn asciidoctor
from("${asciidoctor.outputDir}/html5") {
into 'static/docs'
}
}
: bootJar는 Spring Boot 애플리케이션을 JAR 파일로 패키징하는 작업을 정의하는 부분입니다.

dependsOn asciidoctor: bootJar 작업이 asciidoctor 작업에 종속되어 있다는 것을 나타냅니다. 따라서 asciidoctor 작업이 먼저 실행된 후에 bootJar 작업이 실행됩니다.

from("${asciidoctor.outputDir}/html5"): AsciiDoctor의 출력 디렉토리에서 HTML5로 된 결과물을 가져오도록 설정합니다.

into 'static/docs': 가져온 HTML5 결과물을 static/docs 디렉토리에 넣도록 설정하고 있습니다.

task copyDocument(type: Copy) {
dependsOn asciidoctor
from file("$buildDir/docs/asciidoc")
into file("src/main/resources/static/docs")
}
: Copy 유형의 작업으로 정의되어 있으며, 다른 작업인 asciidoctor에 의존하고 있습니다. dependsOn asciidoctor 라인은 asciidoctor 작업이 먼저 실행된 후에 copyDocument 작업이 실행되어야 함을 의미합니다. AsciiDoc 문서가 있는 디렉토리("$buildDir/docs/asciidoc")에서 파일을 가져와서(from) src/main/resources/static/docs 디렉토리로 복사(into)하는 것으로 보입니다.

build {
dependsOn copyDocument
}
: build 단계가 copyDocument 작업이 완료된 후에 실행될 것임을 의미합니다.

test {
useJUnitPlatform()
outputs.dir snippetsDir
}
: outputs.dir snippetsDir는 테스트 작업이 완료된 후 생성된 결과물이나 출력물을 snippetsDir 디렉토리로 저장하도록 합니다. snippetsDir은 출력 디렉토리를 나타내며, 테스트가 생성하는 어떤 산출물을 저장하는 데 사용됩니다.

Comment on lines +2 to +6
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/board?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=Asia/Seoul&characterEncoding=UTF-8
username: root
password:
Copy link

Choose a reason for hiding this comment

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

로컬 개발 환경 구축이 필요한 부분은 간단하게 docker 스크립트로 제공하는 것도 좋을 것 같습니다.
어떤 개발환경에서 테스트를 했구나? 어떤 부분들을 고려해야할까? 라는 생각이 들 것 같네요.!

@CreatedDate
private LocalDateTime createdAt;

public static User toEntity(UserDto.Request request) {
Copy link

Choose a reason for hiding this comment

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

request 객체가 entity에 영향을 줄 수 있는 부분인 것 같은데
toEntity role을 대신해줄 수 있는 부분이 있을까요?

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class BoardApplicationTests {
Copy link

Choose a reason for hiding this comment

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

불필요한 테스트 클래스는 제거하는 것이 좋겠습니다.!


@AutoConfigureRestDocs
@AutoConfigureMockMvc
@MockBean(JpaMetamodelMappingContext.class)
Copy link

Choose a reason for hiding this comment

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

오 이건 어떤 역할을 하는 클래스인가요?

Copy link
Author

Choose a reason for hiding this comment

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

테스트를 위해 Mock을 쓸 때 클래스,필드 정보들을 한번에 불러와주는 역할을 합니다!

private final UserService service;

@GetMapping
public ApiResult<List<Response>> findAll() {
Copy link

Choose a reason for hiding this comment

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

User에는 페이징이 필요가 없는 걸까요?

Choose a reason for hiding this comment

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

단순히 사용자 입장에서만 생각해서 추가를 하지 않았는데, 관리자 기능을 생각하면 필요할 것 같습니다.
관리자 기능이 필요할 때 User 페이징 기능을 추가해보도록 하겠습니다!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants