Skip to content
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ bin/
out/
!**/src/main/**/out/
!**/src/test/**/out/
application.yml

### NetBeans ###
/nbproject/private/
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0'

compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/likelion/session/SessionApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@SpringBootApplication // Spring Boot 설정 + 컴포넌트 스캔 + 자동 설정 포함
public class SessionApplication {

public static void main(String[] args) {
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/likelion/session/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

import java.util.List;

@RestController
@RequestMapping("/boards")
@RequiredArgsConstructor
@RestController // REST API 컨트롤러 선언 (JSON 반환)
@RequestMapping("/boards") // 기본 url 경로 설정 (/boards)
@RequiredArgsConstructor // final 필드 생성자 자동 생성
public class BoardController {

private final ;
private final BoardService boardService;

/*
게시글 생성
Expand All @@ -32,12 +32,12 @@ public class BoardController {
-> 결과 반환
-> JSON 응답
*/
@Operation(
@Operation( // Swagger API 설명
summary = "게시글 생성",
description = "새로운 게시글을 생성합니다."
)
@
public ResponseEntity<BoardResponse> create(@RequestBody BoardCreateRequest request) {
@PostMapping // POST 요청 처리 (/boards)
public ResponseEntity<BoardResponse> create(@RequestBody BoardCreateRequest request) { // JSON Body 값 가져오기
BoardResponse response = boardService.create(request);
return ResponseEntity.ok(response);
}
Expand All @@ -47,7 +47,7 @@ public ResponseEntity<BoardResponse> create(@RequestBody BoardCreateRequest requ
summary = "게시글 전체 조회",
description = "등록된 모든 게시글을 조회합니다."
)
@
@GetMapping // GET 요청 처리
public ResponseEntity<List<BoardResponse>> findAll() {
List<BoardResponse> response = boardService.findAll();
return ResponseEntity.ok(response);
Expand All @@ -59,7 +59,7 @@ public ResponseEntity<List<BoardResponse>> findAll() {
description = "id로 특정 게시글을 조회합니다."
)
@GetMapping("/{id}")
public ResponseEntity<BoardResponse> findById(@PathVariable Long id) {
public ResponseEntity<BoardResponse> findById(@PathVariable Long id) { // URL 경로 값 가져오기
BoardResponse response = boardService.findById(id);
return ResponseEntity.ok(response);
}
Expand All @@ -69,7 +69,7 @@ public ResponseEntity<BoardResponse> findById(@PathVariable Long id) {
summary = "게시글 수정",
description = "id로 특정 게시글의 제목과 내용을 수정합니다."
)
@("/{id}")
@PutMapping("/{id}") // PUT 요청 처리 (/boards/{id})
public ResponseEntity<BoardResponse> update(@PathVariable Long id,
@RequestBody BoardUpdateRequest request) {
BoardResponse response = boardService.update(id, request);
Expand All @@ -81,7 +81,7 @@ public ResponseEntity<BoardResponse> update(@PathVariable Long id,
summary = "게시글 삭제",
description = "id로 특정 게시글을 삭제합니다."
)
@("/{id}")
@DeleteMapping("/{id}") // DELETE 요청 처리
public ResponseEntity<Void> delete(@PathVariable Long id) {
boardService.delete(id);
return ResponseEntity.noContent().build();
Expand Down
31 changes: 19 additions & 12 deletions src/main/java/com/likelion/session/domain/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,41 @@

import java.time.LocalDateTime;

@Getter // getter 메서드 자동 생성
@Entity // 해당 클래스 DB 테이블로 인식하고 관리
@Table(name = "boards")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter // Getter 메서드 자동 생성
@Entity // JPA 엔티티 선언 (DB 테이블과 매핑)
@Table(name = "boards") // DB 테이블 이름 지정
@NoArgsConstructor(access = AccessLevel.PROTECTED) // 기본 생성자 생성 (외부에서 new 방지)
@AllArgsConstructor // 전체 필드 생성자 생성
public class Board {

@Id
@Id // 기본키(PK) 지정
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long ;
// PK 자동 증가 (DB auto increment)
private Long id;

// 게시글 제목
@Column(nullable = false, length = 100)
private String ;
// nullable=false : NOT NULL
// length=100 : VARCHAR(100)
private String title;

// 게시글 내용
@Column(nullable = false, columnDefinition = "TEXT")
private String ;
// columnDefinition="TEXT" : TEXT 타입으로 생성
private String content;

// 작성자
@Column(nullable = false, length = 30)
private String ;
// NOT NULL + VARCHAR(30)
private String writer;

// 생성 시간
@Column(nullable = false)
private LocalDateTime ;
private LocalDateTime createdAt;

// 수정 시간
@Column(nullable = false)
private LocalDateTime ;
private LocalDateTime updatedAt;


public Board(String title, String content, String writer) {
Expand All @@ -47,12 +52,14 @@ public Board(String title, String content, String writer) {
}

@PrePersist
// Entity 저장 전에 실행 (insert 전에 자동 실행)
public void prePersist() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}

@PreUpdate
// Entity 수정 전에 실행 (update 전에 자동 실행)
public void preUpdate() {
this.updatedAt = LocalDateTime.now();
}
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/likelion/session/dto/BoardCreateRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Getter // 모든 필드의 getter 메서드 자동 생성
@Setter // 모든 필드의 setter 메서드 자동 생성
@NoArgsConstructor // 기본 생성자 생성 (JSON → 객체 변환 시 필요)
@AllArgsConstructor // 모든 필드를 포함한 생성자 생성
public class BoardCreateRequest {
// 넘겨주고 싶은 정보: 제목(title), 내용(content), 작성자(writer)
private String ;
private String ;
private String ;
private String title;
private String content;
private String writer;
}
6 changes: 3 additions & 3 deletions src/main/java/com/likelion/session/dto/BoardResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import java.time.LocalDateTime;

@Getter
@AllArgsConstructor
@Builder
@Getter // 모든 필드의 getter 메서드 자동 생성
@AllArgsConstructor // 모든 필드를 포함하는 생성자 생성
@Builder // Builder 패턴 사용 가능 (객체 생성 시 가독성 ↑)
public class BoardResponse {

// 돌려주고 싶은 응답: id, title, content, writer, createdAt, updatedAt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

@Getter
@Setter
@NoArgsConstructor
@Getter // getter 메서드 자동 생성
@Setter // setter 메서드 자동 생성
@NoArgsConstructor // 기본 생성자 생성 (JSON → 객체 변환 시 필요)
public class BoardUpdateRequest {
@NotBlank(message = "제목은 필수입니다.")
// null, "", 공백(" ") 모두 허용 안함
@Size(max = 100, message = "제목은 100자 이하로 입력해주세요.")
// 문자열 길이 100자 이하 제한
private String title;

@NotBlank(message = "내용은 필수입니다.")
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/likelion/session/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
@Service // Service 계층 Bean 등록
@RequiredArgsConstructor // final 필드 생성자 자동 생성
@Transactional // 트랜잭션 관리
public class BoardService {

private final ;
private final BoardRepository boardRepository;

/*
게시글 생성
Expand Down Expand Up @@ -51,7 +51,7 @@ public BoardResponse create(BoardCreateRequest request) {
- DB에 있는 모든 게시글을 가져옴
- Entity 리스트를 Response DTO 리스트로 변환
*/
@Transactional(readOnly = true)
@Transactional(readOnly = true) // 조회 전용
public List<BoardResponse> findAll() {
return boardRepository.findAll()
.stream()
Expand Down