Skip to content

Commit c9a4d2d

Browse files
committed
feat/ #35 코드 리뷰 반영
1 parent 35cfd1b commit c9a4d2d

6 files changed

Lines changed: 57 additions & 34 deletions

File tree

src/main/java/com/closetnangam/be/domain/catalog/entity/Style.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import lombok.Builder;
88
import lombok.Getter;
99
import lombok.NoArgsConstructor;
10+
import org.hibernate.annotations.BatchSize;
1011

12+
@BatchSize(size = 100)
1113
@Entity
1214
@Getter
1315
@NoArgsConstructor(access = AccessLevel.PROTECTED)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import jakarta.persistence.OrderBy;
1616
import jakarta.persistence.Table;
1717
import jakarta.persistence.Version;
18+
import org.hibernate.annotations.BatchSize;
1819
import lombok.AccessLevel;
1920
import lombok.Builder;
2021
import lombok.Getter;
@@ -75,10 +76,12 @@ public class Clothes extends BaseEntity {
7576
@Column(nullable = false)
7677
private Long version;
7778

79+
@BatchSize(size = 100)
7880
@OrderBy("sortOrder ASC")
7981
@OneToMany(mappedBy = "clothes", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
8082
private List<ClothingColor> colorTags = new ArrayList<>();
8183

84+
@BatchSize(size = 100)
8285
@OrderBy("sortOrder ASC")
8386
@OneToMany(mappedBy = "clothes", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
8487
private List<ClothesStyleTag> styleTags = new ArrayList<>();

src/main/java/com/closetnangam/be/domain/clothes/repository/ClothesRepository.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,6 @@
22

33
import com.closetnangam.be.domain.clothes.entity.Clothes;
44
import org.springframework.data.jpa.repository.JpaRepository;
5-
import org.springframework.data.jpa.repository.Query;
6-
import org.springframework.data.repository.query.Param;
7-
8-
import java.util.Optional;
95

106
public interface ClothesRepository extends JpaRepository<Clothes, Long> {
11-
12-
@Query("""
13-
select c from Clothes c
14-
left join fetch c.colorTags
15-
left join fetch c.styleTags st
16-
left join fetch st.style
17-
where c.id = :clothesId
18-
""")
19-
Optional<Clothes> findByIdWithDetails(@Param("clothesId") Long clothesId);
207
}

src/main/java/com/closetnangam/be/domain/clothes/repository/WardrobeClothesRepository.java

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

33
import com.closetnangam.be.domain.clothes.entity.WardrobeClothes;
4-
import com.closetnangam.be.domain.clothes.enums.SourceType;
4+
import com.closetnangam.be.domain.clothes.enums.OwnershipStatus;
55
import org.springframework.data.jpa.repository.JpaRepository;
66
import org.springframework.data.jpa.repository.Query;
77
import org.springframework.data.repository.query.Param;
@@ -14,44 +14,35 @@ public interface WardrobeClothesRepository extends JpaRepository<WardrobeClothes
1414
@Query("""
1515
select distinct wc from WardrobeClothes wc
1616
join fetch wc.clothes c
17-
left join fetch c.colorTags
18-
left join fetch c.styleTags st
19-
left join fetch st.style
2017
join fetch wc.wardrobe w
2118
join fetch w.user
2219
where w.user.id = :userId
23-
and c.sourceType = :sourceType
20+
and wc.ownershipStatus = :ownershipStatus
2421
order by c.createdAt desc
2522
""")
26-
List<WardrobeClothes> findAllByUserIdAndSourceType(
23+
List<WardrobeClothes> findAllByUserIdAndOwnershipStatus(
2724
@Param("userId") Long userId,
28-
@Param("sourceType") SourceType sourceType
25+
@Param("ownershipStatus") OwnershipStatus ownershipStatus
2926
);
3027

3128
@Query("""
3229
select distinct wc from WardrobeClothes wc
3330
join fetch wc.clothes c
34-
left join fetch c.colorTags
35-
left join fetch c.styleTags st
36-
left join fetch st.style
3731
join fetch wc.wardrobe w
3832
join fetch w.user
3933
where w.user.id = :userId
40-
and c.sourceType = :sourceType
34+
and wc.ownershipStatus = :ownershipStatus
4135
and wc.favorite = true
4236
order by wc.updatedAt desc
4337
""")
44-
List<WardrobeClothes> findFavoritesByUserIdAndSourceType(
38+
List<WardrobeClothes> findFavoritesByUserIdAndOwnershipStatus(
4539
@Param("userId") Long userId,
46-
@Param("sourceType") SourceType sourceType
40+
@Param("ownershipStatus") OwnershipStatus ownershipStatus
4741
);
4842

4943
@Query("""
5044
select wc from WardrobeClothes wc
5145
join fetch wc.clothes c
52-
left join fetch c.colorTags
53-
left join fetch c.styleTags st
54-
left join fetch st.style
5546
join fetch wc.wardrobe w
5647
join fetch w.user
5748
where c.id = :clothesId

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,25 @@ public class ClothesService {
4747
private final WardrobeService wardrobeService;
4848

4949
public List<ClothesResponse> getOwnedClothes(Long userId) {
50-
return wardrobeClothesRepository.findAllByUserIdAndSourceType(userId, SourceType.OWNED).stream()
50+
return wardrobeClothesRepository.findAllByUserIdAndOwnershipStatus(userId, OwnershipStatus.OWNED).stream()
5151
.map(entry -> ClothesResponse.from(entry.getClothes(), entry))
5252
.toList();
5353
}
5454

5555
public List<ClothesResponse> getFavoriteOwnedClothes(Long userId) {
56-
return wardrobeClothesRepository.findFavoritesByUserIdAndSourceType(userId, SourceType.OWNED).stream()
56+
return wardrobeClothesRepository.findFavoritesByUserIdAndOwnershipStatus(userId, OwnershipStatus.OWNED).stream()
5757
.map(entry -> ClothesResponse.from(entry.getClothes(), entry))
5858
.toList();
5959
}
6060

6161
public List<ClothesResponse> getWishlistClothes(Long userId) {
62-
return wardrobeClothesRepository.findAllByUserIdAndSourceType(userId, SourceType.WISHLIST).stream()
62+
return wardrobeClothesRepository.findAllByUserIdAndOwnershipStatus(userId, OwnershipStatus.WISHLIST).stream()
6363
.map(entry -> ClothesResponse.from(entry.getClothes(), entry))
6464
.toList();
6565
}
6666

6767
public List<ClothesResponse> getFavoriteWishlistClothes(Long userId) {
68-
return wardrobeClothesRepository.findFavoritesByUserIdAndSourceType(userId, SourceType.WISHLIST).stream()
68+
return wardrobeClothesRepository.findFavoritesByUserIdAndOwnershipStatus(userId, OwnershipStatus.WISHLIST).stream()
6969
.map(entry -> ClothesResponse.from(entry.getClothes(), entry))
7070
.toList();
7171
}
@@ -226,7 +226,7 @@ public void deleteClothes(Long clothesId) {
226226
}
227227

228228
private Clothes getClothesWithDetails(Long clothesId) {
229-
return clothesRepository.findByIdWithDetails(clothesId)
229+
return clothesRepository.findById(clothesId)
230230
.orElseThrow(() -> new IllegalArgumentException("옷을 찾을 수 없습니다."));
231231
}
232232

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- =====================================================================
2+
-- 마이그레이션: clothes 테이블 분리 (color, wardrobe 관계 테이블 이전)
3+
-- 실행 전 반드시 백업 후 진행하세요.
4+
-- =====================================================================
5+
6+
-- 1. version 컬럼 추가 (낙관적 락)
7+
ALTER TABLE `clothes`
8+
ADD COLUMN `version` BIGINT NOT NULL DEFAULT 0;
9+
10+
-- 2. 기존 clothes.color → clothing_colors 백필
11+
-- (기존 테이블에 color VARCHAR 컬럼이 있었던 경우)
12+
-- INSERT INTO `clothing_colors` (clothes_id, color_code, color_role, sort_order, created_at, updated_at)
13+
-- SELECT id, color, 'PRIMARY', 0, NOW(), NOW()
14+
-- FROM `clothes`
15+
-- WHERE color IS NOT NULL;
16+
17+
-- 3. 기존 clothes.wardrobe_id → wardrobe_clothes 백필
18+
-- (기존 테이블에 wardrobe_id, is_favorite 컬럼이 있었던 경우)
19+
-- INSERT INTO `wardrobe_clothes`
20+
-- (wardrobe_id, clothes_id, ownership_status, size, season, favorite,
21+
-- registration_source, user_image_url, created_at, updated_at)
22+
-- SELECT
23+
-- wardrobe_id,
24+
-- id,
25+
-- source_type, -- 'OWNED' or 'WISHLIST'
26+
-- 'FREE', -- size 기본값 (실제 값으로 교체)
27+
-- NULL, -- season
28+
-- COALESCE(is_favorite, 0),
29+
-- 'MANUAL',
30+
-- image_url,
31+
-- NOW(),
32+
-- NOW()
33+
-- FROM `clothes`
34+
-- WHERE wardrobe_id IS NOT NULL;
35+
36+
-- 4. 이전 완료 후 기존 컬럼 제거
37+
-- (데이터 검증 후 실행)
38+
-- ALTER TABLE `clothes` DROP COLUMN `color`;
39+
-- ALTER TABLE `clothes` DROP COLUMN `wardrobe_id`;
40+
-- ALTER TABLE `clothes` DROP COLUMN `is_favorite`;

0 commit comments

Comments
 (0)