Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[team-01][BE] API - 전체 Todo 조회 요청 / 전체 History 조회 요청 #127

Merged
merged 22 commits into from Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
53fa7d7
fix: PR 리뷰 반영
donggi-lee-bit Apr 11, 2022
b95c219
feat: 전체 todo 를 조회하는 기능 구현
donggi-lee-bit Apr 11, 2022
458eaeb
feat: History 테이블, dummy 데이터 생성 쿼리 작성, H2 DB In-Memory mode 변경
ku-kim Apr 11, 2022
25e1a6a
feat: 전체 Histories를 조회하는 기능 구현
ku-kim Apr 11, 2022
ed6fbcd
refactor: List 에 데이터를 추가하는 로직 메서드 분리
donggi-lee-bit Apr 11, 2022
5c942f4
feat: GET /api/histories 응답 JSON 구조 요청 변경 (user 추가)
ku-kim Apr 12, 2022
bac1fe7
refactor: Service 계층에서 Optional 처리하도록 변경
donggi-lee-bit Apr 12, 2022
bcc4df4
refactor: findAll() 메서드 이름을 findAllTodos() 로 변경
donggi-lee-bit Apr 12, 2022
38884fa
refactor: 테스트 코드 리팩토링
donggi-lee-bit Apr 12, 2022
ca6114e
Merge pull request #37 from ku-kim/feature/BE/GET_api_todos_list
ku-kim Apr 12, 2022
b720734
Merge pull request #40 from ku-kim/feature/BE/GET_api_histories
donggi-lee-bit Apr 12, 2022
be0f505
refactor: TODO, HISTORY 테이블 컨밴션 스네이크 케이스와 더미 데이터 로직 단위 통일
ku-kim Apr 12, 2022
444ebd7
refactor: Todo, History 컨트롤러의 불필요한 autowired 제거, @RequestMapping("api…
ku-kim Apr 12, 2022
ae4b909
refactor: Todo, History 도메인 객체에 Lombok 적용
ku-kim Apr 12, 2022
a76acac
test: 더미데이터 변경하여 통과하지 못한 테스트 코드 수정
ku-kim Apr 12, 2022
46d734f
feat: test 환경의 별도의 application.yml .sql 생성
ku-kim Apr 12, 2022
972c709
refactor: findAll() 메서드 반환 타입 Optional 제거
donggi-lee-bit Apr 12, 2022
7f29871
refactor: 컨트롤러의 @RequestMapping("/api")들을 WebConfig 에서 관리하도록 수정
ku-kim Apr 12, 2022
fa44b3c
refactor: controller 테스트 변경
donggi-lee-bit Apr 12, 2022
cbc49b7
refactor: 중복 코드 제거 및 Exception 메시지 추가
donggi-lee-bit Apr 12, 2022
93179f3
style: 구글 스타일 포매팅
donggi-lee-bit Apr 12, 2022
5d99b38
Merge remote-tracking branch 'origin/develop-BE' into develop-BE
donggi-lee-bit Apr 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion BE/build.gradle
Expand Up @@ -17,13 +17,15 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'

// lombok
implementation 'org.projectlombok:lombok'

// h2
runtimeOnly 'com.h2database:h2'

// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured'

}

tasks.named('test') {
Expand Down
19 changes: 19 additions & 0 deletions BE/src/main/java/codesquad/be/todoserver/config/WebConfig.java
@@ -0,0 +1,19 @@
package codesquad.be.todoserver.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.HandlerTypePredicate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer
.addPathPrefix("/api",
HandlerTypePredicate.forBasePackage("codesquad.be.todoserver.controller"));
}
}
@@ -0,0 +1,22 @@
package codesquad.be.todoserver.controller;

import codesquad.be.todoserver.domain.History;
import codesquad.be.todoserver.service.HistoryService;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HistoryController {

private final HistoryService historiesService;

public HistoryController(HistoryService historiesService) {
this.historiesService = historiesService;
}

@GetMapping("/histories")
public List<History> getAllHistory() {
return historiesService.getAllHistory();
}
}
Expand Up @@ -2,18 +2,15 @@

import codesquad.be.todoserver.domain.Todo;
import codesquad.be.todoserver.service.TodoService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api")
public class TodoController {

@Autowired
private TodoService todoService;
private final TodoService todoService;

public TodoController(TodoService todoService) {
this.todoService = todoService;
Expand All @@ -23,4 +20,9 @@ public TodoController(TodoService todoService) {
public Todo getById(@PathVariable Long id) {
return todoService.getById(id);
}

@GetMapping("/todos")
public List<Todo> todoList() {
return todoService.findTodos();
}
}
29 changes: 29 additions & 0 deletions BE/src/main/java/codesquad/be/todoserver/domain/History.java
@@ -0,0 +1,29 @@
package codesquad.be.todoserver.domain;

import lombok.Getter;

@Getter
public class History {

private final Long id;
private final Long todoId;
private final String todoTitle;
private final String user;
private final String action;
private final String fromStatus;
private final String toStatus;
Hyune-c marked this conversation as resolved.
Show resolved Hide resolved
private final String createdAt;

public History(Long id, Long todoId, String todoTitle, String user, String action,
String fromStatus, String toStatus, String createdAt) {
this.id = id;
this.todoId = todoId;
this.todoTitle = todoTitle;
this.user = user;
this.action = action;
this.fromStatus = fromStatus;
this.toStatus = toStatus;
this.createdAt = createdAt;
}

}
43 changes: 8 additions & 35 deletions BE/src/main/java/codesquad/be/todoserver/domain/Todo.java
@@ -1,22 +1,26 @@
package codesquad.be.todoserver.domain;

import java.time.LocalDateTime;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class Todo {

private Long id;
private final String title;
private final String contents;
private final String user;
private final String status;
private LocalDateTime createdTime;
private LocalDateTime updatedTime;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;

public Todo(Long id, String title, String contents, String userId, String status) {
public Todo(Long id, String title, String contents, String user, String status) {
this.id = id;
this.title = title;
this.contents = contents;
this.user = userId;
this.user = user;
this.status = status;
}

Expand All @@ -27,35 +31,4 @@ public Todo(String title, String contents, String user, String status) {
this.status = status;
}

public Long getId() {
return id;
}

public String getTitle() {
return title;
}

public String getContents() {
return contents;
}

public String getUser() {
return user;
}

public String getStatus() {
return status;
}

public void setId(Long id) {
this.id = id;
}

public void setCreatedTime(LocalDateTime createdTime) {
this.createdTime = createdTime;
}

public void setUpdatedTime(LocalDateTime updatedTime) {
this.updatedTime = updatedTime;
}
}
@@ -0,0 +1,41 @@
package codesquad.be.todoserver.repository;

import codesquad.be.todoserver.domain.History;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class HistoryJdbcRepository implements HistoryRepository {

private final JdbcTemplate jdbcTemplate;

@Autowired
public HistoryJdbcRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}


@Override
public List<History> findAllHistory() {
String sql = "SELECT id, todo_id, todo_title, user, action, from_status, to_status, created_at FROM HISTORY";

return jdbcTemplate.query(sql, historyRowMapper());
}

private RowMapper<History> historyRowMapper() {
return (rs, rowNum) -> new History(
rs.getLong("id"),
rs.getLong("todo_id"),
rs.getString("todo_title"),
rs.getString("user"),
rs.getString("action"),
rs.getString("from_status"),
rs.getString("to_status"),
rs.getString("created_at")
);
}
}
@@ -0,0 +1,11 @@
package codesquad.be.todoserver.repository;

import codesquad.be.todoserver.domain.History;

import java.util.List;

public interface HistoryRepository {
Hyune-c marked this conversation as resolved.
Show resolved Hide resolved

List<History> findAllHistory();

}
@@ -1,32 +1,37 @@
package codesquad.be.todoserver.repository;

import codesquad.be.todoserver.domain.Todo;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public class TodoJdbcRepository implements TodoRepository {

@Autowired
private JdbcTemplate jdbcTemplate;
private final JdbcTemplate jdbcTemplate;

public TodoJdbcRepository(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public Optional<Todo> findById(Long id) {
String sql = "SELECT id, title, contents, user, status, created_time, updated_time FROM TODO WHERE id = ?";
String sql = "SELECT id, title, contents, user, status, created_at, updated_at FROM TODO WHERE id = ?";
List<Todo> todos = jdbcTemplate.query(sql, todoRowMapper(), id);

return todos.stream().findAny();
}

@Override
public List<Todo> findAllTodos() {
String sql = "SELECT id, title, contents, user, status, created_at, updated_at FROM TODO";
List<Todo> todos = jdbcTemplate.query(sql, todoRowMapper());
return todos;
}

public RowMapper<Todo> todoRowMapper() {
return (rs, rowNum) -> {
Todo todo = new Todo(
Expand All @@ -35,8 +40,8 @@ public RowMapper<Todo> todoRowMapper() {
rs.getString("user"),
rs.getString("status"));
todo.setId(rs.getLong("id"));
todo.setCreatedTime(rs.getObject("created_time", LocalDateTime.class));
todo.setUpdatedTime(rs.getObject("updated_time", LocalDateTime.class));
todo.setCreatedAt(rs.getTimestamp("created_at").toLocalDateTime());
todo.setUpdatedAt(rs.getTimestamp("updated_at").toLocalDateTime());
return todo;
};
}
Expand Down
@@ -1,11 +1,15 @@
package codesquad.be.todoserver.repository;

import codesquad.be.todoserver.domain.Todo;
import java.util.Optional;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface TodoRepository {

Optional<Todo> findById(Long id);

List<Todo> findAllTodos();
}
@@ -0,0 +1,28 @@
package codesquad.be.todoserver.service;

import codesquad.be.todoserver.domain.History;
import codesquad.be.todoserver.repository.HistoryRepository;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HistoryService {

private final HistoryRepository historyRepository;

@Autowired
public HistoryService(HistoryRepository historyRepository) {
this.historyRepository = historyRepository;
}

public List<History> getAllHistory() {
List<History> histories = historyRepository.findAllHistory();

if (histories.isEmpty()) {
throw new NoSuchElementException("Empty History");
}
return histories;
}
}
15 changes: 12 additions & 3 deletions BE/src/main/java/codesquad/be/todoserver/service/TodoService.java
Expand Up @@ -3,13 +3,13 @@
import codesquad.be.todoserver.domain.Todo;
import codesquad.be.todoserver.exception.NoSuchTodoFoundException;
import codesquad.be.todoserver.repository.TodoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.stereotype.Service;

@Service
public class TodoService {

@Autowired
private TodoRepository todoRepository;

public TodoService(TodoRepository todoRepository) {
Expand All @@ -18,6 +18,15 @@ public TodoService(TodoRepository todoRepository) {

public Todo getById(Long id) {
return todoRepository.findById(id)
.orElseThrow(() -> new NoSuchTodoFoundException("조회할 수 없는 Todo 입니다. id : " + id));
.orElseThrow(() -> new NoSuchTodoFoundException("id: " + id));
}

public List<Todo> findTodos() {
List<Todo> todos = todoRepository.findAllTodos();

if (todos.isEmpty()) {
throw new NoSuchElementException("Empty Todos");
}
return todos;
}
}
2 changes: 1 addition & 1 deletion BE/src/main/resources/application.yml
@@ -1,7 +1,7 @@
spring:
datasource:
driverClassName: org.h2.Driver
url: jdbc:h2:~/todo-server
url: jdbc:h2:mem:testTodo
username: sa
sql:
init:
Expand Down