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

Step3 테스트를 통한 코드 보호 #87

Open
wants to merge 10 commits into
base: ku-lee
Choose a base branch
from
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- [ ] 테이블 그룹 생성 시 최소 2개 이상의 주문 테이블이 있어야 한다.
- [ ] 공석인 주문 테이블만 추가 가능하다.
- [ ] 테이블 그룹의 생성 일자는 최초 저장 시의 서버 시간을 기준으로 한다.
- [ ] 전체 테이블 그룹 리스트를 조회 할 수 있다.
- [ ] 테이블 그룹을 삭제 할 수 있다.

- 테이블
- [ ] 전체 테이블 리스트를 조회 할 수 있다.
Expand All @@ -48,7 +48,7 @@
- [ ] 테이블 게스트의 수를 수정 할 수 있다.
- [ ] 수정 가능한 게스트의 수는 0명 이상이다.
- [ ] 테이블이 공석인 경우, 수정 할 수 없다.

## 용어 사전

| 한글명 | 영문명 | 설명 |
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/calculator/StringCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

public class StringCalculator {


public StringCalculator() {
}

Expand All @@ -17,7 +16,7 @@ public boolean isEmptyInput(String input) {

public int add(String input) {
if (isEmptyInput(input)) {
return new CalcNumber().value;
return CalcNumber.DEFAULT_CALC_VALUE.getValue();
}

List<String> parsedStrings = StringSplitter.split(input);
Expand All @@ -26,8 +25,8 @@ public int add(String input) {
.stream()
.map(CalcNumber::new)
.reduce(CalcNumber::sum)
.orElseGet(CalcNumber::new);
.orElseGet(() -> CalcNumber.DEFAULT_CALC_VALUE);

return sum.getValue();

}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/calculator/model/CalcNumber.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package calculator.model;

import java.util.Objects;

public class CalcNumber {
public int value;
private static final int DEFAULT_CALC_VALUE = 0;

public CalcNumber(){
this.value = DEFAULT_CALC_VALUE;
}
public static final CalcNumber DEFAULT_CALC_VALUE = new CalcNumber(0);
public int value;

public CalcNumber(int value) {
this.value = value;
this.validate(value);
this.value = value;
}

public CalcNumber(String str) {
Expand All @@ -22,6 +21,7 @@ public int getValue() {
}

private void validate(int value) {
Objects.requireNonNull(value);
if (value < 0) {
throw new RuntimeException("Number must be positive");
}
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/kitchenpos/dao/DefaultMenuDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package kitchenpos.dao;

import kitchenpos.model.Menu;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@Repository
public class DefaultMenuDao implements MenuDao {
private static final String TABLE_NAME = "menu";
private static final String KEY_COLUMN_NAME = "id";

private final NamedParameterJdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert jdbcInsert;

public DefaultMenuDao(final DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
jdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName(TABLE_NAME)
.usingGeneratedKeyColumns(KEY_COLUMN_NAME)
;
}

@Override
public Menu save(final Menu entity) {
final SqlParameterSource parameters = new BeanPropertySqlParameterSource(entity);
final Number key = jdbcInsert.executeAndReturnKey(parameters);
return select(key.longValue());
}

@Override
public Optional<Menu> findById(final Long id) {
try {
return Optional.of(select(id));
} catch (final EmptyResultDataAccessException e) {
return Optional.empty();
}
}

@Override
public List<Menu> findAll() {
final String sql = "SELECT id, name, price, menu_group_id FROM menu ";
return jdbcTemplate.query(sql, (resultSet, rowNumber) -> toEntity(resultSet));
}

@Override
public long countByIdIn(final List<Long> ids) {
final String sql = "SELECT COUNT(*) FROM menu WHERE id IN (:ids)";
final SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("ids", ids);
return jdbcTemplate.queryForObject(sql, parameters, Long.class);
}

private Menu select(final Long id) {
final String sql = "SELECT id, name, price, menu_group_id FROM menu WHERE id = (:id)";
final SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("id", id);
return jdbcTemplate.queryForObject(sql, parameters, (resultSet, rowNumber) -> toEntity(resultSet));
}

private Menu toEntity(final ResultSet resultSet) throws SQLException {
final Menu entity = new Menu();
entity.setId(resultSet.getLong("id"));
entity.setName(resultSet.getString("name"));
entity.setPrice(resultSet.getBigDecimal("price"));
entity.setMenuGroupId(resultSet.getLong("menu_group_id"));
return entity;
}
}
77 changes: 77 additions & 0 deletions src/main/java/kitchenpos/dao/DefaultMenuGroupDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package kitchenpos.dao;

import kitchenpos.model.MenuGroup;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@Repository
public class DefaultMenuGroupDao implements MenuGroupDao {
private static final String TABLE_NAME = "menu_group";
private static final String KEY_COLUMN_NAME = "id";

private final NamedParameterJdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert jdbcInsert;

public DefaultMenuGroupDao(final DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
jdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName(TABLE_NAME)
.usingGeneratedKeyColumns(KEY_COLUMN_NAME)
;
}

@Override
public MenuGroup save(final MenuGroup entity) {
final SqlParameterSource parameters = new BeanPropertySqlParameterSource(entity);
final Number key = jdbcInsert.executeAndReturnKey(parameters);
return select(key.longValue());
}

@Override
public Optional<MenuGroup> findById(final Long id) {
try {
return Optional.of(select(id));
} catch (final EmptyResultDataAccessException e) {
return Optional.empty();
}
}

@Override
public List<MenuGroup> findAll() {
final String sql = "SELECT id, name FROM menu_group";
return jdbcTemplate.query(sql, (resultSet, rowNumber) -> toEntity(resultSet));
}

@Override
public boolean existsById(final Long id) {
final String sql = "SELECT CASE WHEN COUNT(*) > 0 THEN TRUE ELSE FALSE END FROM menu_group WHERE id = (:id)";
final SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("id", id);
return jdbcTemplate.queryForObject(sql, parameters, Boolean.class);
}

private MenuGroup select(final Long id) {
final String sql = "SELECT id, name FROM menu_group WHERE id = (:id)";
final SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("id", id);
return jdbcTemplate.queryForObject(sql, parameters, (resultSet, rowNumber) -> toEntity(resultSet));
}

private MenuGroup toEntity(final ResultSet resultSet) throws SQLException {
final MenuGroup entity = new MenuGroup();
entity.setId(resultSet.getLong("id"));
entity.setName(resultSet.getString("name"));
return entity;
}
}
79 changes: 79 additions & 0 deletions src/main/java/kitchenpos/dao/DefaultMenuProductDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package kitchenpos.dao;

import kitchenpos.model.MenuProduct;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

@Repository
public class DefaultMenuProductDao implements MenuProductDao {
private static final String TABLE_NAME = "menu_product";
private static final String KEY_COLUMN_NAME = "seq";

private final NamedParameterJdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert jdbcInsert;

public DefaultMenuProductDao(final DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
jdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName(TABLE_NAME)
.usingGeneratedKeyColumns(KEY_COLUMN_NAME)
;
}

@Override
public MenuProduct save(final MenuProduct entity) {
final SqlParameterSource parameters = new BeanPropertySqlParameterSource(entity);
final Number key = jdbcInsert.executeAndReturnKey(parameters);
return select(key.longValue());
}

@Override
public Optional<MenuProduct> findById(final Long id) {
try {
return Optional.of(select(id));
} catch (final EmptyResultDataAccessException e) {
return Optional.empty();
}
}

@Override
public List<MenuProduct> findAll() {
final String sql = "SELECT seq, menu_id, product_id, quantity FROM menu_product";
return jdbcTemplate.query(sql, (resultSet, rowNumber) -> toEntity(resultSet));
}

@Override
public List<MenuProduct> findAllByMenuId(final Long menuId) {
final String sql = "SELECT seq, menu_id, product_id, quantity FROM menu_product WHERE menu_id = (:menuId)";
final SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("menuId", menuId);
return jdbcTemplate.query(sql, parameters, (resultSet, rowNumber) -> toEntity(resultSet));
}

private MenuProduct select(final Long id) {
final String sql = "SELECT seq, menu_id, product_id, quantity FROM menu_product WHERE seq = (:seq)";
final SqlParameterSource parameters = new MapSqlParameterSource()
.addValue("seq", id);
return jdbcTemplate.queryForObject(sql, parameters, (resultSet, rowNumber) -> toEntity(resultSet));
}

private MenuProduct toEntity(final ResultSet resultSet) throws SQLException {
final MenuProduct entity = new MenuProduct();
entity.setSeq(resultSet.getLong(KEY_COLUMN_NAME));
entity.setMenuId(resultSet.getLong("menu_id"));
entity.setProductId(resultSet.getLong("product_id"));
entity.setQuantity(resultSet.getLong("quantity"));
return entity;
}
}
Loading