Skip to content

Commit 8a7a5e1

Browse files
[Spring 지하철 노선도 - 1,2단계] 기론(김규철) 미션 제출합니다. (#191)
Co-authored-by: jayjaehunchoi <[email protected]> Co-authored-by: giron <[email protected]>
1 parent 26b109f commit 8a7a5e1

33 files changed

+1179
-104
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,19 @@
3737
## 📝 License
3838

3939
This project is [MIT](https://github.com/woowacourse/atdd-subway-map/blob/master/LICENSE) licensed.
40+
41+
42+
## 기능 구현 목록
43+
44+
- 지하철 역
45+
- [x] 지하철역 생성 시 이미 등록된 이름으로 요청한다면 에러를 응답
46+
47+
- 지하철 노선
48+
- [x] 동일한 노선 생성 불가
49+
- api
50+
- [x] post - /lines 등록 - name, color
51+
- [x] get - /lines - List - id, name, color
52+
- [ ] get - /lines/{id} - id, name, color
53+
- [ ] put - /lines/{id} - name, color
54+
- [ ] delete - /lines/{id}
55+

src/main/java/wooteco/subway/SubwayApplication.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
@SpringBootApplication
77
public class SubwayApplication {
88

9-
public static void main(String[] args) {
10-
SpringApplication.run(SubwayApplication.class, args);
11-
}
9+
public static void main(String[] args) {
10+
SpringApplication.run(SubwayApplication.class, args);
11+
}
1212

1313
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package wooteco.subway.dao;
2+
3+
import wooteco.subway.domain.Line;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
public interface LineRepository {
9+
10+
Line save(Line line);
11+
12+
List<Line> findAll();
13+
14+
Optional<Line> findById(Long id);
15+
16+
Optional<Line> findByName(String name);
17+
18+
void update(Long id, Line line);
19+
20+
void deleteById(Long id);
21+
22+
boolean existByName(String name);
23+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package wooteco.subway.dao;
2+
3+
import org.springframework.jdbc.core.RowMapper;
4+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
5+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
6+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
7+
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
8+
import org.springframework.stereotype.Repository;
9+
import wooteco.subway.domain.Line;
10+
11+
import javax.sql.DataSource;
12+
import java.util.List;
13+
import java.util.Optional;
14+
15+
@Repository
16+
public class LineRepositoryImpl implements LineRepository {
17+
18+
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
19+
private final SimpleJdbcInsert simpleJdbcInsert;
20+
21+
private RowMapper<Line> rowMapper() {
22+
return (resultSet, rowNum) -> {
23+
long id = resultSet.getLong("id");
24+
String name = resultSet.getString("name");
25+
String color = resultSet.getString("color");
26+
return new Line(id, name, color);
27+
};
28+
}
29+
30+
public LineRepositoryImpl(DataSource dataSource) {
31+
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
32+
this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
33+
.withTableName("line")
34+
.usingGeneratedKeyColumns("id");
35+
}
36+
37+
@Override
38+
public Line save(final Line line) {
39+
SqlParameterSource parameters = new MapSqlParameterSource()
40+
.addValue("name", line.getName())
41+
.addValue("color", line.getColor());
42+
43+
long id = simpleJdbcInsert.executeAndReturnKey(parameters).longValue();
44+
return new Line(id, line.getName(), line.getColor());
45+
}
46+
47+
@Override
48+
public List<Line> findAll() {
49+
String sql = "SELECT * FROM line";
50+
return namedParameterJdbcTemplate.query(sql, rowMapper());
51+
}
52+
53+
@Override
54+
public Optional<Line> findById(final Long id) {
55+
String sql = "SELECT * FROM line WHERE id = :id";
56+
SqlParameterSource parameters = new MapSqlParameterSource("id", id);
57+
List<Line> lines = namedParameterJdbcTemplate.query(sql, parameters, rowMapper());
58+
return getOptional(lines);
59+
}
60+
61+
@Override
62+
public Optional<Line> findByName(final String name) {
63+
String sql = "SELECT * FROM line WHERE name = :name";
64+
SqlParameterSource parameters = new MapSqlParameterSource("name", name);
65+
List<Line> lines = namedParameterJdbcTemplate.query(sql, parameters, rowMapper());
66+
return getOptional(lines);
67+
}
68+
69+
private Optional<Line> getOptional(List<Line> lines) {
70+
if (lines.isEmpty()) {
71+
return Optional.empty();
72+
}
73+
return Optional.ofNullable(lines.get(0));
74+
}
75+
76+
@Override
77+
public void update(final Long id, final Line line) {
78+
String sql = "UPDATE line SET name = :name, color = :color WHERE id = :id";
79+
80+
SqlParameterSource nameParameters = new MapSqlParameterSource()
81+
.addValue("id", id)
82+
.addValue("name", line.getName())
83+
.addValue("color", line.getColor());
84+
85+
namedParameterJdbcTemplate.update(sql, nameParameters);
86+
}
87+
88+
@Override
89+
public void deleteById(Long id) {
90+
String sql = "DELETE FROM line WHERE id = :id";
91+
SqlParameterSource parameters = new MapSqlParameterSource("id", id);
92+
namedParameterJdbcTemplate.update(sql, parameters);
93+
}
94+
95+
@Override
96+
public boolean existByName(String name) {
97+
final String sql = "SELECT COUNT(*) FROM line WHERE name = :name";
98+
SqlParameterSource parameters = new MapSqlParameterSource("name", name);
99+
Integer count = namedParameterJdbcTemplate.queryForObject(sql, parameters, Integer.class);
100+
return count != 0;
101+
}
102+
}

src/main/java/wooteco/subway/dao/StationDao.java

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package wooteco.subway.dao;
2+
3+
import wooteco.subway.domain.Station;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
public interface StationRepository {
9+
10+
Station save(final Station station);
11+
12+
List<Station> findAll();
13+
14+
void deleteById(Long id);
15+
16+
Optional<Station> findByName(String name);
17+
18+
Optional<Station> findById(Long id);
19+
20+
boolean existByName(String name);
21+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package wooteco.subway.dao;
2+
3+
import org.springframework.jdbc.core.RowMapper;
4+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
5+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
6+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
7+
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
8+
import org.springframework.stereotype.Repository;
9+
import wooteco.subway.domain.Station;
10+
11+
import javax.sql.DataSource;
12+
import java.util.List;
13+
import java.util.Optional;
14+
15+
@Repository
16+
public class StationRepositoryImpl implements StationRepository {
17+
18+
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
19+
private final SimpleJdbcInsert simpleJdbcInsert;
20+
21+
private RowMapper<Station> rowMapper() {
22+
return (resultSet, rowNum) -> {
23+
long id = resultSet.getLong("id");
24+
String name = resultSet.getString("name");
25+
return new Station(id, name);
26+
};
27+
}
28+
29+
public StationRepositoryImpl(DataSource dataSource) {
30+
this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
31+
this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
32+
.withTableName("station")
33+
.usingGeneratedKeyColumns("id");
34+
}
35+
36+
@Override
37+
public Station save(final Station station) {
38+
SqlParameterSource parameters = new MapSqlParameterSource()
39+
.addValue("name", station.getName());
40+
Long id = simpleJdbcInsert.executeAndReturnKey(parameters).longValue();
41+
return new Station(id, station.getName());
42+
}
43+
44+
@Override
45+
public List<Station> findAll() {
46+
String sql = "SELECT * FROM station";
47+
RowMapper<Station> stationRowMapper = rowMapper();
48+
return namedParameterJdbcTemplate.query(sql, stationRowMapper);
49+
}
50+
51+
@Override
52+
public void deleteById(final Long id) {
53+
String sql = "DELETE FROM station WHERE id = :id";
54+
namedParameterJdbcTemplate.update(sql, new MapSqlParameterSource("id", id));
55+
}
56+
57+
@Override
58+
public Optional<Station> findByName(final String name) {
59+
String sql = "SELECT * FROM station WHERE name = :name";
60+
SqlParameterSource parameters = new MapSqlParameterSource("name", name);
61+
List<Station> stations = namedParameterJdbcTemplate.query(sql, parameters, rowMapper());
62+
return getOptional(stations);
63+
}
64+
65+
@Override
66+
public Optional<Station> findById(Long id) {
67+
String sql = "SELECT * FROM station WHERE id = :id";
68+
SqlParameterSource parameters = new MapSqlParameterSource("id", id);
69+
List<Station> stations = namedParameterJdbcTemplate.query(sql, parameters, rowMapper());
70+
return getOptional(stations);
71+
}
72+
73+
private Optional<Station> getOptional(List<Station> stations) {
74+
if (stations.isEmpty()) {
75+
return Optional.empty();
76+
}
77+
return Optional.ofNullable(stations.get(0));
78+
}
79+
80+
@Override
81+
public boolean existByName(String name) {
82+
final String sql = "SELECT COUNT(*) FROM station WHERE name = :name";
83+
SqlParameterSource parameters = new MapSqlParameterSource("name", name);
84+
Integer count = namedParameterJdbcTemplate.queryForObject(sql, parameters, Integer.class);
85+
return count != 0;
86+
}
87+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package wooteco.subway.domain;
2+
3+
import java.util.Objects;
4+
5+
public class Line {
6+
7+
private final Long id;
8+
private final String name;
9+
private final String color;
10+
11+
public Line(String name, String color) {
12+
this(null, name, color);
13+
}
14+
15+
public Line(Long id, String name, String color) {
16+
this.id = id;
17+
this.name = name;
18+
this.color = color;
19+
}
20+
21+
public Long getId() {
22+
return id;
23+
}
24+
25+
public String getName() {
26+
return name;
27+
}
28+
29+
public String getColor() {
30+
return color;
31+
}
32+
33+
@Override
34+
public boolean equals(Object o) {
35+
if (this == o) return true;
36+
if (o == null || getClass() != o.getClass()) return false;
37+
Line line = (Line) o;
38+
return Objects.equals(name, line.name);
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
return Objects.hash(name);
44+
}
45+
}
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
11
package wooteco.subway.domain;
22

3+
import java.util.Objects;
4+
35
public class Station {
4-
private Long id;
5-
private String name;
6+
private final Long id;
7+
private final String name;
68

7-
public Station() {
9+
public Station(String name) {
10+
this(null, name);
811
}
912

1013
public Station(Long id, String name) {
1114
this.id = id;
1215
this.name = name;
1316
}
1417

15-
public Station(String name) {
16-
this.name = name;
17-
}
18-
1918
public Long getId() {
2019
return id;
2120
}
2221

2322
public String getName() {
2423
return name;
2524
}
25+
26+
@Override
27+
public boolean equals(Object o) {
28+
if (this == o) return true;
29+
if (o == null || getClass() != o.getClass()) return false;
30+
Station station = (Station) o;
31+
return Objects.equals(name, station.name);
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
return Objects.hash(name);
37+
}
2638
}
2739

0 commit comments

Comments
 (0)