Skip to content

Commit

Permalink
[refac] recycle 리팩토링 및 s3 object 삭제 추가 (#73)
Browse files Browse the repository at this point in the history
* [refac] recycle 복구, 가져오기 validation 정리 및 함수 네이밍 변경 #71

* [refac] recycle 삭제 validation 코드 정리, report도 삭제 #71

* [feat] s3 객체 삭제 로직 추가 #72

* [chore] spotless 적용
  • Loading branch information
wjdtkdgns authored Jul 31, 2023
1 parent c0fc12e commit 7da69d3
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import allchive.server.api.config.security.SecurityUtil;
import allchive.server.api.image.model.dto.response.ImageUrlResponse;
import allchive.server.infrastructure.s3.PresignedType;
import allchive.server.infrastructure.s3.S3PresignedUrlService;
import allchive.server.infrastructure.s3.service.S3PresignedUrlService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import allchive.server.domain.domains.archiving.validator.ArchivingValidator;
import allchive.server.domain.domains.content.adaptor.ContentAdaptor;
import allchive.server.domain.domains.content.domain.Content;
import allchive.server.domain.domains.content.domain.enums.ContentType;
import allchive.server.domain.domains.content.service.ContentDomainService;
import allchive.server.domain.domains.content.service.ContentTagGroupDomainService;
import allchive.server.domain.domains.content.validator.ContentValidator;
import allchive.server.domain.domains.recycle.service.RecycleDomainService;
import allchive.server.domain.domains.recycle.validator.RecycleValidator;
import allchive.server.domain.domains.report.service.ReportDomainService;
import allchive.server.domain.domains.user.service.ScrapDomainService;
import allchive.server.infrastructure.s3.service.S3DeleteObjectService;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,27 +34,49 @@ public class ClearDeletedObjectUseCase {
private final ArchivingDomainService archivingDomainService;
private final ContentDomainService contentDomainService;
private final RecycleDomainService recycleDomainService;
private final ReportDomainService reportDomainService;
private final S3DeleteObjectService s3DeleteObjectService;

// TODO: report 지우기
@Transactional
public void execute(ClearDeletedObjectRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
recycleValidator.validateExist(request.getArchivingIds(), request.getContentIds(), userId);
archivingValidator.verifyUserInIdList(userId, request.getArchivingIds());
contentValidator.verifyUserInIdList(userId, request.getContentIds());
validateExecution(userId, request);
List<Content> contents = contentAdaptor.findAllByArchivingIds(request.getArchivingIds());
List<Long> contentsId = getContentsId(contents, request);
scrapDomainService.deleteAllByArchivingIdIn(request.getArchivingIds());
contentTagGroupDomainService.deleteByContentIn(contents);
contentDomainService.deleteAllById(contentsId);
archivingDomainService.deleteAllById(request.getArchivingIds());
recycleDomainService.deleteAllByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
request.getArchivingIds(), request.getContentIds(), userId);
reportDomainService.deleteAllByArchivingIdInOrContentIdIn(
request.getArchivingIds(), request.getContentIds());
deleteS3Object(contents);
}

private void deleteS3Object(List<Content> contents) {
List<String> imageKeys =
contents.stream()
.filter(content -> content.getContentType().equals(ContentType.IMAGE))
.map(Content::getImageUrl)
.toList();
s3DeleteObjectService.deleteS3Object(imageKeys);
}

private List<Long> getContentsId(List<Content> contents, ClearDeletedObjectRequest request) {
List<Long> contentsId = contents.stream().map(Content::getId).toList();
if (!request.getContentIds().isEmpty()) {
if (contentsId.isEmpty()) {
contentsId = new ArrayList<>();
}
contentsId.addAll(request.getContentIds());
}
scrapDomainService.deleteAllByArchivingIdIn(request.getArchivingIds());
contentTagGroupDomainService.deleteByContentIn(contents);
contentDomainService.deleteAllById(contentsId);
archivingDomainService.deleteAllById(request.getArchivingIds());
recycleDomainService.deleteAllByUserIdAndArchivingIdOrUserIdAndContentId(
request.getArchivingIds(), request.getContentIds(), userId);
return contentsId;
}

private void validateExecution(Long userId, ClearDeletedObjectRequest request) {
recycleValidator.validateExist(request.getArchivingIds(), request.getContentIds(), userId);
archivingValidator.verifyUserInIdList(userId, request.getArchivingIds());
contentValidator.verifyUserInIdList(userId, request.getContentIds());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import allchive.server.domain.domains.archiving.service.ArchivingDomainService;
import allchive.server.domain.domains.content.adaptor.ContentAdaptor;
import allchive.server.domain.domains.content.domain.Content;
import allchive.server.domain.domains.content.domain.enums.ContentType;
import allchive.server.domain.domains.content.service.ContentDomainService;
import allchive.server.domain.domains.content.service.ContentTagGroupDomainService;
import allchive.server.domain.domains.recycle.adaptor.RecycleAdaptor;
import allchive.server.domain.domains.recycle.domain.Recycle;
import allchive.server.domain.domains.recycle.domain.enums.RecycleType;
import allchive.server.domain.domains.recycle.service.RecycleDomainService;
import allchive.server.domain.domains.report.service.ReportDomainService;
import allchive.server.domain.domains.user.service.ScrapDomainService;
import allchive.server.infrastructure.s3.service.S3DeleteObjectService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -31,21 +34,46 @@ public class ClearOldDeletedObjectUseCase {
private final ArchivingDomainService archivingDomainService;
private final ContentDomainService contentDomainService;
private final RecycleDomainService recycleDomainService;
private final ReportDomainService reportDomainService;
private final S3DeleteObjectService s3DeleteObjectService;

/** 삭제 후 30일 지난 항목 제거 스케쥴러 매일 02:30에 수행 */
// TODO: s3 asset 삭제
@Scheduled(cron = "0 30 2 * * *")
@Transactional
public void executeSchedule() {
log.info("scheduler on");
LocalDateTime deleteStandard = LocalDateTime.now().minusDays(30);
List<Recycle> recycles = recycleAdaptor.findAllByDeletedAtBefore(deleteStandard);
List<Long> archivingIds = getArchivingIds(recycles);
List<Content> contents = contentAdaptor.findAllByArchivingIds(archivingIds);
List<Long> contentIds = getContentsId(recycles, contents);
scrapDomainService.deleteAllByArchivingIdIn(archivingIds);
contentTagGroupDomainService.deleteByContentIn(contents);
contentDomainService.deleteAllById(contentIds);
archivingDomainService.deleteAllById(archivingIds);
recycleDomainService.deleteAll(recycles);
reportDomainService.deleteAllByArchivingIdInOrContentIdIn(archivingIds, contentIds);
deleteS3Object(contents);
log.info("scheduler off");
}

List<Long> archivingIds =
recycles.stream()
.filter(recycle -> recycle.getRecycleType().equals(RecycleType.ARCHIVING))
.map(Recycle::getArchivingId)
private void deleteS3Object(List<Content> contents) {
List<String> imageKeys =
contents.stream()
.filter(content -> content.getContentType().equals(ContentType.IMAGE))
.map(Content::getImageUrl)
.toList();
s3DeleteObjectService.deleteS3Object(imageKeys);
}

private List<Long> getArchivingIds(List<Recycle> recycles) {
return recycles.stream()
.filter(recycle -> recycle.getRecycleType().equals(RecycleType.ARCHIVING))
.map(Recycle::getArchivingId)
.toList();
}

private List<Long> getContentsId(List<Recycle> recycles, List<Content> contents) {
List<Long> contentIds =
recycles.stream()
.filter(recycle -> recycle.getRecycleType().equals(RecycleType.CONTENT))
Expand All @@ -55,16 +83,9 @@ public void executeSchedule() {
if (contentIds.isEmpty()) {
contentIds = new ArrayList<>();
}
List<Content> contents = contentAdaptor.findAllByArchivingIds(archivingIds);
for (Content content : contents) {
contentIds.add(content.getId());
}

scrapDomainService.deleteAllByArchivingIdIn(archivingIds);
contentTagGroupDomainService.deleteByContentIn(contents);
contentDomainService.deleteAllById(contentIds);
archivingDomainService.deleteAllById(archivingIds);
recycleDomainService.deleteAll(recycles);
log.info("scheduler off");
return contentIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,29 @@ public class GetDeletedObjectUseCase {
public DeletedObjectResponse execute() {
Long userId = SecurityUtil.getCurrentUserId();
List<Recycle> recycles = recycleAdaptor.findAllByUserId(userId);
List<Archiving> archivings = getArchivings(recycles);
List<Content> contents = getContents(recycles);
List<ContentTagGroup> contentTagGroups =
contentTagGroupAdaptor.queryContentTagGroupByContentIn(contents);
return recycleMapper.toDeletedObjectResponse(
archivings, userId, contents, contentTagGroups);
}

private List<Archiving> getArchivings(List<Recycle> recycles) {
List<Long> archivingIds =
recycles.stream()
.filter(recycle -> recycle.getRecycleType().equals(RecycleType.ARCHIVING))
.map(Recycle::getArchivingId)
.toList();
return archivingAdaptor.findAllByIdIn(archivingIds);
}

private List<Content> getContents(List<Recycle> recycles) {
List<Long> contentIds =
recycles.stream()
.filter(recycle -> recycle.getRecycleType().equals(RecycleType.CONTENT))
.map(Recycle::getContentId)
.toList();
List<Archiving> archivings = archivingAdaptor.findAllByIdIn(archivingIds);
List<Content> contents = contentAdaptor.findAllByIdIn(contentIds);
List<ContentTagGroup> contentTagGroups =
contentTagGroupAdaptor.queryContentTagGroupByContentIn(contents);
return recycleMapper.toDeletedObjectResponse(
archivings, userId, contents, contentTagGroups);
return contentAdaptor.findAllByIdIn(contentIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ public class RestoreDeletedObjectUseCase {
@Transactional
public void execute(RestoreDeletedObjectRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
validateExecution(request, userId);
archivingDomainService.restoreByIdIn(request.getArchivingIds());
contentDomainService.restoreByIdIn(request.getContentIds());
recycleDomainService.deleteAllByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
request.getArchivingIds(), request.getContentIds(), userId);
}

private void validateExecution(RestoreDeletedObjectRequest request, Long userId) {
recycleValidator.validateExist(request.getArchivingIds(), request.getContentIds(), userId);
archivingValidator.validateExistInIdList(request.getArchivingIds());
contentValidator.validateExistInIdList(request.getContentIds());
archivingDomainService.restoreInIdList(request.getArchivingIds());
contentDomainService.restoreInIdList(request.getContentIds());
recycleDomainService.deleteAllByUserIdAndArchivingIdOrUserIdAndContentId(
request.getArchivingIds(), request.getContentIds(), userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public enum GlobalErrorCode implements BaseErrorCode {
EXPIRED_TOKEN(UNAUTHORIZED, "AUTH_401_3", "만료된 엑세스 토큰입니다"),
EXPIRED_REFRESH_TOKEN(UNAUTHORIZED, "AUTH_403_1", "인증 시간이 만료되었습니다. 재 로그인 해주세요."),

/** s3 오류 */
S3_OBJECT_NOT_FOUND(BAD_REQUEST, "S3_500_1", "s3 객체가 존재하지 않습니다."),

/** Feign Client 오류 */
OTHER_SERVER_BAD_REQUEST(BAD_REQUEST, "FEIGN_400_1", "Other server bad request"),
OTHER_SERVER_UNAUTHORIZED(BAD_REQUEST, "FEIGN_400_2", "Other server unauthorized"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package allchive.server.core.error.exception;


import allchive.server.core.error.BaseErrorException;
import allchive.server.core.error.GlobalErrorCode;

public class S3ObjectNotFoundException extends BaseErrorException {

public static final BaseErrorException EXCEPTION = new S3ObjectNotFoundException();

private S3ObjectNotFoundException() {
super(GlobalErrorCode.S3_OBJECT_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void softDeleteById(Long archivingId) {
archivingAdaptor.save(archiving);
}

public void restoreInIdList(List<Long> archivingIds) {
public void restoreByIdIn(List<Long> archivingIds) {
List<Archiving> archivings = archivingAdaptor.findAllByIdIn(archivingIds);
archivings.forEach(Archiving::restore);
archivingAdaptor.saveAll(archivings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void softDeleteById(Long contentId) {
save(content);
}

public void restoreInIdList(List<Long> contentIds) {
public void restoreByIdIn(List<Long> contentIds) {
List<Content> contentList = contentAdaptor.findAllByIdIn(contentIds);
contentList.forEach(Content::restore);
contentAdaptor.saveAll(contentList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public void save(Recycle recycle) {
recycleRepository.save(recycle);
}

public List<Recycle> queryRecycleByUserIdInArchivingIdListAndContentIdList(
public List<Recycle> queryRecycleByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
List<Long> archivingIds, List<Long> contentIds, Long userId) {
return recycleRepository.queryRecycleByUserIdInArchivingIdListAndContentIdList(
return recycleRepository.queryRecycleByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
archivingIds, contentIds, userId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
import java.util.List;

public interface RecycleCustomRepository {
List<Recycle> queryRecycleByUserIdInArchivingIdListAndContentIdList(
List<Recycle> queryRecycleByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
List<Long> archivingIds, List<Long> contentIds, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RecycleCustomRepositoryImpl implements RecycleCustomRepository {
private final JPAQueryFactory queryFactory;

@Override
public List<Recycle> queryRecycleByUserIdInArchivingIdListAndContentIdList(
public List<Recycle> queryRecycleByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
List<Long> archivingIds, List<Long> contentIds, Long userId) {
return queryFactory
.selectFrom(recycle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public void save(Recycle recycle) {
recycleAdaptor.save(recycle);
}

public void deleteAllByUserIdAndArchivingIdOrUserIdAndContentId(
public void deleteAllByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
List<Long> archivingIds, List<Long> contentIds, Long userId) {
List<Recycle> recycleList =
recycleAdaptor.queryRecycleByUserIdInArchivingIdListAndContentIdList(
recycleAdaptor.queryRecycleByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
archivingIds, contentIds, userId);
recycleAdaptor.deleteAll(recycleList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RecycleValidator {

public void validateExist(List<Long> archivingIds, List<Long> contentIds, Long userId) {
List<Recycle> recycleList =
recycleAdaptor.queryRecycleByUserIdInArchivingIdListAndContentIdList(
recycleAdaptor.queryRecycleByUserIdAndArchivingIdInOrUserIdAndContentIdIn(
archivingIds, contentIds, userId);
Long archivingCnt =
recycleList.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import allchive.server.core.annotation.Adaptor;
import allchive.server.domain.domains.report.domain.Report;
import allchive.server.domain.domains.report.repository.ReportRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;

@Adaptor
Expand All @@ -26,4 +27,9 @@ public Boolean queryReportExistByUserIdAndArchivingId(Long userId, Long archivin
public void deleteAllByReportedUserId(Long userId) {
reportRepository.deleteAllByReportedUserId(userId);
}

public void deleteAllByArchivingIdInOrContentIdIn(
List<Long> archivingIds, List<Long> contentIds) {
reportRepository.queryDeleteAllByArchivingIdInOrContentIdIn(archivingIds, contentIds);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package allchive.server.domain.domains.report.repository;


import java.util.List;

public interface ReportCustomRepository {
Boolean queryReportExistByUserIdAndContentId(Long userId, Long contentId);

Boolean queryReportExistByUserIdAndArchivingId(Long userId, Long archivingId);

void queryDeleteAllByArchivingIdInOrContentIdIn(List<Long> archivingIds, List<Long> contentIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
Expand Down Expand Up @@ -32,6 +33,12 @@ public Boolean queryReportExistByUserIdAndArchivingId(Long userId, Long archivin
return fetchOne != null;
}

@Override
public void queryDeleteAllByArchivingIdInOrContentIdIn(
List<Long> archivingIds, List<Long> contentIds) {
queryFactory.delete(report).where(archivingIdInOrContentIdIn(archivingIds, contentIds));
}

private BooleanExpression userIdEq(Long userId) {
return report.userId.eq(userId);
}
Expand All @@ -43,4 +50,9 @@ private BooleanExpression contentIdEq(Long contentId) {
private BooleanExpression archivingIdEq(Long archivingId) {
return report.archivingId.eq(archivingId);
}

private BooleanExpression archivingIdInOrContentIdIn(
List<Long> archivingIds, List<Long> contentIds) {
return report.archivingId.in(archivingIds).or(report.contentId.in(contentIds));
}
}
Loading

0 comments on commit 7da69d3

Please sign in to comment.