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 : 즐겨찾기 κΈ°λŠ₯ κ΅¬ν˜„ #241

Open
wants to merge 10 commits into
base: minyul
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
8 changes: 4 additions & 4 deletions src/main/java/nextstep/member/application/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,24 @@ public FavoriteResponse createFavorite(LoginMember loginMember, FavoriteRequest
Member member = memberRepository.findById(loginMember.getId()).orElseThrow(RuntimeException::new);
Station source = stationService.findById(favoriteRequest.getSource());
Station target = stationService.findById(favoriteRequest.getTarget());
Favorite favorite = new Favorite(member, source, target);
Favorite favorite = new Favorite(member.getId(), source, target);
favoriteRepository.save(favorite);

return FavoriteResponse.of(favorite);
}

public List<FavoriteResponse> findFavoritesOfMine(LoginMember loginMember) {
Member member = memberRepository.findById(loginMember.getId()).orElseThrow(RuntimeException::new);
List<Favorite> favorites = favoriteRepository.findByMember(member.getId());

return member.getFavorites().stream()
return favorites.stream()
.map(FavoriteResponse::of)
.collect(Collectors.toList());
}

public void deleteFavorite(LoginMember loginMember, Long favoriteId) {
Member member = memberRepository.findById(loginMember.getId()).orElseThrow(RuntimeException::new);
Favorite favorite = favoriteRepository.findById(favoriteId).orElseThrow(RuntimeException::new);

member.removeFavorite(favorite);
favoriteRepository.delete(favorite);
}
}
9 changes: 4 additions & 5 deletions src/main/java/nextstep/member/domain/Favorite.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ public class Favorite {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

Choose a reason for hiding this comment

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

ν•„λ“œλͺ…이 memberId둜 ν•˜λŠ”κ²Œ μ ν•©ν•˜μ§€ μ•Šμ„κΉŒμš”?

Choose a reason for hiding this comment

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

@minyul

κ³Όκ±° μ‹ μž… ν”„λ‘œμ νŠΈλ‘œ 둜그인μͺ½μ„ κ΅¬ν˜„ν•˜κ³  μ½”λ“œλ¦¬λ·°λ₯Ό λ°›μ•˜μ„ λ•Œ, μ‚¬μš©μžμ™€ κ΄€λ ¨λœ 뢀뢄은 직접참쑰, μ•„λ‹Œ κ²½μš°μ—λŠ” κ°„μ ‘ μ°Έμ‘°λ₯Ό ν•˜λΌκ³  ν”Όλ“œλ°±μ„ λ°›μ•˜μ—ˆλŠ”λ°μš”! μ–΄λ–»κ²Œ μƒκ°ν•˜μ‹œλ‚˜μš”?

μ‹ μž… ν”„λ‘œμ νŠΈμ—μ„œ μ–΄λ–€ μƒν™©μ΄μ—ˆλŠ” μ§€λŠ” μ œκ°€ μ •ν™•νžˆ μ•Œ 지 λͺ»ν•˜μ—¬μ„œ μ •ν™•νžˆ λ‹΅λ³€λ“œλ¦¬μ§€ λͺ»ν•˜κ² λ„€μš”.
ν•˜μ§€λ§Œ μ•„λ§ˆ ν”Όλ“œλ°± μ£Όμ‹  μ‚¬μˆ˜λΆ„μ˜ μ˜λ„κ°€ 항상 μ‚¬μš©μžκ°€ κ΄€λ ¨λœ 뢀뢄은 무쑰건 μ§μ ‘μ°Έμ‘°ν•˜κ³ , 그게 μ•„λ‹ˆλΌλ©΄ κ°„μ ‘ 참쑰해라. μš”λŸ°μ˜λ―ΈλŠ” μ•„λ‹ˆμ—ˆμ„ 것 κ°™μ•„μš”.

μœ„μ—λ„ λ§μ”€λ“œλ Έλ‹€μ‹œν”Ό 직접참쑰, κ°„μ ‘μ°Έμ‘°λŠ” 각자 μž₯단점이 μžˆμŠ΅λ‹ˆλ‹€. μ € 같은 κ²½μš°λŠ” μ‚¬μš©μžμ™€ κ΄€λ ¨λœ μ—”ν‹°ν‹°μ—μ„œ λ‹€λ₯Έ 엔티티와 μ§μ ‘μ°Έμ‘°ν•˜λ©΄ λ°”λ‘œ 데이터λ₯Ό κ°€μ Έμ™€μ„œ μ‚¬μš©ν•  수 μžˆλŠ” μž₯점 λŒ€λΉ„ λ‹€λ₯Έ 엔티티와 결합도가 λ†’μ•„μ§€λŠ” 문제, N+1이 생길 μœ„ν—˜μ„ 항상 μ‹ κ²½μ¨μ•Όν•˜λŠ” λ¬Έμ œκ°€ 더 크닀고 μƒκ°ν•΄μ„œ κ°„μ ‘μ°Έμ‘°ν•˜κ±°λ‚˜, μƒλŒ€λ°© μ—”ν‹°ν‹° μͺ½μ—μ„œ Memberμͺ½μœΌλ‘œ 단방ν–₯ 연관관계λ₯Ό 맺도둝 ν•˜κΈ°λ₯Ό μ„ ν˜Έν•˜λŠ” νŽΈμž…λ‹ˆλ‹€ πŸ˜…


@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = "up_station_id")
Expand All @@ -25,8 +24,8 @@ public class Favorite {

protected Favorite() {}

public Favorite(Member member, Station source, Station target) {
this.member = member;
public Favorite(Long memberId, Station source, Station target) {
this.member = memberId;
this.source = source;
this.target = target;
}
Expand All @@ -35,7 +34,7 @@ public Long getId() {
return id;
}

public Member getMember() {
public Long getMember() {
return member;
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/nextstep/member/domain/FavoriteRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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

import java.util.List;

public interface FavoriteRepository extends JpaRepository<Favorite, Long> {


List<Favorite> findByMember(Long member);
}
15 changes: 0 additions & 15 deletions src/main/java/nextstep/member/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class Member extends BaseEntity {
private String password;
private Integer age;

@Embedded
private Favorites favorites = new Favorites();

protected Member() {

}
Expand Down Expand Up @@ -48,16 +45,4 @@ public void update(Member member) {
this.password = member.password;
this.age = member.age;
}

public List<Favorite> getFavorites() {
return favorites.getFavorites();
}

public void addFavorite(Favorite favorite) {
this.favorites.add(favorite);
}

public void removeFavorite(Favorite favorite) {
this.favorites.remove(favorite);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import nextstep.auth.authentication.AuthenticationToken;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -12,8 +13,7 @@

import static nextstep.subway.acceptance.FavoritesSteps.*;
import static nextstep.subway.acceptance.LineSteps.μ§€ν•˜μ² _λ…Έμ„ _생성_μš”μ²­;
import static nextstep.subway.acceptance.MemberSteps.둜그인_λ˜μ–΄_있음;
import static nextstep.subway.acceptance.MemberSteps.νšŒμ›_생성_μš”μ²­;
import static nextstep.subway.acceptance.MemberSteps.*;
import static nextstep.subway.acceptance.StationSteps.μ§€ν•˜μ² μ—­_생성_μš”μ²­;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -105,7 +105,7 @@ void removeFavorites() {
*/
@DisplayName("λΉ„λ‘œκ·ΈμΈ 즐겨찾기 κΈ°λŠ₯을 μ‚¬μš©ν•  수 μ—†λ‹€.")
@Test
void isUnauthorized() {
void isUnauthorizedByLogin() {
// given
즐겨찾기_생성_μš”μ²­(accessToken, 강남역, μ–‘μž¬μ—­);
accessToken = "";
Expand Down