Skip to content

Commit

Permalink
refactor: 중간 테이블 엔티티 수정 #56 (#57)
Browse files Browse the repository at this point in the history
* refactor: 중간 테이블명 TravelMember로 변경

* refactor: 중간 테이블 OneToMany 필드 추가

* refactor: Member OneToMany 제거

* refactor: OneToMany List 초기화

* refactor: 연관관계 편의 메서드 사용

* chore: ddl 전략 임시 변경

* chore: ddl 전략 변경
  • Loading branch information
linirini authored and devhoya97 committed Jul 24, 2024
1 parent 495f98e commit c2bfc63
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
4 changes: 4 additions & 0 deletions backend/src/main/java/com/staccato/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.staccato.member.domain;

import java.util.List;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

import org.hibernate.annotations.SQLDelete;

import com.staccato.config.domain.BaseEntity;
import com.staccato.travel.domain.TravelMember;

import lombok.AccessLevel;
import lombok.Builder;
Expand Down
9 changes: 9 additions & 0 deletions backend/src/main/java/com/staccato/travel/domain/Travel.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.staccato.travel.domain;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;

import org.hibernate.annotations.SQLDelete;

Expand Down Expand Up @@ -37,6 +40,8 @@ public class Travel extends BaseEntity {
private LocalDate startAt;
@Column(nullable = false)
private LocalDate endAt;
@OneToMany(mappedBy = "travel")
private List<TravelMember> travelMembers = new ArrayList<>();

@Builder
public Travel(String thumbnailUrl, @NonNull String title, String description, @NonNull LocalDate startAt, @NonNull LocalDate endAt) {
Expand All @@ -53,4 +58,8 @@ private void validateDate(LocalDate startAt, LocalDate endAt) {
throw new StaccatoException("끝 날짜가 시작 날짜보다 앞설 수 없어요.");
}
}

public void addTravelMember(TravelMember travelMember) {
travelMembers.add(travelMember);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE mate SET is_deleted = true WHERE id = ?")
public class Mate extends BaseEntity {
public class TravelMember extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -35,8 +35,9 @@ public class Mate extends BaseEntity {
private Travel travel;

@Builder
public Mate(@NonNull Member member, @NonNull Travel travel) {
public TravelMember(@NonNull Member member, @NonNull Travel travel) {
this.member = member;
this.travel = travel;
travel.addTravelMember(this);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.staccato.travel.repository;

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

import com.staccato.travel.domain.TravelMember;

public interface TravelMemberRepostiory extends JpaRepository<TravelMember, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

import com.staccato.member.domain.Member;
import com.staccato.member.repository.MemberRepository;
import com.staccato.travel.domain.Mate;
import com.staccato.travel.domain.Travel;
import com.staccato.travel.repository.MateRepository;
import com.staccato.travel.domain.TravelMember;
import com.staccato.travel.repository.TravelMemberRepostiory;
import com.staccato.travel.repository.TravelRepository;
import com.staccato.travel.service.dto.request.TravelRequest;
import com.staccato.travel.service.dto.response.TravelResponse;
Expand All @@ -19,24 +19,23 @@
@Transactional(readOnly = true)
public class TravelService {
private final TravelRepository travelRepository;
private final MateRepository mateRepository;
private final TravelMemberRepostiory travelMemberRepostiory;
private final MemberRepository memberRepository;

@Transactional
public TravelResponse createTravel(TravelRequest travelRequest, Long memberId) {
Travel travel = travelRequest.toTravel();
Travel savedTravel = travelRepository.save(travel);
saveMate(memberId, savedTravel);
return new TravelResponse(savedTravel);
Travel travel = travelRepository.save(travelRequest.toTravel());
saveTravelMember(memberId, travel);
return new TravelResponse(travel);
}

private void saveMate(Long memberId, Travel travel) {
private TravelMember saveTravelMember(Long memberId, Travel travel) {
Member member = getMemberById(memberId);
Mate mate = Mate.builder()
TravelMember mate = TravelMember.builder()
.travel(travel)
.member(member)
.build();
mateRepository.save(mate);
return travelMemberRepostiory.save(mate);
}

private Member getMemberById(long memberId) {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ spring:
hibernate:
format_sql: true
hibernate:
ddl-auto: none
ddl-auto: update
database-platform: org.hibernate.dialect.MySQL8Dialect
defer-datasource-initialization: true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.staccato.ServiceSliceTest;
import com.staccato.member.domain.Member;
import com.staccato.member.repository.MemberRepository;
import com.staccato.travel.domain.Mate;
import com.staccato.travel.repository.MateRepository;
import com.staccato.travel.domain.TravelMember;
import com.staccato.travel.repository.TravelMemberRepostiory;
import com.staccato.travel.service.dto.request.TravelRequest;
import com.staccato.travel.service.dto.response.TravelResponse;

Expand All @@ -23,7 +23,7 @@ class TravelServiceTest extends ServiceSliceTest {
@Autowired
private MemberRepository memberRepository;
@Autowired
private MateRepository mateRepository;
private TravelMemberRepostiory travelMemberRepostiory;

@DisplayName("여행 상세 정보를 기반으로, 여행 상세를 생성하고 작성자를 저장한다.")
@Test
Expand All @@ -40,12 +40,12 @@ void createTravel() {

// when
TravelResponse travel = travelService.createTravel(travelRequest, member.getId());
Mate mate = mateRepository.findAll().get(0);
TravelMember travelMember = travelMemberRepostiory.findAll().get(0);

// then
assertAll(
() -> Assertions.assertThat(mate.getMember().getId()).isEqualTo(member.getId()),
() -> Assertions.assertThat(mate.getTravel().getId()).isEqualTo(travel.travelId())
() -> Assertions.assertThat(travelMember.getMember().getId()).isEqualTo(member.getId()),
() -> Assertions.assertThat(travelMember.getTravel().getId()).isEqualTo(travel.travelId())
);
}
}

0 comments on commit c2bfc63

Please sign in to comment.