Skip to content

Commit

Permalink
[BE-pio] feat: 카드 수정 기능 구현
Browse files Browse the repository at this point in the history
- card 정보를 넘겨받아서 repository의 save메서드에서 id 유무를 판별 후
id가 존재하면 업데이트를 진행한다.

Related to #18
  • Loading branch information
NB993 committed Apr 10, 2022
1 parent 285fd02 commit c20b731
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,10 +40,18 @@ public ResponseEntity<LogDTO> 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);
}

}
20 changes: 20 additions & 0 deletions backend/src/main/java/com/team05/todolist/domain/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public Integer getOrder() {
return order;
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<String, Object> params = getSaveParams(card);
simpleJdbcInsert.executeAndReturnKey(params).intValue();
}
Expand Down Expand Up @@ -54,6 +64,23 @@ public List<Card> findAll() {

@Override
public Optional<Card> findById(int id) {
return Optional.empty();
List<Card> result = jdbcTemplate.query(
"SELECT id, order_index, delete_yn, title, content, section FROM card WHERE id = ?",
cardRowMapper(), id);
return result.stream().findAny();
}

private RowMapper<Card> 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;
};
}
}
46 changes: 19 additions & 27 deletions backend/src/main/java/com/team05/todolist/service/CardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,34 +26,24 @@ public void save(CardDTO cardDto) {

}


public Card findOne(int id) throws NoSuchElementException {
Optional<Card> 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<Card> findCards() {
// return cardRepository.findAll();
// }

// public List<Log> findLogs() {
// return logRepository.findAll();
// }

// public ResponseCardDto findOne(int id) throws NoSuchElementException {
// Optional<Card> 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) {
//
// }
}

0 comments on commit c20b731

Please sign in to comment.