[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (11)
2021. 4. 12. 16:03ㆍSpring/Practice
1. 문제
- REST API 형식으로 구현
- HTTP METHOD는 GET
- 요청 주소는 "/api/notice/1" (1은 공지사항의 글ID로 동적으로 변함)
- 데이터베이스에 프로그램 실행시 H2 데이터베이스에 INSERT 되어있음
- 조회된 결과가 있는 경우 Entity 리턴, 없는 경우 null
2. 풀이
- data.sql
INSERT INTO NOTICE(ID, CONTENTS, HITS, LIKES, REG_DATE, TITLE) VALUES(1, '내용1', 0, 0, '2021-01-01 01:01:01.000000', '제목1');
INSERT INTO NOTICE(ID, CONTENTS, HITS, LIKES, REG_DATE, TITLE) VALUES(2, '내용2', 0, 0, '2021-01-02 02:02:02.000000', '제목2');
INSERT INTO NOTICE(ID, CONTENTS, HITS, LIKES, REG_DATE, TITLE) VALUES(3, '내용3', 0, 0, '2021-01-03 03:03:03.000000', '제목3');
- schema.sql
DROP TABLE IF EXISTS NOTICE;
create table NOTICE (
ID BIGINT auto_increment primary key,
TITLE VARCHAR(255),
CONTENTS VARCHAR(255),
HITS INTEGER,
LIKES INTEGER,
REG_DATE TIMESTAMP
)
- application.yml
자동으로 데이터베이스에 데이터가 저장이 되도록 설정
spring:
h2:
console:
enabled: true
path: /h2-console
datasource:
url: jdbc:h2:mem:backofficeDb
driver-class-name: org.h2.Driver
username: root
password: '1111'
jpa:
hibernate:
ddl-auto: none # 데이터 삽입을 위한 설
generate-ddl: false # 데이터 삽입을 위한 설정
properties:
format_sql: true
hibernate:
show-sql: true
mvc:
hiddenmethod:
filter:
enabled: true
mustache:
suffix: .html
logging:
level:
org.hibernate.SQL: trace
org.hibernate.type: trace
- ApiController.java
package com.example.jpa.sample.notice.controller;
import com.example.jpa.sample.notice.entity.Notice;
import com.example.jpa.sample.notice.model.NoticeInput;
import com.example.jpa.sample.notice.repository.NoticeRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@RequiredArgsConstructor
@RestController
public class ApiNoticeController {
private final NoticeRepository noticeRepository;
/*
// 문제 6
@GetMapping("/api/notice")
public String noticeString() {
return "공지사항입니다.";
}
*/
/*
// 문제 7
@GetMapping("/api/notice")
public NoticeModel notice() {
LocalDateTime regDate = LocalDateTime.of(2021, 1, 1, 0, 0);
NoticeModel notice = new NoticeModel();
notice.setId(1);;
notice.setTitle("공지사항입니다.");
notice.setContents("공지사항 내용입니다.");
notice.setRegDate(regDate);
return notice;
}
*/
/*
// 문제 8
@GetMapping("/api/notice")
public List<NoticeModel> notice() {
List<NoticeModel> noticeList = new ArrayList<>();
noticeList.add(NoticeModel.builder()
.id(1)
.title("공지사항입니다.")
.contents("공지사항 내용입니다.")
.regDate(LocalDateTime.of(2021, 1, 1, 0, 0))
.build()
);
noticeList.add(NoticeModel.builder()
.id(2)
.title("두번째 공지사항입니다.")
.contents("두번째 공지사항 내용입니다.")
.regDate(LocalDateTime.of(2021, 1, 2, 0, 0))
.build()
);
return noticeList;
}
*/
// 문제 9
@GetMapping("/api/notice")
public List<NoticeInput> notice() {
List<NoticeInput> noticeList = new ArrayList<>();
// return null;
return noticeList;
}
// 문제 10
@GetMapping("/api/notice/count")
public int noticeCount() {
// return "10";
return 10;
}
/*
// 문제 11
@PostMapping("/api/notice")
public NoticeModel addNotice(@RequestParam String title, @RequestParam String contents) {
NoticeModel notice = NoticeModel.builder()
.id(1)
.title(title)
.contents(contents)
.regDate(LocalDateTime.now())
.build();
return notice;
}
*/
/*
// 문제 12
@PostMapping("/api/notice")
public NoticeModel addNotice(NoticeModel noticeModel) {
noticeModel.setId(2);
noticeModel.setRegDate(LocalDateTime.now());
return noticeModel;
}
*/
/*
// 문제 13
@PostMapping("/api/notice")
public NoticeModel addNotice(@RequestBody NoticeModel noticeModel) {
noticeModel.setId(3);
noticeModel.setRegDate(LocalDateTime.now());
return noticeModel;
}
*/
/*
// 문제 14
@PostMapping("/api/notice")
public Notice addNotice(@RequestBody NoticeInput noticeInput) {
Notice notice = Notice.builder()
.title(noticeInput.getTitle())
.contents(noticeInput.getContents())
.regDate(LocalDateTime.now())
.build();
return noticeRepository.save(notice);
}
*/
// 문제 15
@PostMapping("/api/notice")
public Notice addNotice(@RequestBody NoticeInput noticeInput) {
Notice notice = Notice.builder()
.title(noticeInput.getTitle())
.contents(noticeInput.getContents())
.regDate(LocalDateTime.now())
.hits(0)
.likes(0)
.build();
return noticeRepository.save(notice);
}
// 문제 16
@GetMapping("/api/notice/{id}")
public Notice getNotice(@PathVariable long id) {
return noticeRepository.findById(id).orElseThrow(null);
}
}
728x90
'Spring > Practice' 카테고리의 다른 글
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (13) (0) | 2021.04.12 |
---|---|
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (12) (0) | 2021.04.12 |
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (10) (0) | 2021.04.12 |
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (9) (0) | 2021.04.12 |