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

[2단계 - 경로 조회 기능] 제이미(임정수) 미션 제출합니다. #195

Merged
merged 65 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
a84733b
refactor: 패키지 분리 및 이동
JJ503 May 17, 2023
bcf9345
refactor: SectionStationMapper 엔티티 생성
JJ503 May 17, 2023
b5fb095
refactor: Sections sort 메서드 분리
JJ503 May 17, 2023
1604c0f
refactor: Domain, Entity 동등성 재정의 및 toString 재정의
JJ503 May 17, 2023
2bce4a2
fix: 오류 발생 테스트 문제 해결
JJ503 May 18, 2023
fae6028
refactor: schema section 필드 추가
JJ503 May 18, 2023
9c5b287
test: SectionDao 단위 테스트 추가
JJ503 May 18, 2023
9ce5ea5
refactor: LineDao NamedParameterJdbcTemplate로 변경
JJ503 May 18, 2023
50a54d7
refactor: SectionDao SimpleJdbcInsert 수정
JJ503 May 18, 2023
1491e9e
test: LineDao 단위 테스트 추가
JJ503 May 18, 2023
b803d60
refactor: Station을 도메인과 엔티티로 분리
JJ503 May 18, 2023
e991338
test: LineDao 테스트 조건 수정
JJ503 May 18, 2023
deed89c
refactor: exception 네이밍 변경
JJ503 May 18, 2023
94ab852
test: StationDao 단위 테스트 추가
JJ503 May 18, 2023
21cfb58
refactor: Section 필드에 id 추가
JJ503 May 19, 2023
69f38f6
refactor: 역 추가에 대한 로직을 서비스에서 Sections로 이동
JJ503 May 19, 2023
d07b5b3
test: 추가적인 SectionDao 단위 테스트 추가
JJ503 May 19, 2023
4ed9e53
refactor: 역 제거 대한 로직을 서비스에서 Sections로 이동
JJ503 May 19, 2023
aa7eaab
refactor: 기타 컨벤션 적용
JJ503 May 19, 2023
8629a4f
test: 삭제 인수 테스트 추가
JJ503 May 19, 2023
ae241ae
docs: 경로 조회 api 추가
JJ503 May 21, 2023
24fd5cc
refactor: 프로덕션과 테스트 데이터베이스 분리
JJ503 May 21, 2023
7716d5d
feat: 최단 경로 조회 기능 추가
JJ503 May 21, 2023
2f5fa83
feat: 데이터 초기값 파일 설정 추가
JJ503 May 21, 2023
4e4ee12
feat: 경로와 거리를 담는 path 생성
JJ503 May 21, 2023
fdc0c60
docs: 2단계 기능 목록 추가 및 완료 사항 체크
JJ503 May 21, 2023
b51b860
feat: 지하철 요금 계산 및 조회 기능 추가
JJ503 May 21, 2023
e1729e7
refactor: 코드 컨벤션 적용
JJ503 May 21, 2023
c8e5200
test: SectionDao 단위 테스트 추가 및 컨벤션 적용
JJ503 May 21, 2023
d00523e
test: StationDao 단위 테스트 추가 및 컨벤션 적용
JJ503 May 21, 2023
fff3ab8
refactor: LineDaoTest, SectionDaoTest given-when-then 패턴 적용
JJ503 May 21, 2023
a2d3262
test: 테스트 초기 정보 수정
JJ503 May 22, 2023
b96cc79
fix: 역 정렬 잘못된 로직 수정
JJ503 May 22, 2023
7ab3454
test: LineDaoTest 초기 정보 수정
JJ503 May 22, 2023
65a7139
refactor: 정적팩토리메서드로 객체 생성
JJ503 May 22, 2023
3519845
test: LineService 단위 테스트 추가
JJ503 May 22, 2023
7430b1b
feat: 누락된 fare 파일 추가
JJ503 May 22, 2023
6a8592c
refactor: 중복 코드 제거
JJ503 May 22, 2023
d3b33bd
refactor: 중복되는 상수들 TestFeature로 묶음
JJ503 May 22, 2023
c6d8dc0
test: Sections 단위 테스트 추가
JJ503 May 22, 2023
95251c7
test: Map 단위 테스트 추가
JJ503 May 22, 2023
4571dba
test: Fare 단위 테스트 추가
JJ503 May 22, 2023
c3dce4d
test: PathService 단위 테스트 추가
JJ503 May 22, 2023
3df8609
test: SectionService 단위 테스트 추가
JJ503 May 22, 2023
6811348
docs: 완료한 기능 목록 체크
JJ503 May 24, 2023
5ddbeea
test: StationService 단위 테스트 추가
JJ503 May 24, 2023
8c95325
refactor: SQLException GlobalExceptionHandler로 통합
JJ503 May 25, 2023
f078c63
refactor: 주석 제거
JJ503 May 25, 2023
99b8ce2
feat: MethodArgumentNotValidException에 대한 예외 처리 추가
JJ503 May 25, 2023
247f12c
test: Line 통합 테스트 추가
JJ503 May 25, 2023
378c5f6
test: StationController 단위 테스트 추가
JJ503 May 25, 2023
c947831
test: SectionController 단위 테스트 추가
JJ503 May 25, 2023
6691c86
test: PathController 단위 테스트 추가
JJ503 May 25, 2023
197d9db
test: SectionController 통합 테스트 추가
JJ503 May 25, 2023
1c2346a
fix: 코드 수정으로 인한 테스트 오류 해결
JJ503 May 25, 2023
e7593a0
test: PathController 통합 테스트 추가
JJ503 May 25, 2023
ee150fa
refactor: 존재 여부에 대해 exist를 통해 확인하도록 수정
JJ503 Jun 3, 2023
5127fe8
refactor: 중복코드 제거를 위한 메서드 분리
JJ503 Jun 3, 2023
8f7c83e
refactor: 노선의 구간 존재 여부 확인을 dao에서 하도록 수정
JJ503 Jun 4, 2023
b32430d
refactor: 사용하지 않는 생성자 제거
JJ503 Jun 4, 2023
0227734
feat: StationService delete 역 존재 여부 검증 추가
JJ503 Jun 4, 2023
02bcca7
refactor: 생성자 체이닝 적용
JJ503 Jun 4, 2023
c2cc9e2
refactor: SubwayMap 클래스 네이밍 변성
JJ503 Jun 4, 2023
9100f67
refactor: 사용하지 않는 생성자 제거
JJ503 Jun 4, 2023
4093128
refactor: 한 메서드가 10라인이 넘지 않도록 수정
JJ503 Jun 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: Station을 도메인과 엔티티로 분리
  • Loading branch information
