-
Notifications
You must be signed in to change notification settings - Fork 0
[refactor] 아카이브를 수정하는 API 기능의 내부구현을 변경한다. #271
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
base: dev
Are you sure you want to change the base?
Changes from all commits
26b1e99
183a305
575089d
f95ca49
e42c728
de68d04
fcb3a72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| package com.kilometer.domain.archive; | ||
|
|
||
| import com.kilometer.domain.archive.request.ArchiveRequest; | ||
| import com.kilometer.domain.archive.archiveImage.ArchiveImageEntity; | ||
| import com.kilometer.domain.archive.userVisitPlace.UserVisitPlaceEntity; | ||
| import com.kilometer.domain.item.ItemEntity; | ||
| import com.kilometer.domain.user.User; | ||
| import java.time.LocalDateTime; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import javax.persistence.CascadeType; | ||
| import javax.persistence.Column; | ||
| import javax.persistence.Entity; | ||
| import javax.persistence.FetchType; | ||
|
|
@@ -12,6 +16,7 @@ | |
| import javax.persistence.Id; | ||
| import javax.persistence.JoinColumn; | ||
| import javax.persistence.ManyToOne; | ||
| import javax.persistence.OneToMany; | ||
| import javax.persistence.Table; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
|
|
@@ -65,6 +70,35 @@ public class ArchiveEntity { | |
| @JoinColumn(name = "item") | ||
| private ItemEntity item; | ||
|
|
||
| @OneToMany(mappedBy = "archiveEntity", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 혹시 cascade 설정을 이렇게 해주신 이유가 있나요?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분은 다대일 양방향 매핑을 사용하면서 발생한 영속성전이 부분이예요. 하지만 mappedBy는 읽기 전용이기 때문에 쓰기가 불가능해요. 만약, 위와 같이 구성하지 않게되면 |
||
| fetch = FetchType.LAZY, orphanRemoval = true) | ||
| private final List<ArchiveImageEntity> archiveImages = new ArrayList<>(); | ||
|
|
||
| @OneToMany(mappedBy = "archiveEntity", cascade = {CascadeType.PERSIST, CascadeType.MERGE}, | ||
| fetch = FetchType.LAZY, orphanRemoval = true) | ||
| private final List<UserVisitPlaceEntity> userVisitPlaces = new ArrayList<>(); | ||
|
|
||
| public void initArchiveImages(final List<ArchiveImageEntity> archiveImages) { | ||
| this.archiveImages.clear(); | ||
| this.archiveImages.addAll(archiveImages); | ||
| archiveImages.forEach(archiveImage -> archiveImage.initArchiveEntity(this)); | ||
| } | ||
|
|
||
| public void initUserVisitPlaces(final List<UserVisitPlaceEntity> userVisitPlaces) { | ||
| this.userVisitPlaces.clear(); | ||
| this.userVisitPlaces.addAll(userVisitPlaces); | ||
| userVisitPlaces.forEach(userVisitPlace -> userVisitPlace.initArchiveEntity(this)); | ||
| } | ||
|
|
||
| public void update(final String comment, final int starRating, final boolean isVisibleAtItem, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isVisibleAtItem이라는 작명은 어떤의미일까요? |
||
| final List<ArchiveImageEntity> archiveImages, final List<UserVisitPlaceEntity> userVisitPlaces) { | ||
| this.comment = comment; | ||
| this.starRating = starRating; | ||
| this.isVisibleAtItem = isVisibleAtItem; | ||
| initArchiveImages(archiveImages); | ||
| initUserVisitPlaces(userVisitPlaces); | ||
| } | ||
|
|
||
| public void setUser(User user) { | ||
| this.user = user; | ||
| } | ||
|
|
@@ -73,12 +107,6 @@ public void setItem(ItemEntity item) { | |
| this.item = item; | ||
| } | ||
|
|
||
| public void update(ArchiveRequest request) { | ||
| this.comment = request.getComment(); | ||
| this.isVisibleAtItem = request.isVisibleAtItem(); | ||
| this.starRating = request.getStarRating(); | ||
| } | ||
|
|
||
| public ArchiveEntity plusLikeCount() { | ||
| this.likeCount++; | ||
| return this; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,19 @@ | ||
| package com.kilometer.domain.archive; | ||
|
|
||
| import com.kilometer.domain.archive.exception.ArchiveNotFoundException; | ||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| public interface ArchiveRepository extends JpaRepository<ArchiveEntity, Long>, ArchiveRepositoryCustom { | ||
|
|
||
| Optional<ArchiveEntity> findByItemIdAndUserId(Long itemId, Long userId); | ||
|
|
||
| Optional<ArchiveEntity> findByIdAndUserId(final Long id, final Long userId); | ||
|
|
||
| boolean existsByItemIdAndUserId(Long itemId, Long userId); | ||
|
|
||
| default ArchiveEntity getByIdAndUserId(final Long id, final Long userId) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이건 왜 default인건가요??
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분은 어제 논쟁과 유사한 로직인것 같아요!(사실 아직 미완성 PR이여서 수정할 예정입니다ㅎㅎ) interface는 메서드 선언부만 가질수 있지만 JAVA8(JAVA5일수도 있어요)부터 default 메서드가 되입되면서 interface도 메서드 구현체를 만들수 있게되었는데요! 보통은 하지만 개인적인 취향으로 외람되게는 JAVA8의 interface default 메서드 등장배경은 기존 서비스 회사들이 사용하던 JAVA의 Interface SPEC이 바뀌면서 코드를 수정해야하는 불편함들을 해결해주기 위해 interface에 default 메서드로 구현체를 만들었다고 해요 그래서 |
||
| return this.findByIdAndUserId(id, userId) | ||
| .orElseThrow(() -> new ArchiveNotFoundException("해당 회원에게 일치하는 아카이브가 없습니다.")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
꼭 다붙여야 하나용?