-
Notifications
You must be signed in to change notification settings - Fork 1
4 Backend chanul #25
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
base: Backend
Are you sure you want to change the base?
4 Backend chanul #25
Conversation
| import java.util.List; | ||
|
|
||
| @Service | ||
| @AllArgsConstructor |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AllArgsConstructor๋ ๋ชจ๋ ํ๋ ๊ฐ์ ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ ์์ฑ์๋ฅผ ์์ฑ ์ด๋
ธํ
์ด์
์ด๋ผ ์๋น์ค ๋ก์ง์์ ์ฌ์ฉํ๊ธฐ์ ์ ํฉํ์ง ์์๋ณด์ฌ์
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public Long updateBoard(@PathVariable Long id, @RequestBody BoardRequestDto requestDto) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
board๋ฅผ ์์ ํ๋ ๋ฉ์๋ ์ธ๋ฐ ๋ฐํํ๋ ๊ฐ์ด Long์ธ ์ด์ ๊ฐ ์๋์?
| public class BoardController { | ||
| private final BoardService boardService; | ||
|
|
||
| public BoardController(BoardService boardService) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์ฑ์๋ฅผ ๋ง๋ค์ด์ BoardService๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋ @RequiredArgsConstructor๋ฅผ ํตํ ์์กด์ฑ ์ฃผ์
์ ํตํด boardService๋ฅผ ์ฌ์ฉํ๋๊ฒ ๋ ์ข์๊ฑฐ ๊ฐ์์
| @Entity(name = "boards") // DB ํ ์ด๋ธ ์ด๋ฆ์ "boards"๋ก ์ค์ | ||
| public class Board { | ||
| // ๊ธ ๊ณ ์ ์์ด๋ | ||
| @GeneratedValue(strategy = GenerationType.AUTO) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํ์ฌ ๊ธฐ๋ณธํค ์์ฑ ์ ๋ต์ GenerationType.AUTO๋ก ํด๋์ด ํ์ด๋ฒ๋ค์ดํธ๊ฐ ์๋์ผ๋ก ์ ๋ต์ ์ ํํ๊ฒ๋ ๋ฌ๋๋ฐ DB ๋ฒ์ ์ ๋ฐ๋ผ ์ ํ๋๋ ์ ๋ต์ด ๋ฌ๋ผ์ง ์ ์์ผ๋ฏ๋ก ์ฌ์ฉํ๋ DBMS์ ๋ง๋ ์ ๋ต์ ์ฌ์ฉํ์๋ฉด ์ข์๊ฑฐ ๊ฐ์์
| @Transactional | ||
| public Long deleteBoard(Long id) { | ||
| boardRepository.deleteById(id); | ||
| return id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ญ์ ํ๋ ๋ฉ์๋์์ ์ญ์ ํ board์ id๋ฅผ ๋ค์ ๋ฐํํ๋ ์ด์ ๊ฐ ์๋์?
|
|
||
| @Transactional | ||
| public Long deleteBoard(Long id) { | ||
| boardRepository.deleteById(id); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ฐ์์จ id๋ฅผ ๋ฐ๋ก ์ญ์ ํ๋๊ฒ ์๋๋ผ ์ด board์ id๊ฐ ์๋์ง ํ์ธํ๋ ๋ก์ง์ด ์์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์
| @@ -0,0 +1,5 @@ | |||
| spring.datasource.url=jdbc:mysql://localhost:3306/boardmysql?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ง์ ํ๋ก์ ํธ์์ application.properties ํ์ผ๋ณด๋ค application.ymlํ์ผ์ ๋ ๋ง์ด ์ฌ์ฉํด์
๊ทธ๋์ yml ํ์ผ ์ฌ์ฉํด๋ณด๋๊ฒ๋ ์ข์๊ฑฐ ๊ฐ์์
| @@ -0,0 +1,5 @@ | |||
| spring.datasource.url=jdbc:mysql://localhost:3306/boardmysql?serverTimezone=Asia/Seoul&useSSL=false&allowPublicKeyRetrieval=true | |||
| spring.datasource.username=root | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
username์ด๋ password ๊ฐ์ ๋ฏผ๊ฐํ ์ ๋ณด๋ ์ธ๋ถ์ ๋
ธ์ถ๋๋ฉด ์๋๊ธฐ ๋๋ฌธ์ ์ปค๋ฐ์ ํ ๋ ํ๊ฒฝ๋ณ์ ์ฒ๋ฆฌํด์ ์ฌ๋ ค์ฃผ์ธ์
| } | ||
|
|
||
| @PostMapping("/") | ||
| public BoardResponseDto createBoard(@RequestBody BoardRequestDto requestDto) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ปจํธ๋กค๋ฌ์์ ๋ฐํํ ๋ ResponseEntity ์ฌ์ฉํด์ ๋ฐํ๊ฐ๊ณผ ์ํ ์ฝ๋๋ฅผ ํจ๊ป ๋ฐํํ๋ฉด ์ข์๊ฑฐ ๊ฐ์์
ResponseEntity๋ ์ ์ ํ ์ํ ์ฝ๋์ ์๋ต ํค๋ ๋ฐ ์๋ต ๋ณธ๋ฌธ์ ์์ฑํ์ฌ ํด๋ผ์ด์ธํธ์ ์ ๋ฌํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค
| return boardService.findOneBoard(id); | ||
| } | ||
|
|
||
| @PutMapping("/{id}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@PutMapping๋ณด๋ค @PatchMapping์ด ๋์๋ณด์
๋๋ค.
๊ธฐ๋ณธ์ ์ธ http ๋ฉ์๋์ ๋ํด ์์๋ณด์๋๊ฑธ ์ถ์ฒ๋๋ ค์
|
|
||
| @Repository | ||
| public interface BoardRepository extends JpaRepository<Board, Long> { | ||
| List<BoardListResponseDto> findAllByOrderByModifiedAtDesc(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด ๋ฉ์๋ ์ฌ์ฉํ์ง ์๊ณ ์๋๋ฐ ๋ฃ์ผ์ ์ด์ ๊ฐ ์๋์?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ก์ปฌ์์ ์คํํ์ ๋ ModifiedAt์ด๋ผ๋ ํ๋๊ฐ ์์ง๋ง ์ฌ๊ธฐ์ ModifiedAt์ด๋ผ๋ ํ๋๋ฅผ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ๋ คํด์ ์ค๋ฅ๊ฐ ํฐ์ ธ ์คํ์ด ๋์ง ์์ต๋๋ค
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๊ธ ์์ฑ๊ณผ ๊ธ ์์ ํ ๋ ๊ฐ์ dto๋ฅผ ์ฌ์ฉํ๊ณ ์๋๋ฐ ๋ถ๋ฆฌํด์ฃผ์ธ์
ex) ๊ธ์์ฑ -> BoardWriteRequestDto ๊ธ์์ -> BoardUpdateRequestDto (์ด๊ฑด ์์์ ๋๋ค ์ฐธ๊ณ ํด์ ์์ ํด์ฃผ์ธ์)
๐ก ๊ฐ์
๐ ์์ ๋ด์ฉ
๐ ๋ณ๊ฒฝ์ฌํญ
๐โโ๏ธ ์ง๋ฌธ์ฌํญ
๐ ์ด๋ ค์ ๋ ์