Skip to content

Conversation

@chanul0817
Copy link

๐Ÿ’ก ๊ฐœ์š”

  • ๋งˆ์ธ๋“œ์›จ์ด ๊ณผ์ œ ์ˆ˜ํ–‰

๐Ÿ“ƒ ์ž‘์—…๋‚ด์šฉ

  • CRUD๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ Board๊ตฌํ˜„

๐Ÿ”€ ๋ณ€๊ฒฝ์‚ฌํ•ญ

๐Ÿ™‹โ€โ™‚๏ธ ์งˆ๋ฌธ์‚ฌํ•ญ

๐Ÿ’€ ์–ด๋ ค์› ๋˜ ์ 

import java.util.List;

@Service
@AllArgsConstructor
Copy link

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) {
Copy link

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) {
Copy link

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)
Copy link

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;
Copy link

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);
Copy link

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
Copy link

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
Copy link

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) {
Copy link

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}")
Copy link
Member

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();
Copy link
Member

Choose a reason for hiding this comment

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

์ด ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ์žˆ๋Š”๋ฐ ๋„ฃ์œผ์‹  ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”?

Copy link

Choose a reason for hiding this comment

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

๋กœ์ปฌ์—์„œ ์‹คํ–‰ํ–ˆ์„ ๋•Œ ModifiedAt์ด๋ผ๋Š” ํ•„๋“œ๊ฐ€ ์—†์ง€๋งŒ ์—ฌ๊ธฐ์„œ ModifiedAt์ด๋ผ๋Š” ํ•„๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๋ คํ•ด์„œ ์˜ค๋ฅ˜๊ฐ€ ํ„ฐ์ ธ ์‹คํ–‰์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค

Copy link

@ta2ye0n ta2ye0n left a 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 (์ด๊ฑด ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค ์ฐธ๊ณ ํ•ด์„œ ์ˆ˜์ •ํ•ด์ฃผ์„ธ์š”)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants