From c20b73193703f6c3535e90fa1335c833ab655001 Mon Sep 17 00:00:00 2001 From: Jaehong Choe Date: Mon, 11 Apr 2022 02:11:09 +0900 Subject: [PATCH] =?UTF-8?q?[BE-pio]=20feat:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - card 정보를 넘겨받아서 repository의 save메서드에서 id 유무를 판별 후 id가 존재하면 업데이트를 진행한다. Related to #18 --- .../todolist/controller/CardController.java | 9 ++++ .../java/com/team05/todolist/domain/Card.java | 20 ++++++++ .../repository/JdbcCardRepository.java | 29 +++++++++++- .../team05/todolist/service/CardService.java | 46 ++++++++----------- 4 files changed, 76 insertions(+), 28 deletions(-) diff --git a/backend/src/main/java/com/team05/todolist/controller/CardController.java b/backend/src/main/java/com/team05/todolist/controller/CardController.java index d810e0597..f4d19a489 100644 --- a/backend/src/main/java/com/team05/todolist/controller/CardController.java +++ b/backend/src/main/java/com/team05/todolist/controller/CardController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @@ -39,10 +40,18 @@ public ResponseEntity create(CardDTO cardDto) { return ResponseEntity.ok().body(log); } + @PutMapping("/cards/{id}") + public ResponseEntity update(@PathVariable int id, CardDTO cardDto) { + cardService.update(id, cardDto); + + return ResponseEntity.ok(HttpStatus.OK); + } + @DeleteMapping("/cards/{id}") public ResponseEntity delete(@PathVariable int id) { cardService.delete(id); return ResponseEntity.ok(HttpStatus.OK); } + } diff --git a/backend/src/main/java/com/team05/todolist/domain/Card.java b/backend/src/main/java/com/team05/todolist/domain/Card.java index e6413c7b6..34307dd4d 100644 --- a/backend/src/main/java/com/team05/todolist/domain/Card.java +++ b/backend/src/main/java/com/team05/todolist/domain/Card.java @@ -21,6 +21,10 @@ public void setId(Integer id) { this.id = id; } + public Integer getId() { + return id; + } + public Integer getOrder() { return order; } @@ -40,4 +44,20 @@ public String getContent() { public String getSectionType() { return section.getSectionType(); } + + public void changeTitle(String title) { + this.title = title; + } + + public void changeOrder(Integer order) { + this.order = order; + } + + public void changeContent(String content) { + this.content = content; + } + + public void changeSection(String section) { + this.section = Section.getSection(section); + } } diff --git a/backend/src/main/java/com/team05/todolist/repository/JdbcCardRepository.java b/backend/src/main/java/com/team05/todolist/repository/JdbcCardRepository.java index 480d2f827..045acc991 100644 --- a/backend/src/main/java/com/team05/todolist/repository/JdbcCardRepository.java +++ b/backend/src/main/java/com/team05/todolist/repository/JdbcCardRepository.java @@ -5,8 +5,10 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.simple.SimpleJdbcInsert; import org.springframework.stereotype.Repository; @@ -27,6 +29,14 @@ public JdbcCardRepository(DataSource dataSource) { @Override public void save(Card card) { + if (card.getId() != null) { + jdbcTemplate.update( + "UPDATE card SET order_index=?, title=?, content=?, section=? WHERE id=?", + card.getOrder(), card.getTitle(), card. getContent(), card.getSectionType(), card.getId()); + + return; + } + Map params = getSaveParams(card); simpleJdbcInsert.executeAndReturnKey(params).intValue(); } @@ -54,6 +64,23 @@ public List findAll() { @Override public Optional findById(int id) { - return Optional.empty(); + List result = jdbcTemplate.query( + "SELECT id, order_index, delete_yn, title, content, section FROM card WHERE id = ?", + cardRowMapper(), id); + return result.stream().findAny(); + } + + private RowMapper cardRowMapper() { + return (rs, rowNum) -> { + Card card = new Card( + rs.getInt("order_index"), + rs.getInt("delete_yn"), + rs.getString("title"), + rs.getString("content"), + rs.getString("section") + ); + card.setId(rs.getInt("id")); + return card; + }; } } diff --git a/backend/src/main/java/com/team05/todolist/service/CardService.java b/backend/src/main/java/com/team05/todolist/service/CardService.java index 7954b2709..fa31c9a61 100644 --- a/backend/src/main/java/com/team05/todolist/service/CardService.java +++ b/backend/src/main/java/com/team05/todolist/service/CardService.java @@ -3,6 +3,8 @@ import com.team05.todolist.domain.Card; import com.team05.todolist.domain.dto.CardDTO; import com.team05.todolist.repository.CardRepository; +import java.util.NoSuchElementException; +import java.util.Optional; import org.springframework.stereotype.Service; @Service @@ -24,34 +26,24 @@ public void save(CardDTO cardDto) { } + + public Card findOne(int id) throws NoSuchElementException { + Optional card = cardRepository.findById(id); + return card.orElseThrow(); + } + + public void update(int id, CardDTO cardDto) { + Card updateTargetCard = findOne(id); + + updateTargetCard.changeTitle(cardDto.getTitle()); + updateTargetCard.changeOrder(cardDto.getOrder()); + updateTargetCard.changeContent(cardDto.getContent()); + updateTargetCard.changeSection(cardDto.getSection()); + + cardRepository.save(updateTargetCard); + } + public void delete(int id) { cardRepository.delete(id); } - -// public List findCards() { -// return cardRepository.findAll(); -// } - -// public List findLogs() { -// return logRepository.findAll(); -// } - -// public ResponseCardDto findOne(int id) throws NoSuchElementException { -// Optional card = cardRepository.findById(id); -// return card.orElseThrow(); -// } -// -// public void update(RequestCardDto updateCardDto) { -// Card updateTargetCard = findOne(updateCardDto.getId()); -// updateTargetCard.changeSection(updateCardDto.getSection()); -// updateTargetCard.changeTitle(updateCardDto.getTitle()); -// updateTargetCard.changeContent(updateCardDto.getContent()); -// updateTargetCard.changeOrderIndex(updateCardDto.getOrderIndex()); -// -// cardRepository.save(updateTargetCard); -// } -// -// public void delete(RequestCardDto requestCardDto) { -// -// } }