Skip to content

Commit

Permalink
fix: AgendaPhotoService 클래스에 nullifyPhotoInAgendaPhotoList(List<Long>…
Browse files Browse the repository at this point in the history
… photoIdList) 구현

CQRS 패턴에 따라 PhotoService에서 PhotoQueryService를 분화시켜 순환 참조를 방지하였다.
  • Loading branch information
bflykky committed Aug 20, 2024
1 parent 537fe9d commit 97a61fd
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public void delete() {
}
super.delete();
}

public void nullifyPhoto() {
this.photo = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.umc.naoman.domain.agenda.entity.AgendaPhoto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -10,4 +11,5 @@
public interface AgendaPhotoRepository extends JpaRepository<AgendaPhoto, Long> {
List<AgendaPhoto> findByAgendaId(Long agendaId);
List<AgendaPhoto> findByPhotoId(Long photoId);
List<AgendaPhoto> findByPhotoIdIn(List<Long> photoIdList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface AgendaPhotoService {
AgendaPhoto findAgendaPhoto(Long agendaPhotoId);
List<AgendaPhoto> findAgendaPhotoList(Long agendaId);
List<AgendaPhoto> findAgendaPhotoListByPhotoId(Long photoId);
void nullifyPhotoInAgendaPhotoList(List<Long> photoIdList);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.umc.naoman.domain.agenda.entity.AgendaPhoto;
import com.umc.naoman.domain.agenda.repository.AgendaPhotoRepository;
import com.umc.naoman.domain.photo.entity.Photo;
import com.umc.naoman.domain.photo.service.PhotoService;
import com.umc.naoman.domain.photo.service.PhotoQueryService;
import com.umc.naoman.global.error.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -19,14 +19,14 @@
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AgendaPhotoServiceImpl implements AgendaPhotoService {
private final PhotoService photoService;
private final PhotoQueryService photoQueryService;
private final AgendaPhotoRepository agendaPhotoRepository;

@Override
@Transactional
public void saveAgendaPhotoList(Agenda agenda, List<Long> photos) {
for (Long photoId : photos) {
Photo photo = photoService.findPhoto(photoId);
Photo photo = photoQueryService.findPhoto(photoId);
agendaPhotoRepository.save(AgendaPhotoConverter.toEntity(agenda, photo));
}
}
Expand All @@ -46,4 +46,12 @@ public List<AgendaPhoto> findAgendaPhotoList(Long agendaId) {
public List<AgendaPhoto> findAgendaPhotoListByPhotoId(Long photoId) {
return agendaPhotoRepository.findByPhotoId(photoId);
}

@Override
@Transactional
public void nullifyPhotoInAgendaPhotoList(List<Long> photoIdList) {
List<AgendaPhoto> agendaPhotoList = agendaPhotoRepository.findByPhotoIdIn(photoIdList);
agendaPhotoList
.forEach(AgendaPhoto::nullifyPhoto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AgendaServiceImpl implements AgendaService {
private final AgendaRepository agendaRepository;
private final ShareGroupService shareGroupService;
private final AgendaPhotoService agendaPhotoService;
private final AgendaRepository agendaRepository;
private final AgendaConverter agendaConverter;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.umc.naoman.domain.member.entity.SocialType;
import com.umc.naoman.domain.member.repository.MemberRepository;
import com.umc.naoman.domain.member.service.redis.RefreshTokenService;
import com.umc.naoman.domain.photo.service.PhotoQueryService;
import com.umc.naoman.domain.photo.service.PhotoService;
import com.umc.naoman.domain.shareGroup.entity.Profile;
import com.umc.naoman.domain.shareGroup.entity.Role;
Expand All @@ -35,6 +36,7 @@
public class MemberServiceImpl implements MemberService {
private final RefreshTokenService refreshTokenService;
private final PhotoService photoService;
private final PhotoQueryService photoQueryService;
private final MemberRepository memberRepository;
private final MemberConverter memberConverter;
private final ShareGroupService shareGroupService;
Expand Down Expand Up @@ -97,7 +99,7 @@ public CheckMemberRegistration checkRegistration(LoginRequest request) {

@Override
public HasSamplePhoto hasSamplePhoto(Member member) {
boolean hasSamplePhoto = photoService.hasSamplePhoto(member);
boolean hasSamplePhoto = photoQueryService.hasSamplePhoto(member);
return memberConverter.toHasSamplePhoto(hasSamplePhoto);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.umc.naoman.domain.photo.service;

import com.umc.naoman.domain.member.entity.Member;
import com.umc.naoman.domain.photo.entity.Photo;

public interface PhotoQueryService {
Photo findPhoto(Long photoId);
boolean hasSamplePhoto(Member member);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.umc.naoman.domain.photo.service;

import com.umc.naoman.domain.member.entity.Member;
import com.umc.naoman.domain.photo.entity.Photo;
import com.umc.naoman.domain.photo.repository.PhotoRepository;
import com.umc.naoman.domain.photo.repository.SamplePhotoRepository;
import com.umc.naoman.global.error.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.umc.naoman.global.error.code.S3ErrorCode.PHOTO_NOT_FOUND;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class PhotoQueryServiceImpl implements PhotoQueryService {
private final PhotoRepository photoRepository;
private final SamplePhotoRepository samplePhotoRepository;

@Override
public Photo findPhoto(Long photoId) {
return photoRepository.findById(photoId)
.orElseThrow(() -> new BusinessException(PHOTO_NOT_FOUND));
}

@Override
public boolean hasSamplePhoto(Member member) {
return samplePhotoRepository.existsByMemberId(member.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface PhotoService {

void deleteSamplePhotoList(Member member);

Photo findPhoto(Long photoId);
// Photo findPhoto(Long photoId);

boolean hasSamplePhoto(Member member);
// boolean hasSamplePhoto(Member member);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.amazonaws.services.s3.Headers;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.umc.naoman.domain.agenda.service.AgendaPhotoService;
import com.umc.naoman.domain.member.entity.Member;
import com.umc.naoman.domain.photo.converter.PhotoConverter;
import com.umc.naoman.domain.photo.converter.SamplePhotoConverter;
Expand Down Expand Up @@ -51,6 +52,7 @@
@RequiredArgsConstructor
public class PhotoServiceImpl implements PhotoService {
private final ShareGroupService shareGroupService;
private final AgendaPhotoService agendaPhotoService;
private final FaceDetectionService faceDetectionService;
private final PhotoRepository photoRepository;
private final SamplePhotoRepository samplePhotoRepository;
Expand Down Expand Up @@ -346,8 +348,10 @@ public void deletePhotoListByShareGroupId(Long shareGroupId) {
// Elasticsearch 사진 데이터 삭제
List<Long> photoIdList = photoEsClientRepository.deletePhotoEsByShareGroupId(shareGroupId);

List<Photo> photoList = photoRepository.findByIdIn(photoIdList);
// 삭제하려는 사진들 중 안건 후보로 등록된 것들에 대하여, 해당 사진들의 참조를 삭제
agendaPhotoService.nullifyPhotoInAgendaPhotoList(photoIdList);

List<Photo> photoList = photoRepository.findByIdIn(photoIdList);
// S3 버킷 사진 데이터 삭제
for (Photo photo : photoList) {
deletePhoto(photo.getName());
Expand Down Expand Up @@ -380,6 +384,7 @@ private void validateShareGroupAndProfile(Long shareGroupId, Member member) {
shareGroupService.findProfile(shareGroupId, member.getId());
}

/*
@Override
public Photo findPhoto(Long photoId) {
return photoRepository.findById(photoId)
Expand All @@ -390,4 +395,5 @@ public Photo findPhoto(Long photoId) {
public boolean hasSamplePhoto(Member member) {
return samplePhotoRepository.existsByMemberId(member.getId());
}
*/
}

0 comments on commit 97a61fd

Please sign in to comment.