JJ503 committed May 18, 2023
commit b803d60c3c814949196505487d39b4178dde7165
32 changes: 20 additions & 12 deletions src/main/java/subway/application/StationService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package subway.application;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import subway.dao.StationDao;
import subway.dao.entity.StationEntity;
import subway.domain.Station;
import subway.dto.request.StationRequest;
import subway.dto.response.StationResponse;
import subway.exception.NotFoundException;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StationService {

Expand All @@ -20,33 +22,39 @@ public StationService(StationDao stationDao) {

public StationResponse saveStation(StationRequest stationRequest) {
checkDuplicatedStationName(stationRequest);
Station station = stationDao.insert(new Station(stationRequest.getName()));
return StationResponse.of(station);
StationEntity station = stationDao.insert(new StationEntity(stationRequest.getName()));
return StationResponse.of(Station.from(station));
}

public StationResponse findStationResponseById(Long id) {
Station station = stationDao.findById(id).orElseThrow(() -> new NotFoundException("해당 역이 존재하지 않습니다."));
return StationResponse.of(station);
StationEntity station = stationDao.findById(id)
.orElseThrow(() -> new NotFoundException("해당 역이 존재하지 않습니다."));
return StationResponse.of(Station.from(station));
}

public List<StationResponse> findAllStationResponses() {
List<Station> stations = stationDao.findAll();
List<StationEntity> stationEntities = stationDao.findAll();

List<Station> stations = stationEntities.stream()
.map(Station::from)
.collect(Collectors.toList());

return stations.stream()
.map(StationResponse::of)
.collect(Collectors.toList());
.map(StationResponse::of)
.collect(Collectors.toList());
}

public void updateStation(Long id, StationRequest stationRequest) {
stationDao.update(new Station(id, stationRequest.getName()));
stationDao.update(new StationEntity(id, stationRequest.getName()));
}

public void deleteStationById(Long id) {
stationDao.deleteById(id);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

존재하지 않는 역의 id로 삭제요청을 보내는 경우에 대한 처리가 필요할 것 같아요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다!
해당 id를 먼저 검증하도록 수정했습니다.


private void checkDuplicatedStationName(StationRequest stationRequest) {
if (stationDao.findByName(stationRequest.getName()).isPresent()) {
private void checkDuplicatedStationName(StationRequest stationRequest) {
if (stationDao.findByName(stationRequest.getName())
.isPresent()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exists 쿼리를 통해 한 번에 존재여부 확인을 할 수도 있겠네요 ㅎㅎ

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

존재 여부에 대한 확인만 필요하다면 굳이 findBy~를 통해 찾아와 레코드를 가져오고 객체를 생성하는 과정을 수행하는 것보다는 확인만 하고 데이터를 가져오지 않는 방법의 성능이 더 낫겠네요!
좋은 팁 감사합니다!!

throw new IllegalArgumentException("이미 존재하는 역 이름 입니다");
}
}
Expand Down
54 changes: 30 additions & 24 deletions src/main/java/subway/dao/StationDao.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,76 @@
package subway.dao;

import java.util.List;
import java.util.Optional;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
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 subway.domain.Station;
import subway.dao.entity.StationEntity;

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

@Repository
public class StationDao {
private final JdbcTemplate jdbcTemplate;
private final NamedParameterJdbcTemplate jdbcTemplate;
private final SimpleJdbcInsert insertAction;

private RowMapper<Station> rowMapper = (rs, rowNum) ->
new Station(
private RowMapper<StationEntity> rowMapper = (rs, rowNum) ->
new StationEntity(
rs.getLong("id"),
rs.getString("name")
);


public StationDao(JdbcTemplate jdbcTemplate, DataSource dataSource) {
public StationDao(NamedParameterJdbcTemplate jdbcTemplate, DataSource dataSource) {
this.jdbcTemplate = jdbcTemplate;
this.insertAction = new SimpleJdbcInsert(dataSource)
.withTableName("station")
.usingGeneratedKeyColumns("id");
}

public Station insert(Station station) {
public StationEntity insert(StationEntity station) {
SqlParameterSource params = new BeanPropertySqlParameterSource(station);
Long id = insertAction.executeAndReturnKey(params).longValue();
return new Station(id, station.getName());
return new StationEntity(id, station.getName());
}

public List<Station> findAll() {
String sql = "select * from STATION";
public List<StationEntity> findAll() {
String sql = "SELECT * FROM station";
return jdbcTemplate.query(sql, rowMapper);
}

public Optional<Station> findById(Long id) {
String sql = "select * from STATION where id = ?";
public Optional<StationEntity> findById(Long id) {
String sql = "SELECT * from station WHERE id = :id";
SqlParameterSource source = new MapSqlParameterSource("id", id);
try {
return Optional.of(jdbcTemplate.queryForObject(sql, rowMapper, id));
return Optional.of(jdbcTemplate.queryForObject(sql, source, rowMapper));
} catch (DataAccessException exception) {
return Optional.empty();
}
}

public void update(Station newStation) {
String sql = "update STATION set name = ? where id = ?";
jdbcTemplate.update(sql, new Object[]{newStation.getName(), newStation.getId()});
public void update(StationEntity newStation) {
String sql = "UPDATE station SET name = :name WHERE id = :id";
SqlParameterSource source = new BeanPropertySqlParameterSource(newStation);
jdbcTemplate.update(sql, source);
}

public void deleteById(Long id) {
String sql = "delete from STATION where id = ?";
jdbcTemplate.update(sql, id);
String sql = "DELETE FROM station WHERE id = :id";
SqlParameterSource source = new MapSqlParameterSource("id", id);
jdbcTemplate.update(sql, source);
}

public Optional<Station> findByName(String name) {
String sql = "select * from STATION where name = ?";
public Optional<StationEntity> findByName(String name) {
String sql = "SELECT * FROM station WHERE name = :name";
SqlParameterSource source = new MapSqlParameterSource("name", name);
try {
return Optional.of(jdbcTemplate.queryForObject(sql, rowMapper, name));
return Optional.of(jdbcTemplate.queryForObject(sql, source, rowMapper));
} catch (DataAccessException exception) {
return Optional.empty();
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/subway/dao/entity/StationEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ public class StationEntity {
private Long id;
private String name;

public StationEntity(String name) {
this.name = name;
}

public StationEntity(Long id, String name) {
this.id = id;
this.name = name;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/subway/domain/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ private Section(Station upStation, Station downStation, Integer distance) {

public static Section from(SectionStationMapper sectionStationMapper) {
return new Section(
new Station(sectionStationMapper.getUpStationId(), sectionStationMapper.getUpStationName()),
new Station(sectionStationMapper.getDownStationId(), sectionStationMapper.getDownStationName()),
Station.of(sectionStationMapper.getUpStationId(), sectionStationMapper.getUpStationName()),
Station.of(sectionStationMapper.getDownStationId(), sectionStationMapper.getDownStationName()),
sectionStationMapper.getDistance()
);
}
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/subway/domain/Station.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package subway.domain;

import subway.dao.entity.StationEntity;

import java.util.Objects;

public class Station {
private Long id;
private String name;

public Station() {
}
private final Long id;
private final String name;

public Station(Long id, String name) {
private Station(Long id, String name) {
this.id = id;
this.name = name;
}

public Station(String name) {
this.name = name;
public static Station from(StationEntity stationEntity) {
return new Station(stationEntity.getId(), stationEntity.getName());
}

public static Station of(Long id, String name) {
return new Station(id, name);
}

public Long getId() {
Expand Down