Skip to content

Comments

[refactor] 아카이브를 저장하는 API의 내부 로직을 변경한다.#269

Open
gudonghee2000 wants to merge 6 commits intodevfrom
refactor/256-archive-save
Open

[refactor] 아카이브를 저장하는 API의 내부 로직을 변경한다.#269
gudonghee2000 wants to merge 6 commits intodevfrom
refactor/256-archive-save

Conversation

@gudonghee2000
Copy link
Contributor

  • 🔀 PR 제목의 형식을 잘 작성했나요? e.g. [feat] PR을 등록한다.
  • 💯 테스트는 잘 통과했나요?
  • 🏗️ 빌드는 성공했나요?
  • 🧹 불필요한 코드는 제거했나요?
  • 💭 이슈는 등록했나요?
  • 🏷️ 라벨은 등록했나요?

작업 내용

  • 아카이브 Entity 객체 생성을 위한 Mapper 빈 객체 생성
  • ArchiveArchiveImages, UserVisitPlace 엔티티객체에 대한 다대일 양방향 연관관계 생성

주의사항

Closes #256

@gudonghee2000 gudonghee2000 added the refactor Refactor code label Feb 21, 2023
@gudonghee2000 gudonghee2000 self-assigned this Feb 21, 2023
@ApiOperation(value = "신규 아카이브 등록")
public ArchiveInfo saveArchive(
@ApiParam(value = "등록할 아카이브 데이터", required = true) @RequestBody ArchiveRequest request) {
@ApiParam(value = "등록할 아카이브 데이터", required = true) @RequestBody ArchiveCreateRequest request) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Create 붙여준거 조아요~

private ItemEntity item;

@OneToMany(mappedBy = "archiveEntity", cascade = CascadeType.PERSIST)
private List<ArchiveImageEntity> archiveImages;
Copy link
Collaborator

Choose a reason for hiding this comment

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

흠 PERSIST가 맞을까요? 이걸로 하신 이유가 있을까요?

Copy link
Contributor Author

@gudonghee2000 gudonghee2000 Feb 22, 2023

Choose a reason for hiding this comment

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

해당 부분은 다대일 양방향 매핑을 사용하면서 발생한 영속성전이 부분이예요.
Archive와 ArchiveImage 중 연관관계 주인이 ArchiveImage가 되면서 양방향 매핑을 위해 @OneToManymappedBy 옵션이 붙게 되었어요

하지만 mappedBy는 읽기 전용이기 때문에 쓰기가 불가능해요.
그래서 CaseCade.Persist 옵션을 주어 Archive를 DB에 영속시킬때 영속성 전이를 통해 ArchiveImages를 같이 영속화 시킴을 명시해주어야 해요

만약, 위와 같이 구성하지 않게되면
ArchiveRepository.save(), ArchiveImageRepository.save() 로직이 두번 발생하게되는데요.
ArchiveImage는 Archive에 생명주기가 종속적인 Entity이기 때문에 영속성 전이를 사용했어요!

private ArchiveEntity archiveEntity;

public void setArchiveEntity(ArchiveEntity archiveEntity) {
public void initArchiveEntity(ArchiveEntity archiveEntity) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 이름이 set이 아닌 init으로 바꾼이유가 뭘까용?

Copy link
Contributor Author

@gudonghee2000 gudonghee2000 Feb 22, 2023

Choose a reason for hiding this comment

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

개인적으로 set이라는 네이밍을 좋아하지 않아서 변경해보았어요~

Copy link
Collaborator

Choose a reason for hiding this comment

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

장단점이 뭘까요? 왜 set이라는 네이밍을 좋아하지 않으신가용?

return ArchiveEntity.builder()
.comment(this.getComment())
.starRating(this.getStarRating())
.isVisibleAtItem(this.isVisibleAtItem())
Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 이름이 왜 isVisibleAtItem이에요? 무슨 뜻인가용?

Copy link
Contributor Author

@gudonghee2000 gudonghee2000 Feb 22, 2023

Choose a reason for hiding this comment

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

해당 필드명이 isVisibleAtItem이예요.
기존에 설정되어 있던 네이밍인데요. 아카이브 공개설정에 대한 옵션인것 같아요~
지금 보면 네이밍에 item이 붙어있어 헷갈리기도 하네요!

Copy link
Collaborator

Choose a reason for hiding this comment

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

AtItem부터는 빼도 되지 않을까 싶긴하네요~


return archiveAggregateConverter.convertArchiveInfo(archiveEntity, userResponse, archiveImageEntities,
userVisitPlaceEntities);
private void validateNotDuplicateArchive(Long userId, Long itemId) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 validate은 왜 domain에 없고 여기에 있나용?

Copy link
Contributor Author

@gudonghee2000 gudonghee2000 Feb 22, 2023

Choose a reason for hiding this comment

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

해당 validate는 Repository 접근이 필요한 검증인데요.
이 부분을 domain에 옮긴다면 함수형 인터페이스나 Repository 빈을 파라미터로 domain 객체에게 전달해주어야 하는데

이미 Service가 Repository를 알고있기때문에 검증을 위해 domain에게 Repository 빈을 넣어주는것이 불필요하다 생각되어 Service에서 처리하도록 하였어요!

Copy link
Collaborator

Choose a reason for hiding this comment

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

흠 제가 도메인을 서비스가 아닌 이런 도메인 객체로 크게 가져가본 적이 없어서 질문이 이상할 수 도 있어요~
도메인에서 validate할 수 있는 것, 없는 것이 나누어지면 굳이 도메인 객체로서 가져갈 필요가 있나요?

import com.kilometer.exception.KilometerErrorCode;
import com.kilometer.exception.KilometerException;

public class UserNotFoundException extends KilometerException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

요거,,,, 만드러주세요 익셉션 핸들러

Copy link
Contributor Author

@gudonghee2000 gudonghee2000 Feb 22, 2023

Choose a reason for hiding this comment

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

이부분 만들어서 같이 pr에 반영하겠습니다🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Refactor code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

아카이브를 저장하는 API의 내부 로직을 변경한다.

2 participants