Skip to content

Commit

Permalink
[refac] archiving 리팩토링 (#51)
Browse files Browse the repository at this point in the history
* [refac] archiving usecase 리팩토링, validation 수정 #49

* [refac] archiving 미사용 함수 정리 및 adaptor 함수 이름 변경 #49
  • Loading branch information
wjdtkdgns authored Jul 21, 2023
1 parent 7edc125 commit 58bd986
Show file tree
Hide file tree
Showing 31 changed files with 110 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,33 @@ public class ArchivingContentsResponse {
@Schema(description = "아카이빙의 총 컨텐츠 개수")
private Long totalContentsCount;

@Schema(description = "유저 소유 여부")
private Boolean isMine;

@Builder
private ArchivingContentsResponse(
SliceResponse<ContentResponse> contents,
String archivingTitle,
Long archivingId,
Long totalContentsCount) {
Long totalContentsCount,
Boolean isMine) {
this.contents = contents;
this.archivingTitle = archivingTitle;
this.archivingId = archivingId;
this.totalContentsCount = totalContentsCount;
this.isMine = isMine;
}

public static ArchivingContentsResponse of(
SliceResponse<ContentResponse> contentResponseSlice, Archiving archiving) {
SliceResponse<ContentResponse> contentResponseSlice,
Archiving archiving,
Boolean isMine) {
return ArchivingContentsResponse.builder()
.archivingId(archiving.getId())
.archivingTitle(archiving.getTitle())
.totalContentsCount(archiving.getScrapCnt() + archiving.getImgCnt())
.contents(contentResponseSlice)
.isMine(isMine)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public class CreateArchivingUseCase {
public void execute(CreateArchivingRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
final Archiving archiving = archivingMapper.toEntity(request, userId);
archivingDomainService.createArchiving(archiving);
archivingDomainService.save(archiving);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import allchive.server.domain.domains.archiving.service.ArchivingDomainService;
import allchive.server.domain.domains.archiving.validator.ArchivingValidator;
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 lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -23,10 +22,13 @@ public class DeleteArchivingUseCase {
@Transactional
public void execute(Long archivingId) {
Long userId = SecurityUtil.getCurrentUserId();
archivingValidator.verifyUser(userId, archivingId);
validateExecution(archivingId, userId);
archivingDomainService.softDeleteById(archivingId);
Recycle recycle =
recycleMapper.toArchivingRecycleEntity(userId, archivingId, RecycleType.ARCHIVING);
Recycle recycle = recycleMapper.toArchivingRecycleEntity(userId, archivingId);
recycleDomainService.save(recycle);
}

private void validateExecution(Long archivingId, Long userId) {
archivingValidator.verifyUser(userId, archivingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
@UseCase
@RequiredArgsConstructor
public class GetArchivedArchivingUseCase {
// TODO : 이거 네이밍 한번 수정해야함
private final ArchivingAdaptor archivingAdaptor;

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,34 @@ public class GetArchivingContentsUseCase {
@Transactional(readOnly = true)
public ArchivingContentsResponse execute(Long archivingId, Pageable pageable) {
Long userId = SecurityUtil.getCurrentUserId();
validateExecution(archivingId, userId);
Archiving archiving = archivingAdaptor.findById(archivingId);
Slice<ContentResponse> contentResponseSlice =
getContentResponseSlice(archivingId, pageable);
return ArchivingContentsResponse.of(
SliceResponse.of(contentResponseSlice),
archiving,
calculateIsMine(archiving, userId));
}

private void validateExecution(Long archivingId, Long userId) {
archivingValidator.validatePublicStatus(archivingId, userId);
archivingValidator.validateDeleteStatus(archivingId, userId);
Archiving archiving = archivingAdaptor.findById(archivingId);
}

private Slice<ContentResponse> getContentResponseSlice(Long archivingId, Pageable pageable) {
Slice<Content> contentList =
contentAdaptor.querySliceContentByArchivingId(archivingId, pageable);
List<ContentTagGroup> contentTagGroupList =
contentTagGroupAdaptor.queryContentTagGroupByContentIn(contentList.getContent());
Slice<ContentResponse> contentResponseSlice =
contentList.map(
content -> contentMapper.toContentResponse(content, contentTagGroupList));
return ArchivingContentsResponse.of(SliceResponse.of(contentResponseSlice), archiving);
return contentList.map(
content -> contentMapper.toContentResponse(content, contentTagGroupList));
}

private Boolean calculateIsMine(Archiving archiving, Long userId) {
if (archiving.getUserId().equals(userId)) {
return Boolean.TRUE;
}
return Boolean.FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public SliceResponse<ArchivingResponse> execute(Category category, Pageable page
scrapAdaptor.findAllByUserId(userId).stream().map(Scrap::getArchivingId).toList();
Slice<ArchivingResponse> archivingSlices =
archivingAdaptor
.querySliceArchivingIn(archivingIdList, category, pageable)
.querySliceArchivingByIdIn(archivingIdList, category, pageable)
.map(
archiving ->
ArchivingResponse.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ public class UpdateArchivingPinUseCase {
@Transactional
public void execute(Long archivingId, Boolean cancel) {
Long userId = SecurityUtil.getCurrentUserId();
validateExecution(archivingId, userId, cancel);
if (cancel) {
archivingDomainService.updatePin(archivingId, userId, false);
} else {
archivingDomainService.updatePin(archivingId, userId, true);
}
}

private void validateExecution(Long archivingId, Long userId, Boolean cancel) {
archivingValidator.validateExistById(archivingId);
archivingValidator.validateDeleteStatus(archivingId, userId);
if (cancel) {
archivingValidator.validateNotPinStatus(archivingId, userId);
archivingDomainService.updatePin(archivingId, userId, false);
} else {
archivingValidator.validateAlreadyPinStatus(archivingId, userId);
archivingDomainService.updatePin(archivingId, userId, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ public class UpdateArchivingScrapUseCase {

@Transactional
public void execute(Long archivingId, Boolean cancel) {
archivingValidator.validateExistById(archivingId);
Long userId = SecurityUtil.getCurrentUserId();
archivingValidator.validateDeleteStatus(archivingId, userId);
User user = userAdaptor.findUserById(userId);
validateExecution(archivingId, userId, cancel);
User user = userAdaptor.findById(userId);
if (cancel) {
scrapDomainService.deleteScrapByUserAndArchivingId(user, archivingId);
archivingDomainService.updateScrapCount(archivingId, -1);
} else {
scrapValidator.validateExistScrap(user, archivingId);
Scrap scrap = Scrap.of(user, archivingId);
scrapDomainService.save(scrap);
archivingDomainService.updateScrapCount(archivingId, 1);
}
}

private void validateExecution(Long archivingId, Long userId, Boolean cancel) {
archivingValidator.validateExistById(archivingId);
archivingValidator.validateDeleteStatus(archivingId, userId);
if (!cancel) {
scrapValidator.validateExistScrap(userId, archivingId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public class UpdateArchivingUseCase {

@Transactional
public void execute(Long archivingId, UpdateArchivingRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
archivingValidator.verifyUser(userId, archivingId);
validateExecution(archivingId);
Archiving archiving = archivingAdaptor.findById(archivingId);
archivingDomainService.updateArchiving(
archiving,
Expand All @@ -31,4 +30,9 @@ public void execute(Long archivingId, UpdateArchivingRequest request) {
request.isPublicStatus(),
request.getCategory());
}

private void validateExecution(Long archivingId) {
Long userId = SecurityUtil.getCurrentUserId();
archivingValidator.verifyUser(userId, archivingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class TokenRefreshUseCase {
public OauthRegisterResponse execute(String refreshToken) {
RefreshTokenEntity oldToken = refreshTokenAdaptor.findTokenByRefreshToken(refreshToken);
Long userId = jwtTokenProvider.parseRefreshToken(oldToken.getRefreshToken());
User user = userAdaptor.findUserById(userId);
User user = userAdaptor.findById(userId);
return OauthRegisterResponse.from(tokenGenerateHelper.execute(user));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class WithdrawUserUseCase {

public void execute(OauthProvider provider, String appleAccessToken) {
Long userId = SecurityUtil.getCurrentUserId();
User user = userAdaptor.findUserById(userId);
User user = userAdaptor.findById(userId);
// oauth쪽 탈퇴
withdrawOauth(provider, appleAccessToken, user);
// 우리쪽 탈퇴
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public BlockResponse execute(BlockRequest request) {
blockValidator.validateNotMyself(userId, request.getUserId());
Block block = blockMapper.toEntity(userId, request.getUserId());
blockDomainService.save(block);
return BlockResponse.from(userAdaptor.findUserById(request.getUserId()).getNickname());
return BlockResponse.from(userAdaptor.findById(request.getUserId()).getNickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public BlockResponse execute(BlockRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
blockValidator.validateExist(userId, request.getUserId());
blockDomainService.deleteByBlockFromAndBlockUser(userId, request.getUserId());
return BlockResponse.from(userAdaptor.findUserById(request.getUserId()).getNickname());
return BlockResponse.from(userAdaptor.findById(request.getUserId()).getNickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@ public class ImageUrlResponse {
@Schema(defaultValue = "이미지 presigned url", description = "이미지 업로드에 사용")
private String url;

@Schema(defaultValue = "이미지 고유 key 값", description = "서버로 이 값만 보내주시면 됩니다")
private String key;

@Builder
private ImageUrlResponse(String url, String key) {
private ImageUrlResponse(String url) {
this.url = url;
this.key = key;
}

public static ImageUrlResponse from(ImageUrlDto imageUrlDto) {
return ImageUrlResponse.builder()
.key(imageUrlDto.getKey())
.url(imageUrlDto.getUrl())
.build();
return ImageUrlResponse.builder().url(imageUrlDto.getUrl()).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public Recycle toContentRecycleEntity(Long userId, Long contentId, RecycleType t
return Recycle.of(type, contentId, null, userId);
}

public Recycle toArchivingRecycleEntity(Long userId, Long archivingId, RecycleType type) {
return Recycle.of(type, null, archivingId, userId);
public Recycle toArchivingRecycleEntity(Long userId, Long archivingId) {
return Recycle.of(RecycleType.ARCHIVING, null, archivingId, userId);
}

public DeletedObjectResponse toDeletedObjectResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class GetUserInfoUseCase {
public GetUserInfoResponse execute() {
Long userId = SecurityUtil.getCurrentUserId();
userValidator.validateUserStatusNormal(userId);
User user = userAdaptor.findUserById(userId);
User user = userAdaptor.findById(userId);
return GetUserInfoResponse.from(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class GetUserProfileUseCase {
public GetUserProfileResponse execute() {
Long userId = SecurityUtil.getCurrentUserId();
userValidator.validateUserStatusNormal(userId);
User user = userAdaptor.findUserById(userId);
User user = userAdaptor.findById(userId);
List<Archiving> archivingList = archivingAdaptor.findAllByUserId(userId);
return userMapper.toGetUserProfileResponse(archivingList, user);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ public Archiving findById(Long archivingId) {
.orElseThrow(() -> ArchivingNotFoundException.EXCEPTION);
}

public void deleteById(Long archivingId) {
archivingRepository.deleteById(archivingId);
}

public Slice<Archiving> querySliceArchivingExceptBlock(
List<Long> archivingIdList,
List<Long> blockList,
Expand All @@ -48,9 +44,9 @@ public Slice<Archiving> querySliceArchivingByUserId(
return archivingRepository.querySliceArchivingByUserId(userId, category, pageable);
}

public Slice<Archiving> querySliceArchivingIn(
public Slice<Archiving> querySliceArchivingByIdIn(
List<Long> archivingIdList, Category category, Pageable pageable) {
return archivingRepository.querySliceArchivingIn(archivingIdList, category, pageable);
return archivingRepository.querySliceArchivingByIdIn(archivingIdList, category, pageable);
}

public List<Archiving> queryArchivingByUserId(Long userId) {
Expand All @@ -73,10 +69,6 @@ public void saveAll(List<Archiving> archivings) {
archivingRepository.saveAll(archivings);
}

public List<Archiving> findAllByUserIdAndDeleted(Long userId) {
return archivingRepository.findAllByUserIdAndDeleteStatus(userId, true);
}

public void deleteAllById(List<Long> archivingIds) {
archivingRepository.deleteAllById(archivingIds);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Slice<Archiving> querySliceArchivingExceptBlock(

Slice<Archiving> querySliceArchivingByUserId(Long userId, Category category, Pageable pageable);

Slice<Archiving> querySliceArchivingIn(
Slice<Archiving> querySliceArchivingByIdIn(
List<Long> archivingIdList, Category category, Pageable pageable);

List<Archiving> queryArchivingByUserId(Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Slice<Archiving> querySliceArchivingByUserId(
}

@Override
public Slice<Archiving> querySliceArchivingIn(
public Slice<Archiving> querySliceArchivingByIdIn(
List<Long> archivingIdList, Category category, Pageable pageable) {
List<Archiving> archivings =
queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ public interface ArchivingRepository

List<Archiving> findAllByIdIn(List<Long> ids);

List<Archiving> findAllByUserIdAndDeleteStatus(Long userId, boolean deleteStatus);

List<Archiving> findAllByPublicStatus(Boolean publicStatus);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class ArchivingDomainService {
private final ArchivingAdaptor archivingAdaptor;

public void createArchiving(Archiving archiving) {
public void save(Archiving archiving) {
archivingAdaptor.save(archiving);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import allchive.server.core.annotation.Adaptor;
import allchive.server.domain.domains.user.domain.Scrap;
import allchive.server.domain.domains.user.domain.User;
import allchive.server.domain.domains.user.exception.exceptions.ScrapNotFoundException;
import allchive.server.domain.domains.user.repository.ScrapRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

@Adaptor
Expand All @@ -18,8 +18,10 @@ public List<Scrap> findAllByUserId(Long userId) {
return scrapRepository.findAllByUserId(userId);
}

public Optional<Scrap> findByUserAndArchivingId(User user, Long archivingId) {
return scrapRepository.findAllByUserAndArchivingId(user, archivingId);
public Scrap findByUserAndArchivingId(User user, Long archivingId) {
return scrapRepository
.findAllByUserAndArchivingId(user, archivingId)
.orElseThrow(() -> ScrapNotFoundException.EXCEPTION);
}

public void delete(Scrap scrap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public class UserAdaptor {
private final UserRepository userRepository;

public Boolean exist(OauthInfo oauthInfo) {
return userRepository.findByOauthInfo(oauthInfo).isPresent();
return userRepository.existsByOauthInfo(oauthInfo);
}

public User queryUserByOauthInfo(OauthInfo oauthInfo) {
public User findByOauthInfo(OauthInfo oauthInfo) {
return userRepository
.findByOauthInfo(oauthInfo)
.orElseThrow(() -> UserNotFoundException.EXCEPTION);
Expand All @@ -28,7 +28,7 @@ public void save(User user) {
userRepository.save(user);
}

public User findUserById(Long userId) {
public User findById(Long userId) {
return userRepository.findById(userId).orElseThrow(() -> UserNotFoundException.EXCEPTION);
}

Expand Down
Loading

0 comments on commit 58bd986

Please sign in to comment.