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

๐Ÿš€ 1๋‹จ๊ณ„ - ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ฐœ์„  #606

Open
wants to merge 15 commits into
base: jjisun
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
df2d0b1
3rd init
boorownie Jan 16, 2022
c66bb09
4th init
boorownie Jan 16, 2022
37f79e0
5th init
boorownie Jun 19, 2022
3244b9d
6th init
boorownie Dec 26, 2022
b01ce62
step1: ๊ตฌ๊ธ€ ํŽ˜์ด์ง€ ์ ‘๊ทผ ํ…Œ์ŠคํŠธ
jjisun Jun 26, 2023
d202435
step1: ๊ตฌ๊ธ€ ํŽ˜์ด์ง€ ์ ‘๊ทผ ํ…Œ์ŠคํŠธ (#763)
jjisun Jun 26, 2023
053adea
๐Ÿš€ 2๋‹จ๊ณ„ - ์ง€ํ•˜์ฒ  ๋…ธ์„  ๊ด€๋ฆฌ (#899)
jjisun Jul 6, 2023
ecccf73
test: ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ์ถ”๊ฐ€, ์‚ญ์ œ ์ธ์ˆ˜ํ…Œ์ŠคํŠธ ์ž‘์„ฑ, ๊ธฐ์กด ํ…Œ์ŠคํŠธ ์ˆ˜์ •
jjisun Jul 14, 2023
02b8012
feat: ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ์ถ”๊ฐ€, ์‚ญ์ œ ๊ตฌํ˜„
jjisun Jul 14, 2023
80cea42
test: ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ฐœ์„  ์ธ์ˆ˜ํ…Œ์ŠคํŠธ ์ž‘์„ฑ
jjisun Jul 14, 2023
c350315
refactor: ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ๊ด€๋ฆฌ ๋ฆฌํŒฉํ† ๋ง ๋ฆฌ๋ทฐ ๋ฐ˜์˜
jjisun Jul 17, 2023
81dc0c6
Merge branch 'step3' into tdd-step1
jjisun Jul 18, 2023
293651e
feature: ์ง€ํ•˜์ฒ  ๊ตฌ๊ฐ„ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ ๊ฐœ์„ 
jjisun Jul 18, 2023
7cc45cf
Merge branch 'step4' into step1
jjisun Jul 18, 2023
10f13fa
Merge branch 'jjisun' into step1
jjisun Jul 18, 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
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ dependencies {
// log
implementation 'net.rakugakibox.spring.boot:logback-access-spring-boot-starter:2.7.1'

//lombok
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

// jgraph
implementation 'org.jgrapht:jgrapht-core:1.0.1'

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/subway/SubwayApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package subway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SubwayApplication {

public static void main(String[] args) {
SpringApplication.run(SubwayApplication.class, args);
}

}
36 changes: 36 additions & 0 deletions src/main/java/subway/common/DatabaseCleaner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package subway.common;

import java.util.List;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;
import javax.transaction.Transactional;
import org.springframework.stereotype.Component;

@Component
public class DatabaseCleaner {
private final EntityManager entityManager;
private final List<String> tableNames;

public DatabaseCleaner(final EntityManager entityManager) {
this.entityManager = entityManager;
this.tableNames = entityManager.getMetamodel()
.getEntities()
.stream()
.map(EntityType::getName)
.map(String::toLowerCase)
.collect(Collectors.toList());
}

@Transactional
public void execute() {
entityManager.flush();
entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate();

for (String tableName : tableNames) {
entityManager.createNativeQuery("TRUNCATE TABLE " + tableName).executeUpdate();
}

entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate();
}
Comment on lines +25 to +35

Choose a reason for hiding this comment

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

์ธ์ˆ˜ํ…Œ์ŠคํŠธ ๊ฒฉ๋ฆฌ ๐Ÿ‘

}
47 changes: 47 additions & 0 deletions src/main/java/subway/controller/LineController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package subway.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import subway.dto.request.LineModifyRequest;
import subway.dto.request.LineRequest;
import subway.dto.response.LineResponse;
import subway.service.LineService;

import java.net.URI;
import java.util.List;

@RestController
@RequiredArgsConstructor
public class LineController {
private final LineService lineService;

@PostMapping("/lines")
public ResponseEntity<LineResponse> createLine(@RequestBody LineRequest request) {
LineResponse line = lineService.saveLine(request);
return ResponseEntity.created(URI.create("/lines/" + line.getId())).body(line);
}

@GetMapping("/lines")
public ResponseEntity<List<LineResponse>> findLines() {
return ResponseEntity.ok(lineService.findLines());
}

@GetMapping("/lines/{id}")
public ResponseEntity<LineResponse> findLine(@PathVariable Long id) {
LineResponse line = lineService.findLineResponse(id);
return ResponseEntity.created(URI.create("/lines/" + line.getId())).body(line);
}

@DeleteMapping("/lines/{id}")
public ResponseEntity<Void> deleteLine(@PathVariable Long id) {
lineService.deleteLine(id);
return ResponseEntity.noContent().build();
}

@PutMapping("/lines/{id}")
public ResponseEntity<Void> modifyStation(@PathVariable Long id, @RequestBody LineModifyRequest request) {
lineService.modifyLine(id, request);
return ResponseEntity.noContent().build();
}
}
35 changes: 35 additions & 0 deletions src/main/java/subway/controller/SectionController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package subway.controller;

import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import subway.dto.request.SectionRequest;
import subway.dto.response.LineResponse;
import subway.service.SectionService;

@RequestMapping("/lines/{id}/sections")
@RestController
@RequiredArgsConstructor
public class SectionController {

private final SectionService sectionService;

@PostMapping
public ResponseEntity<LineResponse> addSection(@PathVariable Long id, @RequestBody SectionRequest request) {
LineResponse line = sectionService.addSection(id, request);
return ResponseEntity.created(URI.create("/lines/" + line.getId())).body(line);
}

@DeleteMapping
public ResponseEntity<Void> deleteSection(@PathVariable Long id, Long stationId) {
sectionService.deleteSection(id, stationId);
return ResponseEntity.noContent().build();
}

}
36 changes: 36 additions & 0 deletions src/main/java/subway/controller/StationController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package subway.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import subway.dto.request.StationRequest;
import subway.dto.response.StationResponse;
import subway.service.StationService;

import java.net.URI;
import java.util.List;

@RestController
public class StationController {
private StationService stationService;

public StationController(StationService stationService) {
this.stationService = stationService;
}

@PostMapping("/stations")
public ResponseEntity<StationResponse> createStation(@RequestBody StationRequest stationRequest) {
StationResponse station = stationService.saveStation(stationRequest);
return ResponseEntity.created(URI.create("/stations/" + station.getId())).body(station);
}

@GetMapping(value = "/stations")
public ResponseEntity<List<StationResponse>> showStations() {
return ResponseEntity.ok().body(stationService.findAllStations());
}

@DeleteMapping("/stations/{id}")
public ResponseEntity<Void> deleteStation(@PathVariable Long id) {
stationService.deleteStationById(id);
return ResponseEntity.noContent().build();
}
}
17 changes: 17 additions & 0 deletions src/main/java/subway/dto/request/LineModifyRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package subway.dto.request;


import lombok.Builder;
import lombok.Getter;

@Getter
public class LineModifyRequest {
private String name;
private String color;

@Builder
public LineModifyRequest(String name, String color) {
this.name = name;
this.color = color;
}
}
23 changes: 23 additions & 0 deletions src/main/java/subway/dto/request/LineRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package subway.dto.request;


import lombok.Builder;
import lombok.Getter;

@Getter
public class LineRequest {
private String name;
private String color;
private Long upStationId;
private Long downStationId;
private Long distance;

@Builder
public LineRequest(String name, String color, Long upStationId, Long downStationId, Long distance) {
this.name = name;
this.color = color;
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
}
}
18 changes: 18 additions & 0 deletions src/main/java/subway/dto/request/SectionRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package subway.dto.request;

import lombok.Builder;
import lombok.Getter;

@Getter
public class SectionRequest {
private Long upStationId;
private Long downStationId;
private Long distance;

@Builder
public SectionRequest(Long upStationId, Long downStationId, Long distance) {
this.upStationId = upStationId;
this.downStationId = downStationId;
this.distance = distance;
}
}
9 changes: 9 additions & 0 deletions src/main/java/subway/dto/request/StationRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package subway.dto.request;

public class StationRequest {
private String name;

public String getName() {
return name;
}
}
41 changes: 41 additions & 0 deletions src/main/java/subway/dto/response/LineResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package subway.dto.response;


import java.util.stream.Collectors;
import lombok.Builder;
import lombok.Getter;
import subway.entity.Line;
import subway.entity.Station;

import java.util.List;

@Getter
public class LineResponse {
private Long id;
private String name;
private String color;
private List<StationResponse> stations;

private Long totalDistance;

@Builder
public LineResponse(Long id, String name, String color, List<StationResponse> stations, Long totalDistance) {
this.id = id;
this.name = name;
this.color = color;
this.stations = stations;
this.totalDistance = totalDistance;
}

public static LineResponse from(Line line) {
return LineResponse.builder()
.id(line.getId())
.name(line.getName())
.color(line.getColor())
.stations(line.getStations().stream()
.map(StationResponse::from)
.collect(Collectors.toList()))
.totalDistance(line.getSections().totalDistance())
.build();
}
}
31 changes: 31 additions & 0 deletions src/main/java/subway/dto/response/StationResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package subway.dto.response;

import lombok.Builder;
import subway.entity.Station;

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

@Builder
public StationResponse(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}


public static StationResponse from(Station station) {
return StationResponse.builder()
.id(station.getId())
.name(station.getName())
.build();
}
}
47 changes: 47 additions & 0 deletions src/main/java/subway/entity/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package subway.entity;

import java.util.Arrays;
import java.util.List;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import subway.dto.request.LineModifyRequest;

@Getter
@Entity
@NoArgsConstructor
public class Line {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

private String color;

@Embedded
private Sections sections;

@Builder
public Line(String name, String color, Section section) {
this.name = name;
this.color = color;
this.sections = Sections.builder()
.sections(Arrays.asList(section))
.build();
}

public List<Station> getStations() {
return this.sections.stations();
}

public void modify(LineModifyRequest request) {
this.name = request.getName();
this.color = request.getColor();
}
}
Loading