Skip to content

Commit

Permalink
[refac] exception 세분화 및 validation 정리 (#89)
Browse files Browse the repository at this point in the history
* [refac] archiving, content 소유자 response에 추가 #88

* [refac] archiving api validation exception 세분화 #88

* [refac] content api validation exception 세분화 #88

* [refac] recycle api validation exception 세분화 #88

* [refac] search api validation exception 세분화 #88

* [refac] user api validation exception 세분화 #88

* [refac] validation 정리 #88

* [chore] spotless 적용 #88
  • Loading branch information
wjdtkdgns authored Aug 4, 2023
1 parent 56a2cc1 commit 971da20
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ArchivingContentsResponse {
@Schema(description = "아카이빙의 총 컨텐츠 개수")
private Long totalContentsCount;

@Schema(description = "아카이빙 소유자 고유번호")
private Long ownerId;

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

Expand All @@ -30,11 +33,13 @@ private ArchivingContentsResponse(
String archivingTitle,
Long archivingId,
Long totalContentsCount,
Long ownerId,
Boolean isMine) {
this.contents = contents;
this.archivingTitle = archivingTitle;
this.archivingId = archivingId;
this.totalContentsCount = totalContentsCount;
this.ownerId = ownerId;
this.isMine = isMine;
}

Expand All @@ -47,6 +52,7 @@ public static ArchivingContentsResponse of(
.archivingTitle(archiving.getTitle())
.totalContentsCount(archiving.getScrapCnt() + archiving.getImgCnt())
.contents(contentResponseSlice)
.ownerId(archiving.getUserId())
.isMine(isMine)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public ArchivingContentsResponse execute(Long archivingId, Pageable pageable) {
}

private void validateExecution(Long archivingId, Long userId) {
archivingValidator.validatePublicStatus(archivingId, userId);
archivingValidator.validateDeleteStatus(archivingId, userId);
archivingValidator.validatePublic(archivingId, userId);
archivingValidator.validateNotDeleteExceptUser(archivingId, userId);
}

private Slice<ContentResponse> getContentResponseSlice(Long archivingId, Pageable pageable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public class GetArchivingInfoUseCase {
private final ArchivingValidator archivingValidator;
private final ArchivingAdaptor archivingAdaptor;

@Transactional
@Transactional(readOnly = true)
public ArchivingResponse execute(Long archivingId) {
archivingValidator.validateExistById(archivingId);

validateExecution(archivingId);
Archiving archiving = archivingAdaptor.findById(archivingId);
return ArchivingResponse.of(archiving, Boolean.FALSE);
}

private void validateExecution(Long archivingId) {
archivingValidator.validateExistById(archivingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ public class UpdateArchivingPinUseCase {
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);
}
archivingDomainService.updatePin(archivingId, userId, !cancel);
}

private void validateExecution(Long archivingId, Long userId, Boolean cancel) {
archivingValidator.validateExistById(archivingId);
archivingValidator.validateDeleteStatus(archivingId, userId);
archivingValidator.validateNotDeleteExceptUser(archivingId, userId);
if (cancel) {
archivingValidator.validateNotPinStatus(archivingId, userId);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public void execute(Long archivingId, Boolean cancel) {
}

private void validateExecution(Long archivingId, Long userId, Boolean cancel) {
archivingValidator.validateExistById(archivingId);
archivingValidator.validateDeleteStatus(archivingId, userId);
archivingValidator.validateNotDeleteExceptUser(archivingId, userId);
if (cancel) {
scrapValidator.validateExistScrap(userId, archivingId);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public class ContentTagResponse {

private List<TagResponse> tagList;

@Schema(description = "컨텐츠 소유자 고유번호")
private Long ownerId;

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

@Builder
Expand All @@ -54,6 +58,7 @@ private ContentTagResponse(
String imgUrl,
LocalDateTime contentCreatedAt,
List<TagResponse> tagList,
Long ownerId,
Boolean isMine) {
this.contentId = contentId;
this.contentTitle = contentTitle;
Expand All @@ -63,11 +68,12 @@ private ContentTagResponse(
this.imgUrl = imgUrl;
this.contentCreatedAt = contentCreatedAt;
this.tagList = tagList;
this.ownerId = ownerId;
this.isMine = isMine;
}

public static ContentTagResponse of(
Content content, List<TagResponse> tagList, Boolean isMine) {
Content content, List<TagResponse> tagList, Boolean isMine, Long ownerId) {
return ContentTagResponse.builder()
.contentId(content.getId())
.contentTitle(content.getTitle())
Expand All @@ -77,6 +83,7 @@ public static ContentTagResponse of(
.imgUrl(UrlUtil.toAssetUrl(content.getImageUrl()))
.contentCreatedAt(content.getCreatedAt())
.tagList(tagList)
.ownerId(ownerId)
.isMine(isMine)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ public Content toEntity(CreateContentRequest request) {
}

public ContentTagResponse toContentTagResponse(
Content content, List<ContentTagGroup> contentTagGroupList, Boolean isMine) {
Content content,
List<ContentTagGroup> contentTagGroupList,
Boolean isMine,
Long ownerId) {
List<TagResponse> tagResponseList =
contentTagGroupList.stream()
.map(contentTagGroup -> TagResponse.from(contentTagGroup.getTag()))
.toList();
return ContentTagResponse.of(content, tagResponseList, isMine);
return ContentTagResponse.of(content, tagResponseList, isMine, ownerId);
}

public List<ContentTagGroup> toContentTagGroupEntityList(Content content, List<Tag> tags) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public void execute(CreateContentRequest request) {

private void validateExecution(CreateContentRequest request) {
Long userId = SecurityUtil.getCurrentUserId();
archivingValidator.validateExistById(request.getArchivingId());
archivingValidator.verifyUser(request.getArchivingId(), userId);
archivingValidator.verifyUser(userId, request.getArchivingId());
tagValidator.validateExistTagsAndUser(request.getTagIds(), userId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ public ContentTagResponse execute(Long contentId) {
Long userId = SecurityUtil.getCurrentUserId();
validateExecution(contentId, userId);
Content content = contentAdaptor.findById(contentId);
Long ownerId = archivingAdaptor.findById(content.getArchivingId()).getUserId();
List<ContentTagGroup> contentTagGroupList =
contentTagGroupAdaptor.queryContentTagGroupByContentWithTag(content);
Boolean isMine = calculateIsMine(content.getArchivingId(), userId);
return contentMapper.toContentTagResponse(content, contentTagGroupList, isMine);
return contentMapper.toContentTagResponse(content, contentTagGroupList, isMine, ownerId);
}

private void validateExecution(Long contentId, Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

import allchive.server.domain.common.model.BaseTimeEntity;
import allchive.server.domain.domains.archiving.domain.enums.Category;
import allchive.server.domain.domains.archiving.exception.exceptions.DeletedArchivingException;
import allchive.server.domain.domains.archiving.exception.exceptions.NoAuthurityUpdateArchivingException;
import allchive.server.domain.domains.archiving.exception.exceptions.NotPublicArchivingException;
import allchive.server.domain.domains.archiving.exception.exceptions.*;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
Expand Down Expand Up @@ -85,13 +83,13 @@ public void validateUser(Long userId) {
}
}

public void validatePublicStatus(Long userId) {
public void validatePublic(Long userId) {
if (!this.publicStatus && !this.userId.equals(userId)) {
throw NotPublicArchivingException.EXCEPTION;
}
}

public void validateDeleteStatus(Long userId) {
public void validateNotDeleteExceptUser(Long userId) {
if (this.deleteStatus && !this.userId.equals(userId)) {
throw DeletedArchivingException.EXCEPTION;
}
Expand Down Expand Up @@ -130,4 +128,16 @@ public void validateNotDelete() {
throw DeletedArchivingException.EXCEPTION;
}
}

public void validateAlreadyPinStatus(Long userId) {
if (this.pinUserId.contains(userId)) {
throw AlreadyPinnedArchivingException.EXCEPTION;
}
}

public void validateNotPinStatus(Long userId) {
if (!this.pinUserId.contains(userId)) {
throw NotPinnedArchivingException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import allchive.server.core.annotation.Validator;
import allchive.server.domain.domains.archiving.adaptor.ArchivingAdaptor;
import allchive.server.domain.domains.archiving.domain.Archiving;
import allchive.server.domain.domains.archiving.exception.exceptions.AlreadyPinnedArchivingException;
import allchive.server.domain.domains.archiving.exception.exceptions.ArchivingNotFoundException;
import allchive.server.domain.domains.archiving.exception.exceptions.NoAuthurityUpdateArchivingException;
import allchive.server.domain.domains.archiving.exception.exceptions.NotPinnedArchivingException;
import java.util.List;
import lombok.RequiredArgsConstructor;

Expand All @@ -20,12 +17,12 @@ public void verifyUser(Long userId, Long archivingId) {
archivingAdaptor.findById(archivingId).validateUser(userId);
}

public void validatePublicStatus(Long archivingId, Long userId) {
archivingAdaptor.findById(archivingId).validatePublicStatus(userId);
public void validatePublic(Long archivingId, Long userId) {
archivingAdaptor.findById(archivingId).validatePublic(userId);
}

public void validateDeleteStatus(Long archivingId, Long userId) {
archivingAdaptor.findById(archivingId).validateDeleteStatus(userId);
public void validateNotDeleteExceptUser(Long archivingId, Long userId) {
archivingAdaptor.findById(archivingId).validateNotDeleteExceptUser(userId);
}

public void validateExistById(Long archivingId) {
Expand All @@ -35,15 +32,11 @@ public void validateExistById(Long archivingId) {
}

public void validateAlreadyPinStatus(Long archivingId, Long userId) {
if (archivingAdaptor.findById(archivingId).getPinUserId().contains(userId)) {
throw AlreadyPinnedArchivingException.EXCEPTION;
}
archivingAdaptor.findById(archivingId).validateAlreadyPinStatus(userId);
}

public void validateNotPinStatus(Long archivingId, Long userId) {
if (!archivingAdaptor.findById(archivingId).getPinUserId().contains(userId)) {
throw NotPinnedArchivingException.EXCEPTION;
}
archivingAdaptor.findById(archivingId).validateNotPinStatus(userId);
}

public void validateExistInIdList(List<Long> archivingIdList) {
Expand All @@ -55,12 +48,7 @@ public void validateExistInIdList(List<Long> archivingIdList) {

public void verifyUserInIdList(Long userId, List<Long> archivingIds) {
List<Archiving> archivingList = archivingAdaptor.findAllByIdIn(archivingIds);
archivingList.forEach(
archiving -> {
if (!archiving.getUserId().equals(userId)) {
throw NoAuthurityUpdateArchivingException.EXCEPTION;
}
});
archivingList.forEach(archiving -> archiving.validateUser(userId));
}

public void validateNotDeleted(Long archivingId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import allchive.server.domain.common.model.BaseTimeEntity;
import allchive.server.domain.domains.content.domain.enums.ContentType;
import allchive.server.domain.domains.content.exception.exceptions.AlreadyDeletedContentException;
import javax.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -89,4 +90,10 @@ public void updateImageContent(Long archivingId, String imgUrl, String title, St
this.title = title;
this.memo = memo;
}

public void validateNotDelete() {
if (this.isDeleteStatus()) {
throw AlreadyDeletedContentException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import allchive.server.domain.domains.archiving.domain.Archiving;
import allchive.server.domain.domains.content.adaptor.ContentAdaptor;
import allchive.server.domain.domains.content.domain.Content;
import allchive.server.domain.domains.content.exception.exceptions.AlreadyDeletedContentException;
import allchive.server.domain.domains.content.exception.exceptions.ContentNotFoundException;
import allchive.server.domain.domains.content.exception.exceptions.NoAuthorityUpdateContentException;
import allchive.server.domain.domains.content.exception.exceptions.NotPublicContentException;
Expand Down Expand Up @@ -56,9 +55,7 @@ public void verifyUser(Long contentId, Long userId) {
}

public void validateNotDelete(Long contentId) {
if (contentAdaptor.findById(contentId).isDeleteStatus()) {
throw AlreadyDeletedContentException.EXCEPTION;
}
contentAdaptor.findById(contentId).validateNotDelete();
}

public void validatePublic(Long contentId, Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import allchive.server.core.annotation.Validator;
import allchive.server.domain.domains.content.adaptor.TagAdaptor;
import allchive.server.domain.domains.content.domain.Tag;
import allchive.server.domain.domains.content.exception.exceptions.NoAuthorityUpdateTagException;
import allchive.server.domain.domains.content.exception.exceptions.TagNotFoundException;
import java.util.List;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,11 +22,6 @@ public void validateExistTagsAndUser(List<Long> tagIds, Long userId) {
if (tagIds.size() != tags.size()) {
throw TagNotFoundException.EXCEPTION;
}
tags.forEach(
tag -> {
if (!tag.getUserId().equals(userId)) {
throw NoAuthorityUpdateTagException.EXCEPTION;
}
});
tags.forEach(tag -> tag.validateUser(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import allchive.server.domain.common.model.BaseTimeEntity;
import allchive.server.domain.domains.search.exception.exceptions.NoAuthorityUpdateLatestSearchException;
import javax.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down Expand Up @@ -30,4 +31,10 @@ private LatestSearch(String keyword, Long userId) {
public static LatestSearch of(String keyword, Long userId) {
return LatestSearch.builder().keyword(keyword).userId(userId).build();
}

public void validateUser(Long userId) {
if (!this.userId.equals(userId)) {
throw NoAuthorityUpdateLatestSearchException.EXCEPTION;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import allchive.server.domain.domains.search.adaptor.LatestSearchAdaptor;
import allchive.server.domain.domains.search.domain.LatestSearch;
import allchive.server.domain.domains.search.exception.exceptions.LatestSearchNotFoundException;
import allchive.server.domain.domains.search.exception.exceptions.NoAuthorityUpdateLatestSearchException;
import java.util.List;
import lombok.RequiredArgsConstructor;

Expand All @@ -23,11 +22,6 @@ public void validateExistByIdIn(List<Long> ids) {

public void verifyUserByIdIn(List<Long> ids, Long userId) {
List<LatestSearch> searches = latestSearchAdaptor.findAllByIdIn(ids);
searches.forEach(
search -> {
if (!search.getUserId().equals(userId)) {
throw NoAuthorityUpdateLatestSearchException.EXCEPTION;
}
});
searches.forEach(search -> search.validateUser(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ public void updateInfo(String name, String email, String nickname, String imgUrl
this.nickname = nickname;
this.profileImgUrl = imgUrl;
}

public void validateUserStateNormal() {
if (!this.userState.equals(UserState.NORMAL)) {
throw ForbiddenUserException.EXCEPTION;
}
}
}
Loading

0 comments on commit 971da20

Please sign in to comment.