Skip to content

Commit 6326f5e

Browse files
committed
feat/ #18 claude 코드 리뷰 반영
1 parent 984eed0 commit 6326f5e

6 files changed

Lines changed: 31 additions & 11 deletions

File tree

src/main/java/com/closetnangam/be/domain/catalog/repository/StyleRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public interface StyleRepository extends JpaRepository<Style, Long> {
1010

1111
Optional<Style> findByCode(String code);
1212

13+
List<Style> findByCodeIn(List<String> codes);
14+
1315
List<Style> findAllByOrderByCodeAsc();
1416

1517
boolean existsByCode(String code);

src/main/java/com/closetnangam/be/domain/catalog/service/CategoryCatalogService.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,8 @@ public void validateStyleCodes(List<String> styleCodes) {
140140
if (styleCodes == null || styleCodes.isEmpty()) {
141141
throw new IllegalArgumentException("스타일은 1개 이상 선택해야 합니다.");
142142
}
143-
144143
for (String styleCode : styleCodes) {
145144
StyleCode.fromCode(styleCode);
146-
if (!styleRepository.existsByCode(styleCode)) {
147-
throw new IllegalArgumentException("존재하지 않는 스타일 코드입니다: " + styleCode);
148-
}
149145
}
150146
}
151147

src/main/java/com/closetnangam/be/domain/clothes/controller/ClothesController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ClothesController {
3030

3131
private final ClothesService clothesService;
3232

33+
// TODO: JWT 인증 구현 후 @PreAuthorize 또는 SecurityContextHolder로 userId 소유권 검증 추가 필요
3334
@Operation(summary = "보유 옷 목록 조회", description = "사용자 옷장의 보유 옷(OWNED) 목록을 조회합니다.")
3435
@GetMapping("/api/users/{userId}/clothes")
3536
public ResponseEntity<ApiResponse<List<ClothesResponse>>> getOwnedClothes(@PathVariable Long userId) {

src/main/java/com/closetnangam/be/domain/clothes/entity/Clothes.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import lombok.NoArgsConstructor;
2323

2424
import java.util.ArrayList;
25+
import java.util.HashSet;
2526
import java.util.List;
27+
import java.util.Set;
28+
import java.util.stream.Collectors;
2629

2730
@Entity
2831
@Getter
@@ -136,8 +139,19 @@ public void update(
136139
}
137140

138141
public void replaceStyleTags(List<ClothesStyleTag> newStyleTags) {
139-
this.styleTags.clear();
140-
this.styleTags.addAll(newStyleTags);
142+
Set<Long> newStyleIds = newStyleTags.stream()
143+
.map(tag -> tag.getStyle().getId())
144+
.collect(Collectors.toSet());
145+
146+
this.styleTags.removeIf(existing -> !newStyleIds.contains(existing.getStyle().getId()));
147+
148+
Set<Long> existingStyleIds = this.styleTags.stream()
149+
.map(tag -> tag.getStyle().getId())
150+
.collect(Collectors.toSet());
151+
152+
newStyleTags.stream()
153+
.filter(tag -> !existingStyleIds.contains(tag.getStyle().getId()))
154+
.forEach(this.styleTags::add);
141155
}
142156

143157
public void addStyleTag(ClothesStyleTag styleTag) {

src/main/java/com/closetnangam/be/domain/clothes/service/ClothesService.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.closetnangam.be.domain.clothes.service;
22

3+
import com.closetnangam.be.domain.catalog.entity.Style;
34
import com.closetnangam.be.domain.catalog.repository.StyleRepository;
45
import com.closetnangam.be.domain.catalog.service.CategoryCatalogService;
56
import com.closetnangam.be.domain.clothes.dto.request.ClothesCreateRequest;
@@ -129,11 +130,11 @@ private void applyStyleTags(Clothes clothes, List<String> styleCodes) {
129130
}
130131

131132
private List<ClothesStyleTag> buildStyleTags(Clothes clothes, List<String> styleCodes) {
132-
return styleCodes.stream()
133-
.map(styleRepository::findByCode)
134-
.map(optionalStyle -> optionalStyle.orElseThrow(
135-
() -> new IllegalArgumentException("존재하지 않는 스타일 코드입니다.")
136-
))
133+
List<Style> styles = styleRepository.findByCodeIn(styleCodes);
134+
if (styles.size() != styleCodes.size()) {
135+
throw new IllegalArgumentException("존재하지 않는 스타일 코드가 포함되어 있습니다.");
136+
}
137+
return styles.stream()
137138
.map(style -> ClothesStyleTag.create(clothes, style))
138139
.toList();
139140
}

src/main/java/com/closetnangam/be/global/common/exception/GlobalExceptionHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.closetnangam.be.global.common.exception;
22

33
import com.closetnangam.be.global.common.response.ApiResponse;
4+
import org.springframework.dao.DataIntegrityViolationException;
45
import org.springframework.http.HttpStatus;
56
import org.springframework.http.ResponseEntity;
67
import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -20,6 +21,11 @@ public ResponseEntity<ApiResponse<Void>> handleIllegalState(IllegalStateExceptio
2021
return ResponseEntity.status(HttpStatus.CONFLICT).body(ApiResponse.fail(exception.getMessage()));
2122
}
2223

24+
@ExceptionHandler(DataIntegrityViolationException.class)
25+
public ResponseEntity<ApiResponse<Void>> handleDataIntegrityViolation(DataIntegrityViolationException exception) {
26+
return ResponseEntity.status(HttpStatus.CONFLICT).body(ApiResponse.fail("이미 존재하는 데이터입니다."));
27+
}
28+
2329
@ExceptionHandler(MethodArgumentNotValidException.class)
2430
public ResponseEntity<ApiResponse<Void>> handleValidation(MethodArgumentNotValidException exception) {
2531
String message = exception.getBindingResult().getFieldErrors().stream()

0 commit comments

Comments
 (0)