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

Step1,2,3 통합 PR #559

Open
wants to merge 24 commits into
base: hoon25
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
baa4f09
7th default sample
boorownie Jul 11, 2023
98cc7d0
test(member): getMyInfo ATDD(step1)
hoon25 Oct 7, 2023
afaf335
feat(member): 토큰기반_내정보조회_기능추가(step1)
hoon25 Oct 7, 2023
8318a25
test(githubClient): githubClient Fake 생성(step2)
hoon25 Oct 7, 2023
8ddeeef
feat(access_token): 기능구현(step2)
hoon25 Oct 7, 2023
2cf7ecc
feat(github_profile): 기능구현(step2)
hoon25 Oct 7, 2023
1e10816
test(favorites): 생성 ATDD
hoon25 Oct 7, 2023
54ae731
feat(favorites): 생성 Controller 뼈대구현
hoon25 Oct 7, 2023
d7197a4
feat(favorite): 생성기능 구현
hoon25 Oct 7, 2023
02d8d41
test(favorite): 조회기능 ATDD
hoon25 Oct 7, 2023
6f36363
feat(favorite): 조회기능구현
hoon25 Oct 7, 2023
a278b6e
test(favorite): 삭제기능 ATDD
hoon25 Oct 7, 2023
3c60e68
feat(favorite): 삭제기능 구현
hoon25 Oct 7, 2023
4446313
test(favorite): 비로그인체크 atdd
hoon25 Oct 7, 2023
8f49d72
feat(favorite): 비로그인체크 기능구현
hoon25 Oct 7, 2023
2c020ba
fix(favorite): create-test-fix
hoon25 Oct 7, 2023
4569e82
test(favorite): 본인의 즐겨찾기인지 검증 atdd
hoon25 Oct 7, 2023
bbfddf5
test(favorite): 본인의 즐겨찾기인지 검증 tdd
hoon25 Oct 7, 2023
16182d8
feat(favorite): 본인의 즐겨찾기인지 검증 domain 구현
hoon25 Oct 7, 2023
70ea6d5
feat(favorite): 본인의 즐겨찾기인지 검증 기능 구현
hoon25 Oct 7, 2023
0f01ac0
test(favorite): 경로검증 ATDD
hoon25 Oct 7, 2023
9fa7dd3
test(favorite): 경로검증 TDD
hoon25 Oct 7, 2023
bc12ef3
test(favorite): 경로검증 Domain TDD 삭제
hoon25 Oct 7, 2023
05fb879
feat(favorite): 경로검증 기능구현
hoon25 Oct 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public boolean supportsParameter(MethodParameter parameter) {
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String authorization = webRequest.getHeader("Authorization");
if (authorization == null) {
throw new AuthenticationException();
}
if (!"bearer".equalsIgnoreCase(authorization.split(" ")[0])) {
throw new AuthenticationException();
}
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/nextstep/member/application/FavoriteService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package nextstep.member.application;

import nextstep.member.application.dto.CreateFavoriteRequest;
import nextstep.member.application.dto.FavoriteResponse;
import nextstep.member.domain.Favorite;
import nextstep.member.domain.FavoriteRepository;
import nextstep.member.domain.Member;
import nextstep.member.domain.MemberRepository;
import nextstep.subway.applicaion.LineService;
import nextstep.subway.applicaion.StationService;
import nextstep.subway.domain.Line;
import nextstep.subway.domain.Station;
import nextstep.subway.domain.SubwayMap;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FavoriteService {

private FavoriteRepository favoriteRepository;
private StationService stationService;
private MemberRepository memberRepository;
private LineService lineService;

public FavoriteService(FavoriteRepository favoriteRepository, StationService stationService, MemberRepository memberRepository, LineService lineService) {
this.favoriteRepository = favoriteRepository;
this.stationService = stationService;
this.memberRepository = memberRepository;
this.lineService = lineService;
}

public Favorite createFavorite(CreateFavoriteRequest request, String email) {
Station source = stationService.findById(request.getSource());
Station target = stationService.findById(request.getTarget());

List<Line> lines = lineService.findLines();
SubwayMap subwayMap = new SubwayMap(lines);
if (subwayMap.findPath(source, target) == null) {
throw new IllegalArgumentException();
}

Member member = memberRepository.findByEmail(email).orElseThrow(RuntimeException::new);
return favoriteRepository.save(new Favorite(member, source, target));
}

public FavoriteResponse findFavorite(Long id, String email) {
Favorite favorite = favoriteRepository.findById(id).orElseThrow(RuntimeException::new);
Member member = memberRepository.findByEmail(email).orElseThrow(RuntimeException::new);
favorite.checkOwner(member);
return FavoriteResponse.of(favorite);
}

public void deleteFavorite(Long id, String email) {
Member member = memberRepository.findByEmail(email).orElseThrow(RuntimeException::new);
Favorite favorite = favoriteRepository.findById(id).orElseThrow(RuntimeException::new);
favorite.checkOwner(member);
favoriteRepository.delete(favorite);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nextstep.member.application.dto;

public class CreateFavoriteRequest {
private Long source;
private Long target;

public CreateFavoriteRequest() {
}

public CreateFavoriteRequest(Long source, Long target) {
this.source = source;
this.target = target;
}

public Long getSource() {
return source;
}

public Long getTarget() {
return target;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nextstep.member.application.dto;

import nextstep.member.domain.Favorite;
import nextstep.subway.applicaion.dto.StationResponse;

public class FavoriteResponse {

private Long id;
private StationResponse source;
private StationResponse target;

public FavoriteResponse() {
}

public FavoriteResponse(Long id, StationResponse source, StationResponse target) {
this.id = id;
this.source = source;
this.target = target;
}


public static FavoriteResponse of(Favorite favorite) {
return new FavoriteResponse(favorite.getId(), StationResponse.of(favorite.getSource()), StationResponse.of(favorite.getTarget()));
}

public Long getId() {
return id;
}

public StationResponse getSource() {
return source;
}

public StationResponse getTarget() {
return target;
}
}
54 changes: 54 additions & 0 deletions src/main/java/nextstep/member/domain/Favorite.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package nextstep.member.domain;

import nextstep.auth.AuthenticationException;
import nextstep.subway.domain.Station;

import javax.persistence.*;

@Entity
public class Favorite {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "source_id")
private Station source;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "target_id")
private Station target;


public Favorite() {
}

public Favorite(Member member, Station source, Station target) {
this.member = member;
this.source = source;
this.target = target;
}

public Long getId() {
return id;
}

public Station getSource() {
return source;
}

public Station getTarget() {
return target;
}

public void checkOwner(Member member) {
if (this.member != member) {
throw new AuthenticationException();
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/nextstep/member/domain/FavoriteRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package nextstep.member.domain;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface FavoriteRepository extends JpaRepository<Favorite, Long> {
Optional<Favorite> findByMember(Long memberId);
}
42 changes: 42 additions & 0 deletions src/main/java/nextstep/member/ui/FavoriteController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nextstep.member.ui;

import nextstep.auth.principal.AuthenticationPrincipal;
import nextstep.auth.principal.UserPrincipal;
import nextstep.member.application.FavoriteService;
import nextstep.member.application.dto.CreateFavoriteRequest;
import nextstep.member.application.dto.FavoriteResponse;
import nextstep.member.domain.Favorite;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

@RestController
public class FavoriteController {

private FavoriteService favoriteService;

public FavoriteController(FavoriteService favoriteService) {
this.favoriteService = favoriteService;
}

@PostMapping("/favorites")
public ResponseEntity<Void> createFavorite(@RequestBody CreateFavoriteRequest request, @AuthenticationPrincipal UserPrincipal userPrincipal) {
Favorite favorite = favoriteService.createFavorite(request, userPrincipal.getUsername());
return ResponseEntity.created(URI.create("/favorites/" + favorite.getId())).build();
}

@GetMapping("/favorites/{id}")
public ResponseEntity<FavoriteResponse> findFavorite(@PathVariable Long id, @AuthenticationPrincipal UserPrincipal userPrincipal) {
String email = userPrincipal.getUsername();
FavoriteResponse favorite = favoriteService.findFavorite(id, email);
return ResponseEntity.ok().body(favorite);
}

@DeleteMapping("/favorites/{id}")
public ResponseEntity<Void> deleteFavorite(@PathVariable Long id, @AuthenticationPrincipal UserPrincipal userPrincipal) {
String email = userPrincipal.getUsername();
favoriteService.deleteFavorite(id, email);
return ResponseEntity.noContent().build();
}
}
7 changes: 5 additions & 2 deletions src/main/java/nextstep/member/ui/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package nextstep.member.ui;

import nextstep.auth.principal.AuthenticationPrincipal;
import nextstep.auth.principal.UserPrincipal;
import nextstep.member.application.MemberService;
import nextstep.member.application.dto.MemberRequest;
import nextstep.member.application.dto.MemberResponse;
Expand Down Expand Up @@ -41,8 +43,9 @@ public ResponseEntity<MemberResponse> deleteMember(@PathVariable Long id) {
}

@GetMapping("/members/me")
public ResponseEntity<MemberResponse> findMemberOfMine() {
return ResponseEntity.ok().build();
public ResponseEntity<MemberResponse> findMemberOfMine(@AuthenticationPrincipal UserPrincipal userPrincipal) {
MemberResponse memberResponse = memberService.findMemberByEmail(userPrincipal.getUsername());
return ResponseEntity.ok().body(memberResponse);
}
}

89 changes: 89 additions & 0 deletions src/main/java/nextstep/subway/applicaion/LineService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package nextstep.subway.applicaion;

import nextstep.subway.applicaion.dto.LineRequest;
import nextstep.subway.applicaion.dto.LineResponse;
import nextstep.subway.applicaion.dto.SectionRequest;
import nextstep.subway.applicaion.dto.StationResponse;
import nextstep.subway.domain.Line;
import nextstep.subway.domain.LineRepository;
import nextstep.subway.domain.Station;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional(readOnly = true)
public class LineService {
private LineRepository lineRepository;
private StationService stationService;

public LineService(LineRepository lineRepository, StationService stationService) {
this.lineRepository = lineRepository;
this.stationService = stationService;
}

@Transactional
public LineResponse saveLine(LineRequest request) {
Line line = lineRepository.save(new Line(request.getName(), request.getColor()));
if (request.getUpStationId() != null && request.getDownStationId() != null && request.getDistance() != 0) {
Station upStation = stationService.findById(request.getUpStationId());
Station downStation = stationService.findById(request.getDownStationId());
line.addSection(upStation, downStation, request.getDistance());
}
return LineResponse.of(line);
}

public List<Line> findLines() {
return lineRepository.findAll();
}

public List<LineResponse> findLineResponses() {
return lineRepository.findAll().stream()
.map(LineResponse::of)
.collect(Collectors.toList());
}

public LineResponse findLineResponseById(Long id) {
return LineResponse.of(findById(id));
}

public Line findById(Long id) {
return lineRepository.findById(id).orElseThrow(IllegalArgumentException::new);
}

@Transactional
public void updateLine(Long id, LineRequest lineRequest) {
Line line = findById(id);
line.update(lineRequest.getName(), lineRequest.getColor());
}

@Transactional
public void deleteLine(Long id) {
lineRepository.deleteById(id);
}

@Transactional
public void addSection(Long lineId, SectionRequest sectionRequest) {
Station upStation = stationService.findById(sectionRequest.getUpStationId());
Station downStation = stationService.findById(sectionRequest.getDownStationId());
Line line = findById(lineId);

line.addSection(upStation, downStation, sectionRequest.getDistance());
}

private List<StationResponse> createStationResponses(Line line) {
return line.getStations().stream()
.map(it -> stationService.createStationResponse(it))
.collect(Collectors.toList());
}

@Transactional
public void deleteSection(Long lineId, Long stationId) {
Line line = findById(lineId);
Station station = stationService.findById(stationId);

line.deleteSection(station);
}
}
31 changes: 31 additions & 0 deletions src/main/java/nextstep/subway/applicaion/PathService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nextstep.subway.applicaion;

import nextstep.subway.applicaion.dto.PathResponse;
import nextstep.subway.domain.Line;
import nextstep.subway.domain.Path;
import nextstep.subway.domain.Station;
import nextstep.subway.domain.SubwayMap;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PathService {
private LineService lineService;
private StationService stationService;

public PathService(LineService lineService, StationService stationService) {
this.lineService = lineService;
this.stationService = stationService;
}

public PathResponse findPath(Long source, Long target) {
Station upStation = stationService.findById(source);
Station downStation = stationService.findById(target);
List<Line> lines = lineService.findLines();
SubwayMap subwayMap = new SubwayMap(lines);
Path path = subwayMap.findPath(upStation, downStation);

return PathResponse.of(path);
}
}
Loading