Skip to content

Commit

Permalink
[BE-nathan] feat: todoList 화면 출력 기능 구현
Browse files Browse the repository at this point in the history
- 아직 페이징 기능 미구현
- Section별 카드 정보, 전체 Log 정보를 응답 값으로 반환

Related to #16
  • Loading branch information
nathan29849 committed Apr 11, 2022
1 parent cb487b1 commit d8e3d65
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +35,14 @@ public CardController(CardService cardService, LogService logService) {
this.logService = logService;
}

@ApiOperation("전체 카드 조회")
@GetMapping("/cards")
public ResponseEntity<ListResponseDTO> inquireAll() {
ClassifiedCardsDTO classifiedCards = cardService.findCards();
List<LogDTO> logs = logService.findLogs();
return ResponseEntity.ok().body(new ListResponseDTO(classifiedCards, logs));
}

@ApiOperation("카드 등록")
@PostMapping("/cards")
public ResponseEntity<ResponseDTO> create(CardDTO cardDto) {
Expand Down
8 changes: 6 additions & 2 deletions backend/src/main/java/com/team05/todolist/domain/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +23,10 @@ public void setId(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public String getEventType() {
return event.getEventType();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<CardDTO>> classifiedCards;

public ClassifiedCardsDTO() {
this.classifiedCards = new ConcurrentHashMap<>();
classifiedCards.put("todo", new ArrayList<>());
classifiedCards.put("doing", new ArrayList<>());
classifiedCards.put("done", new ArrayList<>());
}

public List<CardDTO> get(String section) {
return classifiedCards.get(section);
}
}
Original file line number Diff line number Diff line change
@@ -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<LogDTO> logs;

public ListResponseDTO(ClassifiedCardsDTO classifiedCardsDTO, List<LogDTO> logs) {
this.classifiedCardsDTO = classifiedCardsDTO;
this.logs = logs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,7 +60,9 @@ public void delete(int id) {

@Override
public List<Card> findAll() {
return null;
return jdbcTemplate.query(
"SELECT id, order_index, delete_yn, title, content, section FROM card where delete_yn = ?",
cardRowMapper(), NON_DELETED);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -38,8 +42,26 @@ private Map<String, Object> getSaveParams(Log log) {
return params;
}

@Override
public List<Log> findAll() {
return null;
return jdbcTemplate.query("SELECT id, event, title, log_time, prev_section, section FROM log",
logRowMapper());
}

private RowMapper<Log> 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;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
public interface LogRepository {

int save(Log log);
List<Log> findAll();

List<Log> findAll();
}
27 changes: 27 additions & 0 deletions backend/src/main/java/com/team05/todolist/service/CardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,4 +51,29 @@ public void update(int id, CardDTO cardDto) {
public void delete(int id) {
cardRepository.delete(id);
}

public ClassifiedCardsDTO findCards() {
List<Card> cards = cardRepository.findAll();
return classifyBySection(cards);
}

private ClassifiedCardsDTO classifyBySection(List<Card> cards) {
ClassifiedCardsDTO classifiedCards = new ClassifiedCardsDTO();
List<CardDTO> 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<CardDTO> cards) {
return cards.size() < 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<LogDTO> findLogs() {
List<Log> logs = logRepository.findAll();
List<LogDTO> 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;
}
}
22 changes: 22 additions & 0 deletions backend/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -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
);

0 comments on commit d8e3d65

Please sign in to comment.