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 14e9e7aaf..e47a796c9 100644 --- a/backend/src/main/java/com/team05/todolist/controller/CardController.java +++ b/backend/src/main/java/com/team05/todolist/controller/CardController.java @@ -2,17 +2,21 @@ import com.team05.todolist.domain.Event; import com.team05.todolist.domain.dto.CardDTO; +import com.team05.todolist.domain.dto.ClassifiedCardsDTO; +import com.team05.todolist.domain.dto.ListResponseDTO; 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; +import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; @@ -31,6 +35,14 @@ public CardController(CardService cardService, LogService logService) { this.logService = logService; } + @ApiOperation("전체 카드 조회") + @GetMapping("/cards") + public ResponseEntity inquireAll() { + ClassifiedCardsDTO classifiedCards = cardService.findCards(); + List logs = logService.findLogs(); + return ResponseEntity.ok().body(new ListResponseDTO(classifiedCards, logs)); + } + @ApiOperation("카드 등록") @PostMapping("/cards") public ResponseEntity create(CardDTO cardDto) { diff --git a/backend/src/main/java/com/team05/todolist/domain/Log.java b/backend/src/main/java/com/team05/todolist/domain/Log.java index f61490854..0c3bc5a08 100644 --- a/backend/src/main/java/com/team05/todolist/domain/Log.java +++ b/backend/src/main/java/com/team05/todolist/domain/Log.java @@ -11,8 +11,8 @@ public class Log { private String prevSection; private Section section; - public Log(Event event, LocalDateTime logTime, String title, String prevSection, String section) { - this.event = event; + public Log(String event, LocalDateTime logTime, String title, String prevSection, String section) { + this.event = Event.getEvent(event); this.logTime = logTime; this.title = title; this.prevSection = prevSection; @@ -23,6 +23,10 @@ public void setId(Integer id) { this.id = id; } + public Integer getId() { + return id; + } + public String getEventType() { return event.getEventType(); } diff --git a/backend/src/main/java/com/team05/todolist/domain/dto/ClassifiedCardsDTO.java b/backend/src/main/java/com/team05/todolist/domain/dto/ClassifiedCardsDTO.java new file mode 100644 index 000000000..d1e929709 --- /dev/null +++ b/backend/src/main/java/com/team05/todolist/domain/dto/ClassifiedCardsDTO.java @@ -0,0 +1,24 @@ +package com.team05.todolist.domain.dto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import lombok.Getter; + +@Getter +public class ClassifiedCardsDTO { + + private final Map> classifiedCards; + + public ClassifiedCardsDTO() { + this.classifiedCards = new ConcurrentHashMap<>(); + classifiedCards.put("todo", new ArrayList<>()); + classifiedCards.put("doing", new ArrayList<>()); + classifiedCards.put("done", new ArrayList<>()); + } + + public List get(String section) { + return classifiedCards.get(section); + } +} diff --git a/backend/src/main/java/com/team05/todolist/domain/dto/ListResponseDTO.java b/backend/src/main/java/com/team05/todolist/domain/dto/ListResponseDTO.java new file mode 100644 index 000000000..a66bee249 --- /dev/null +++ b/backend/src/main/java/com/team05/todolist/domain/dto/ListResponseDTO.java @@ -0,0 +1,16 @@ +package com.team05.todolist.domain.dto; + +import java.util.List; +import lombok.Getter; + +@Getter +public class ListResponseDTO { + + private ClassifiedCardsDTO classifiedCardsDTO; + private List logs; + + public ListResponseDTO(ClassifiedCardsDTO classifiedCardsDTO, List logs) { + this.classifiedCardsDTO = classifiedCardsDTO; + this.logs = logs; + } +} 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 ec1c5dda0..f0ccb3835 100644 --- a/backend/src/main/java/com/team05/todolist/repository/JdbcCardRepository.java +++ b/backend/src/main/java/com/team05/todolist/repository/JdbcCardRepository.java @@ -15,6 +15,7 @@ @Repository public class JdbcCardRepository implements CardRepository { + private static final int NON_DELETED = 0; private static final int DELETED = 1; private JdbcTemplate jdbcTemplate; @@ -59,7 +60,9 @@ public void delete(int id) { @Override public List findAll() { - return null; + return jdbcTemplate.query( + "SELECT id, order_index, delete_yn, title, content, section FROM card where delete_yn = ?", + cardRowMapper(), NON_DELETED); } @Override diff --git a/backend/src/main/java/com/team05/todolist/repository/JdbcLogRepository.java b/backend/src/main/java/com/team05/todolist/repository/JdbcLogRepository.java index 16d350d57..fb0c19fa3 100644 --- a/backend/src/main/java/com/team05/todolist/repository/JdbcLogRepository.java +++ b/backend/src/main/java/com/team05/todolist/repository/JdbcLogRepository.java @@ -1,11 +1,15 @@ package com.team05.todolist.repository; import com.team05.todolist.domain.Log; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.HashMap; import java.util.List; import java.util.Map; 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; @@ -38,8 +42,26 @@ private Map getSaveParams(Log log) { return params; } - @Override public List findAll() { - return null; + return jdbcTemplate.query("SELECT id, event, title, log_time, prev_section, section FROM log", + logRowMapper()); + } + + private RowMapper logRowMapper() { + + return (rs, rowNum) -> { +// h2 database +// Timestamp timestamp = (Timestamp) rs.getObject("log_time"); +// LocalDateTime.ofInstant(timestamp.toInstant(), ZoneOffset.ofHours(0)), + Log log = new Log( + rs.getString("event"), + (LocalDateTime)rs.getObject("log_time"), + rs.getString("title"), + rs.getString("prev_section"), + rs.getString("section") + ); + log.setId(rs.getInt("id")); + return log; + }; } } diff --git a/backend/src/main/java/com/team05/todolist/repository/LogRepository.java b/backend/src/main/java/com/team05/todolist/repository/LogRepository.java index 4020fd4e8..d50f45f5a 100644 --- a/backend/src/main/java/com/team05/todolist/repository/LogRepository.java +++ b/backend/src/main/java/com/team05/todolist/repository/LogRepository.java @@ -6,6 +6,6 @@ public interface LogRepository { int save(Log log); - List findAll(); + List findAll(); } 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 e6c10b50e..722949074 100644 --- a/backend/src/main/java/com/team05/todolist/service/CardService.java +++ b/backend/src/main/java/com/team05/todolist/service/CardService.java @@ -2,7 +2,9 @@ import com.team05.todolist.domain.Card; import com.team05.todolist.domain.dto.CardDTO; +import com.team05.todolist.domain.dto.ClassifiedCardsDTO; import com.team05.todolist.repository.CardRepository; +import java.util.List; import java.util.NoSuchElementException; import java.util.Optional; import org.springframework.stereotype.Service; @@ -49,4 +51,29 @@ public void update(int id, CardDTO cardDto) { public void delete(int id) { cardRepository.delete(id); } + + public ClassifiedCardsDTO findCards() { + List cards = cardRepository.findAll(); + return classifyBySection(cards); + } + + private ClassifiedCardsDTO classifyBySection(List cards) { + ClassifiedCardsDTO classifiedCards = new ClassifiedCardsDTO(); + List sectionCards; + CardDTO cardDto; + for (Card card : cards) { + sectionCards = classifiedCards.get(card.getSectionType()); + if (check10Cards(sectionCards)) { + cardDto = new CardDTO(card.getOrder(), card.getTitle(), card.getContent(), + card.getSectionType()); + cardDto.setCardId(card.getId()); + sectionCards.add(cardDto); + } + } + return classifiedCards; + } + + private boolean check10Cards(List cards) { + return cards.size() < 10; + } } diff --git a/backend/src/main/java/com/team05/todolist/service/LogService.java b/backend/src/main/java/com/team05/todolist/service/LogService.java index c47cddd6a..72df30480 100644 --- a/backend/src/main/java/com/team05/todolist/service/LogService.java +++ b/backend/src/main/java/com/team05/todolist/service/LogService.java @@ -6,6 +6,8 @@ import com.team05.todolist.domain.dto.LogDTO; import com.team05.todolist.repository.LogRepository; import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; import org.springframework.stereotype.Service; @Service @@ -18,11 +20,20 @@ public LogService(LogRepository logRepository) { } public LogDTO save(Event event, String cardTitle, String section) { - Log log = new Log(event, LocalDateTime.now(), cardTitle, null, section); + Log log = new Log(event.getEventType(), LocalDateTime.now(), cardTitle, null, section); Integer logId = logRepository.save(log); return new LogDTO(logId, log.getEventType(), log.getLogTime(), log.getTitle(), log.getPrevSection(), log.getSectionType()); } + public List findLogs() { + List logs = logRepository.findAll(); + List logDtos = new ArrayList<>(); + for (Log log : logs) { // log를 10개씩 출력으로 변경해야 한다. + logDtos.add(new LogDTO(log.getId(), log.getEventType(), log.getLogTime(), + log.getTitle(), log.getPrevSection(), log.getSectionType())); + } + return logDtos; + } } diff --git a/backend/src/main/resources/schema.sql b/backend/src/main/resources/schema.sql new file mode 100644 index 000000000..5c0b6db6a --- /dev/null +++ b/backend/src/main/resources/schema.sql @@ -0,0 +1,22 @@ +drop table if exists card; +drop table if exists log; + +CREATE TABLE card ( + id int PRIMARY KEY AUTO_INCREMENT, + order_index int NOT NULL, + delete_yn tinyint(1) NOT NULL, + title varchar(255) NOT NULL, + content varchar(255) NOT NULL, + section enum('todo', 'doing', 'done') NOT NULL +); + + + +CREATE TABLE log ( + id int PRIMARY KEY AUTO_INCREMENT, + event enum('create', 'move', 'delete', 'update') NOT NULL, + title varchar(255) NOT NULL, + log_time dateTime NOT NULL, + prev_section varchar(255), + section varchar(255) NOT NULL +);