Skip to content

Commit

Permalink
[refac] report, block 리팩토링 (#75)
Browse files Browse the repository at this point in the history
* [refac] 차단하기 validation 정리 #74

* [refac] 차단 풀기 validation 추가 및 정리 #74

* [refac] 신고하기 함수 정리 #74
  • Loading branch information
wjdtkdgns authored Aug 1, 2023
1 parent 7da69d3 commit ec87e9e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import allchive.server.domain.domains.block.service.BlockDomainService;
import allchive.server.domain.domains.block.validator.BlockValidator;
import allchive.server.domain.domains.user.adaptor.UserAdaptor;
import allchive.server.domain.domains.user.validator.UserValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
public class CreateBlockUseCase {
private final UserValidator userValidator;
private final BlockValidator blockValidator;
private final BlockMapper blockMapper;
private final BlockDomainService blockDomainService;
Expand All @@ -24,10 +26,15 @@ public class CreateBlockUseCase {
@Transactional
public BlockResponse execute(BlockRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
blockValidator.validateNotDuplicate(userId, request.getUserId());
blockValidator.validateNotMyself(userId, request.getUserId());
validateExecution(userId, request);
Block block = blockMapper.toEntity(userId, request.getUserId());
blockDomainService.save(block);
return BlockResponse.from(userAdaptor.findById(request.getUserId()).getNickname());
}

private void validateExecution(Long userId, BlockRequest request) {
userValidator.validateExist(request.getUserId());
blockValidator.validateNotDuplicate(userId, request.getUserId());
blockValidator.validateNotMyself(userId, request.getUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@
import allchive.server.domain.domains.block.service.BlockDomainService;
import allchive.server.domain.domains.block.validator.BlockValidator;
import allchive.server.domain.domains.user.adaptor.UserAdaptor;
import allchive.server.domain.domains.user.validator.UserValidator;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;

@UseCase
@RequiredArgsConstructor
public class DeleteBlockUseCase {
private final UserValidator userValidator;
private final BlockValidator blockValidator;
private final BlockDomainService blockDomainService;
private final UserAdaptor userAdaptor;

@Transactional
public BlockResponse execute(BlockRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
blockValidator.validateExist(userId, request.getUserId());
validateExecution(userId, request);
blockDomainService.deleteByBlockFromAndBlockUser(userId, request.getUserId());
return BlockResponse.from(userAdaptor.findById(request.getUserId()).getNickname());
}

private void validateExecution(Long userId, BlockRequest request) {
userValidator.validateExist(request.getUserId());
blockValidator.validateExist(userId, request.getUserId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,29 @@ public class CreateReportUseCase {
@Transactional
public void execute(CreateReportRequest request, ReportObjectType type) {
Long userId = SecurityUtil.getCurrentUserId();
reportValidator.validateNotDuplicateReport(userId, request.getId(), type);
validateExecution(userId, request.getId(), type);
Long reportedUserId = getReportedUserId(request.getId(), type);
Report report = reportMapper.toEntity(request, type, userId, reportedUserId);
reportDomainService.save(report);
}

private void validateExecution(Long userId, Long objId, ReportObjectType type) {
reportValidator.validateNotDuplicateReport(userId, objId, type);
}

private Long getReportedUserId(Long objId, ReportObjectType type) {
Long reportedUserId = 0L;
switch (type) {
case CONTENT -> {
contentValidator.validateExistById(request.getId());
Long archivingId = contentAdaptor.findById(request.getId()).getArchivingId();
contentValidator.validateExistById(objId);
Long archivingId = contentAdaptor.findById(objId).getArchivingId();
reportedUserId = archivingAdaptor.findById(archivingId).getUserId();
}
case ARCHIVING -> {
archivingValidator.validateExistById(request.getId());
reportedUserId = archivingAdaptor.findById(request.getId()).getUserId();
archivingValidator.validateExistById(objId);
reportedUserId = archivingAdaptor.findById(objId).getUserId();
}
}
Report report = reportMapper.toEntity(request, type, userId, reportedUserId);
reportDomainService.save(report);
return reportedUserId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ public Boolean existsByNickname(String nickname) {
public List<User> findAllByIdIn(List<Long> userIds) {
return userRepository.findAllByIdIn(userIds);
}

public Boolean existsById(Long userId) {
return userRepository.existsById(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import allchive.server.domain.domains.user.domain.enums.UserState;
import allchive.server.domain.domains.user.exception.exceptions.AlreadySignUpUserException;
import allchive.server.domain.domains.user.exception.exceptions.ForbiddenUserException;
import allchive.server.domain.domains.user.exception.exceptions.UserNotFoundException;
import lombok.RequiredArgsConstructor;

@Validator
Expand All @@ -27,4 +28,10 @@ public void validateUserStatusNormal(Long userId) {
throw ForbiddenUserException.EXCEPTION;
}
}

public void validateExist(Long userId) {
if (!userAdaptor.existsById(userId)) {
throw UserNotFoundException.EXCEPTION;
}
}
}

0 comments on commit ec87e9e

Please sign in to comment.