-
Notifications
You must be signed in to change notification settings - Fork 0
[refactor] 아카이브를 저장하는 API의 내부 로직을 변경한다. #269
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
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.archiveImage.ArchiveImageEntity; | ||
| import com.kilometer.domain.archive.request.ArchiveRequest; | ||
| 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.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,22 @@ public class ArchiveEntity { | |
| @JoinColumn(name = "item") | ||
| private ItemEntity item; | ||
|
|
||
| @OneToMany(mappedBy = "archiveEntity", cascade = CascadeType.PERSIST) | ||
| private List<ArchiveImageEntity> archiveImages; | ||
|
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. 흠 PERSIST가 맞을까요? 이걸로 하신 이유가 있을까요?
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는 읽기 전용이기 때문에 쓰기가 불가능해요. 만약, 위와 같이 구성하지 않게되면 |
||
|
|
||
| @OneToMany(mappedBy = "archiveEntity", cascade = CascadeType.PERSIST) | ||
| private List<UserVisitPlaceEntity> userVisitPlaces; | ||
|
|
||
| public void addArchiveImages(final List<ArchiveImageEntity> archiveImages) { | ||
| this.archiveImages = archiveImages; | ||
| archiveImages.forEach(archiveImage -> archiveImage.initArchiveEntity(this)); | ||
| } | ||
|
|
||
| public void addUserVisitPlaces(final List<UserVisitPlaceEntity> userVisitPlaces) { | ||
| this.userVisitPlaces = userVisitPlaces; | ||
| userVisitPlaces.forEach(userVisitPlace -> userVisitPlace.initArchiveEntity(this)); | ||
| } | ||
|
|
||
| public void setUser(User user) { | ||
| this.user = user; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,7 +47,7 @@ public class ArchiveImageEntity { | |
| @JoinColumn(name = "archive") | ||
| private ArchiveEntity archiveEntity; | ||
|
|
||
| public void setArchiveEntity(ArchiveEntity archiveEntity) { | ||
| public void initArchiveEntity(ArchiveEntity archiveEntity) { | ||
|
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. 이거 이름이 set이 아닌 init으로 바꾼이유가 뭘까용?
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. 개인적으로 set이라는 네이밍을 좋아하지 않아서 변경해보았어요~
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. 장단점이 뭘까요? 왜 set이라는 네이밍을 좋아하지 않으신가용? |
||
| this.archiveEntity = archiveEntity; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,16 @@ | ||
| package com.kilometer.domain.archive.domain; | ||
|
|
||
| import com.kilometer.domain.archive.ArchiveEntity; | ||
| import com.kilometer.domain.archive.archiveImage.ArchiveImageEntity; | ||
| import com.kilometer.domain.archive.domain.archiveFilter.ArchiveFilter; | ||
| import com.kilometer.domain.archive.domain.userVisitPlace.UserVisitPlace; | ||
| import com.kilometer.domain.archive.exception.ArchiveValidationException; | ||
| import com.kilometer.domain.archive.userVisitPlace.UserVisitPlaceEntity; | ||
| import com.kilometer.domain.item.ItemEntity; | ||
| import com.kilometer.domain.user.User; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
|
|
@@ -51,4 +57,26 @@ private static void validateStarRating(final int starRating) { | |
| throw new ArchiveValidationException("별점은 0~5 사이의 양수이어야 합니다."); | ||
| } | ||
| } | ||
|
|
||
| public ArchiveEntity toEntity(final User user, final ItemEntity itemEntity) { | ||
| return ArchiveEntity.builder() | ||
| .comment(this.getComment()) | ||
| .starRating(this.getStarRating()) | ||
| .isVisibleAtItem(this.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이에요? 무슨 뜻인가용?
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. 해당 필드명이 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. AtItem부터는 빼도 되지 않을까 싶긴하네요~ |
||
| .user(user) | ||
| .item(itemEntity) | ||
| .build(); | ||
| } | ||
|
|
||
| public List<ArchiveImageEntity> createArchiveImageEntities() { | ||
| return this.archiveImages.stream() | ||
| .map(ArchiveImage::toEntity) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<UserVisitPlaceEntity> createUserVisitPlaceEntities() { | ||
| return this.userVisitPlaces.stream() | ||
| .map(UserVisitPlace::toEntity) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.kilometer.domain.archive.request; | ||
|
|
||
| import com.kilometer.domain.archive.domain.Archive; | ||
| import com.kilometer.domain.archive.domain.ArchiveImage; | ||
| import com.kilometer.domain.archive.domain.userVisitPlace.UserVisitPlace; | ||
| import com.kilometer.domain.archive.dto.PlaceInfo; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| @AllArgsConstructor | ||
| public class ArchiveCreateRequest { | ||
|
|
||
| private Long itemId; | ||
| private String comment; | ||
| private int starRating; | ||
| private boolean isVisibleAtItem; | ||
| private List<String> photoUrls; | ||
| private List<PlaceInfo> placeInfos; | ||
|
|
||
| public Archive toDomain() { | ||
| return Archive.createArchive( | ||
| this.comment, this.starRating, this.isVisibleAtItem, toArchiveImages(), toUserVisitPlace()); | ||
| } | ||
|
|
||
| public List<ArchiveImage> toArchiveImages() { | ||
| return this.photoUrls.stream() | ||
| .map(ArchiveImage::createArchiveImage) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| public List<UserVisitPlace> toUserVisitPlace() { | ||
| return this.placeInfos.stream() | ||
| .map(PlaceInfo::toDomain) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package com.kilometer.domain.archive.service; | ||
|
|
||
| import com.kilometer.domain.archive.ArchiveEntity; | ||
| import com.kilometer.domain.archive.domain.Archive; | ||
| import com.kilometer.domain.archive.request.ArchiveCreateRequest; | ||
| import com.kilometer.domain.item.ItemEntity; | ||
| import com.kilometer.domain.item.ItemRepository; | ||
| import com.kilometer.domain.item.exception.ItemNotFoundException; | ||
| import com.kilometer.domain.user.User; | ||
| import com.kilometer.domain.user.UserRepository; | ||
| import com.kilometer.domain.user.exception.UserNotFoundException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Service | ||
| @Transactional(readOnly = true) | ||
| @RequiredArgsConstructor | ||
| public class ArchiveEntityMapper { | ||
|
|
||
| private final UserRepository userRepository; | ||
| private final ItemRepository itemRepository; | ||
|
|
||
| public ArchiveEntity mapToArchiveEntity(final Long userId, final ArchiveCreateRequest request) { | ||
| Archive archive = request.toDomain(); | ||
|
|
||
| ArchiveEntity archiveEntity = createArchiveEntity(userId, request.getItemId(), archive); | ||
| archiveEntity.addArchiveImages(archive.createArchiveImageEntities()); | ||
| archiveEntity.addUserVisitPlaces(archive.createUserVisitPlaceEntities()); | ||
| return archiveEntity; | ||
| } | ||
|
|
||
| private ArchiveEntity createArchiveEntity(final Long userId, final Long itemId, final Archive archive) { | ||
| User user = getUser(userId); | ||
| ItemEntity itemEntity = getItem(itemId); | ||
| return archive.toEntity(user, itemEntity); | ||
| } | ||
|
|
||
| private User getUser(final Long userId) { | ||
| return userRepository.findById(userId) | ||
| .orElseThrow(() -> new UserNotFoundException("저장되지 않은 사용자 입니다.")); | ||
| } | ||
|
|
||
| private ItemEntity getItem(final Long itemId) { | ||
| ItemEntity itemEntity = itemRepository.findById(itemId) | ||
| .orElseThrow(() -> new ItemNotFoundException("저장되지 않은 아이템 입니다.")); | ||
| itemEntity.validateExposureTypeIsOn(); | ||
| return itemEntity; | ||
| } | ||
| } |
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.
Create 붙여준거 조아요~