Skip to content

Commit

Permalink
refactor: validate 로직 분리 - TagValidator 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyaak committed Sep 28, 2024
1 parent 4a2fd94 commit fec22ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,6 +15,7 @@
import kernel360.techpick.feature.tag.model.dto.TagUpdateRequest;
import kernel360.techpick.feature.tag.model.mapper.TagMapper;
import kernel360.techpick.feature.tag.model.provider.TagProvider;
import kernel360.techpick.feature.tag.validator.TagValidator;
import lombok.RequiredArgsConstructor;

@Service
Expand All @@ -24,12 +24,13 @@ public class TagService {

private final TagMapper tagMapper;
private final TagProvider tagProvider;
private final TagValidator tagValidator;
private final PickTagRepository pickTagRepository;

@Transactional
public TagResponse createTag(Long userId, TagCreateRequest request) throws ApiTagException, ApiUserException {

validateTagNameExists(userId, request.name());
tagValidator.validateTagNameExists(userId, request.name());

int lastOrder = tagProvider.getLastOrderByUSerId(userId);
Tag tag = tagProvider.save(tagMapper.createTag(request, lastOrder, userId));
Expand All @@ -54,7 +55,7 @@ public List<TagResponse> updateTagList(Long userId, List<TagUpdateRequest> tagUp
List<Tag> targetTagList = new ArrayList<>();
for (var req : tagUpdateRequests) {
Tag targetTag = tagProvider.findById(req.id());
validateTagAccess(userId, targetTag);
tagValidator.validateTagAccess(userId, targetTag);
tagMapper.updateTag(req, targetTag);
targetTagList.add(targetTag);
}
Expand All @@ -69,24 +70,10 @@ public List<TagResponse> updateTagList(Long userId, List<TagUpdateRequest> tagUp
public void deleteById(Long userId, Long tagId) throws ApiTagException {

Tag targetTag = tagProvider.findById(tagId);
validateTagAccess(userId, targetTag);
tagValidator.validateTagAccess(userId, targetTag);
// 해당 태그를 등록한 픽에서 해당 태그를 모두 삭제
pickTagRepository.deleteById(tagId);
tagProvider.deleteById(tagId);
}

private void validateTagAccess(Long userId, Tag tag) throws ApiTagException {

if (tag == null || !Objects.equals(userId, tag.getUser().getId())) {
throw ApiTagException.UNAUTHORIZED_TAG_ACCESS();
}
}

private void validateTagNameExists(Long userId, String name) throws ApiTagException {

if (tagProvider.existsByUserIdAndName(userId, name)) {
throw ApiTagException.TAG_ALREADY_EXIST();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package kernel360.techpick.feature.tag.validator;

import java.util.Objects;

import org.springframework.stereotype.Component;

import kernel360.techpick.core.exception.feature.tag.ApiTagException;
import kernel360.techpick.core.model.tag.Tag;
import kernel360.techpick.feature.tag.model.provider.TagProvider;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class TagValidator {

private final TagProvider tagProvider;

public void validateTagAccess(Long userId, Tag tag) throws ApiTagException {

if (tag == null || !Objects.equals(userId, tag.getUser().getId())) {
throw ApiTagException.UNAUTHORIZED_TAG_ACCESS();
}
}

public void validateTagNameExists(Long userId, String name) throws ApiTagException {

if (tagProvider.existsByUserIdAndName(userId, name)) {
throw ApiTagException.TAG_ALREADY_EXIST();
}
}
}

0 comments on commit fec22ac

Please sign in to comment.