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
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
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//JSON 형태로 응답을 반환하는 컨트롤러(@controller+@ResponseBody)
@RequestMapping("/boards")//URL의 기본 경로 설정
@RequiredArgsConstructor//final 필드 자동 생성자 생성(의존성 자동 주입)
public class BoardController {

private final ;
private final BoardService boardService;

/*
게시글 생성
Expand All @@ -32,11 +32,11 @@ public class BoardController {
-> 결과 반환
-> JSON 응답
*/
@Operation(
@Operation(//API 설명 추가(문서화)
summary = "게시글 생성",
description = "새로운 게시글을 생성합니다."
)
@
@PostMapping//POST 요청 처리(생성)
public ResponseEntity<BoardResponse> create(@RequestBody BoardCreateRequest request) {
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//조회 요청 처리
public ResponseEntity<List<BoardResponse>> findAll() {
List<BoardResponse> response = boardService.findAll();
return ResponseEntity.ok(response);
Expand All @@ -69,9 +69,9 @@ public ResponseEntity<BoardResponse> findById(@PathVariable Long id) {
summary = "게시글 수정",
description = "id로 특정 게시글의 제목과 내용을 수정합니다."
)
@("/{id}")
@PutMapping("/{id}")//수정 요청 처리(데이터 업데이트)
public ResponseEntity<BoardResponse> update(@PathVariable Long id,
@RequestBody BoardUpdateRequest request) {
@RequestBody BoardUpdateRequest request) {//JSON->Java객체 변환
BoardResponse response = boardService.update(id, request);
return ResponseEntity.ok(response);
}
Expand All @@ -81,8 +81,8 @@ public ResponseEntity<BoardResponse> update(@PathVariable Long id,
summary = "게시글 삭제",
description = "id로 특정 게시글을 삭제합니다."
)
@("/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
@DeleteMapping("/{id}")//삭제 요청 처리
public ResponseEntity<Void> delete(@PathVariable Long id) {//URL값 가져오기
boardService.delete(id);
return ResponseEntity.noContent().build();
}
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/likelion/session/domain/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@

@Getter // getter 메서드 자동 생성
@Entity // 해당 클래스 DB 테이블로 인식하고 관리
@Table(name = "boards")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Table(name = "boards")//테이블 이름 지정
@NoArgsConstructor(access = AccessLevel.PROTECTED)//파라미터 없는 기본 생성자 자동 생성
@AllArgsConstructor//모든 필드를 파라미터로 받는 생성자 생성
public class Board {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long ;
@Id//기본키(데이터 하나를 구분하는 기준)
@GeneratedValue(strategy = GenerationType.IDENTITY)//ID 자동 생성
private Long id;

// 게시글 제목
@Column(nullable = false, length = 100)
private String ;
@Column(nullable = false, length = 100)//DB 컬럼 제약조건 설정
private String title;

// 게시글 내용
@Column(nullable = false, columnDefinition = "TEXT")
private String ;
private String content;

// 작성자
@Column(nullable = false, length = 30)
private String ;
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 @@ -46,13 +46,13 @@ public Board(String title, String content, String writer) {
this.writer = writer;
}

@PrePersist
@PrePersist//DB 저장 직전에 실행(save()처음할 때)
public void prePersist() {
this.createdAt = LocalDateTime.now();
this.updatedAt = LocalDateTime.now();
}

@PreUpdate
@PreUpdate//DB 수정 직전에 실행(update(save 재호출 시))
public void preUpdate() {
this.updatedAt = LocalDateTime.now();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import lombok.Setter;

@Getter
@Setter
@Setter//객체의 값을 바꾸는 메서드
@NoArgsConstructor
@AllArgsConstructor
public class BoardCreateRequest {
// 넘겨주고 싶은 정보: 제목(title), 내용(content), 작성자(writer)
private String ;
private String ;
private String ;
private String title;
private String content;
private String writer;
}
3 changes: 1 addition & 2 deletions src/main/java/com/likelion/session/dto/BoardResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

@Getter
@AllArgsConstructor
@Builder
@Builder//객체를 안전하게 생성하게 해주는 방식
public class BoardResponse {

// 돌려주고 싶은 응답: id, title, content, writer, createdAt, updatedAt
private Long id;
private String title;
private String content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
@Setter
@NoArgsConstructor
public class BoardUpdateRequest {
@NotBlank(message = "제목은 필수입니다.")
@Size(max = 100, message = "제목은 100자 이하로 입력해주세요.")
@NotBlank(message = "제목은 필수입니다.")//값 반드시 있어햐함(공백도 금지)
@Size(max = 100, message = "제목은 100자 이하로 입력해주세요.")//문자 길이 제한
private String title;

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

import java.util.List;

@Slf4j
@Service
@Slf4j//로그 찍는 도구 자동 생성
@Service//비즈니스 로직 담당 클래스
@RequiredArgsConstructor
@Transactional
@Transactional//하나의 작업을 묶어서 처리(성공->커밋, 실패->롤백)
public class BoardService {

private final ;
private final BoardRepository boardRepository;

/*
게시글 생성
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
port: 8080
port: 8082

spring:
application:
Expand All @@ -9,7 +9,7 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/session?serverTimezone=Asia/Seoul&characterEncoding=UTF-8
username: root
password: yebin1448!
password: Chaewon04!

jpa:
hibernate:
Expand Down