Skip to content

Commit

Permalink
[refactor] 태그 수정 검증로직 추가 및 태그 순서 필드명 수정 (#89)
Browse files Browse the repository at this point in the history
* refactor: 중복 이름 검증 로직 추가 및 순서,이름 로직를 하나로 묶음

* refactor: 태그 순서 필드명 수정 order -> tagOrder

* refactor: 태그 순서 필드명 수정 order -> tagOrder

* refactor: 태그 수정시 결과 리스트 tagOrder로 오름차순 정렬하여 반환
  • Loading branch information
Gyaak authored Sep 30, 2024
1 parent dd4a1ae commit 2e4b2b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,16 @@ public static TagUpdater fromTagList(List<Tag> tagList) {
public void updateTag(TagUpdateRequest request) {

Tag target = getTagById(request.id());
target.updateTag(request.name(), request.order());
target.updateTag(request.name(), request.tagOrder());
}

public void validateTagOrder() throws ApiTagException {

Set<Integer> orderSet = new HashSet<>();

for (Tag tag : tagMap.values()) {
// 중복되거나 음수면 유효하지 않은 tag order
if (!orderSet.add(tag.getTagOrder()) || tag.getTagOrder() < 0) {
throw ApiTagException.TAG_INVALID_ORDER();
}
}
public void validateUpdateTag() throws ApiTagException {
validateTagOrder();
validateTagName();
}

public Collection<Tag> getTags() {

return tagMap.values();
}

Expand All @@ -58,4 +51,26 @@ private Tag getTagById(Long tagId) {
throw ApiTagException.UNAUTHORIZED_TAG_ACCESS();
}

private void validateTagOrder() throws ApiTagException {

Set<Integer> orderSet = new HashSet<>();

for (Tag tag : tagMap.values()) {
// 중복되거나 음수면 유효하지 않은 tag order
if (!orderSet.add(tag.getTagOrder()) || tag.getTagOrder() < 0) {
throw ApiTagException.TAG_INVALID_ORDER();
}
}
}

private void validateTagName() throws ApiTagException {
Set<String> nameSet = new HashSet<>();

for (Tag tag : tagMap.values()) {
if (!nameSet.add(tag.getName())) {
throw ApiTagException.TAG_INVALID_NAME();
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kernel360.techpick.feature.tag.service;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -56,11 +57,12 @@ public List<TagResponse> updateTagList(Long userId, List<TagUpdateRequest> tagUp
for (var req : tagUpdateRequests) {
tagUpdater.updateTag(req);
}
tagUpdater.validateTagOrder();
tagUpdater.validateUpdateTag();

return tagProvider.saveAll(tagUpdater.getTags())
.stream()
.map(tagMapper::createTagResponse)
.sorted(Comparator.comparingInt(TagResponse::tagOrder))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public record TagResponse(
Long id,
String name,
int order,
int tagOrder,
Long userId
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public record TagUpdateRequest(
Long id,
String name,
int order
int tagOrder
) {
}

0 comments on commit 2e4b2b8

Please sign in to comment.