Skip to content

Commit

Permalink
[BE-nathan] refactor: ResponseDTO를 사용하여 카드 생성시 카드 정보 전달
Browse files Browse the repository at this point in the history
- 카드 생성 후 저장시, card ID를 반환하여 CardDTO 생성
  • Loading branch information
nathan29849 committed Apr 10, 2022
1 parent c20b731 commit cb487b1
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.team05.todolist.domain.Event;
import com.team05.todolist.domain.dto.CardDTO;
import com.team05.todolist.domain.dto.LogDTO;
import com.team05.todolist.domain.dto.ResponseDTO;
import com.team05.todolist.service.CardService;
import com.team05.todolist.service.LogService;
import io.swagger.annotations.ApiOperation;
Expand Down Expand Up @@ -32,12 +33,12 @@ public CardController(CardService cardService, LogService logService) {

@ApiOperation("카드 등록")
@PostMapping("/cards")
public ResponseEntity<LogDTO> create(CardDTO cardDto) {
cardService.save(cardDto);
public ResponseEntity<ResponseDTO> create(CardDTO cardDto) {
CardDTO newCardDto = cardService.save(cardDto);
LogDTO log = logService.save(Event.CREATE, cardDto.getTitle(), cardDto.getSection());

logger.debug("[card-title] {}, [log-information] {}({})", cardDto.getTitle(), log.getLogEventType(), log.getLogTime()); // card Id 추가
return ResponseEntity.ok().body(log);
logger.debug("[card-{}] {}, [log-information] {}-{}({})", newCardDto.getCardId(), newCardDto.getTitle(), log.getSection(), log.getLogEventType(), log.getLogTime());
return ResponseEntity.ok().body(new ResponseDTO(newCardDto, log));
}

@PutMapping("/cards/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.team05.todolist.domain.dto;

import lombok.Getter;

@Getter
public class ResponseDTO {

private CardDTO card;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface CardRepository {

void save(Card card);
int save(Card card);
void delete(int id);
List<Card> findAll();
Optional<Card> findById(int id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ public JdbcCardRepository(DataSource dataSource) {
}

@Override
public void save(Card card) {
public int 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;
return card.getId();
}

Map<String, Object> params = getSaveParams(card);
simpleJdbcInsert.executeAndReturnKey(params).intValue();
return simpleJdbcInsert.executeAndReturnKey(params).intValue();
}

private Map<String, Object> getSaveParams(Card card) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public CardService(CardRepository cardRepository) {
this.cardRepository = cardRepository;
}

public void save(CardDTO cardDto) {
public CardDTO save(CardDTO cardDto) {
Card card = new Card(cardDto.getOrder(), NON_DELETED, cardDto.getTitle(), cardDto.getContent(),
cardDto.getSection());

cardRepository.save(card);

int newCardId = cardRepository.save(card);
CardDTO newCardDto = new CardDTO(card.getOrder(), card.getTitle(), card.getContent(),
card.getSectionType());
newCardDto.setCardId(newCardId);
return newCardDto;
}


Expand Down

0 comments on commit cb487b1

Please sign in to comment.