Skip to content

Commit 75c2c45

Browse files
committed
feat: 옷 착용 통계 조회 추가
1 parent 4c24c08 commit 75c2c45

File tree

6 files changed

+61
-0
lines changed

6 files changed

+61
-0
lines changed

src/main/java/com/cloop/cloop/clothes/controller/ClothController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,13 @@ public ResponseEntity<?> donateCloth(@PathVariable Long clothId,
8181

8282
return ResponseEntity.ok(clothService.markAsDonated(clothId));
8383
}
84+
85+
@Operation(summary = "옷 착용 통계", description = "전체 옷 착용 통계를 반환합니다")
86+
@GetMapping("/statistics")
87+
public ResponseEntity<?> getClothStatistics(@AuthenticationPrincipal Long userId) {
88+
User user = userRepository.findByUserId(userId)
89+
.orElseThrow(() -> new RuntimeException("유저를 찾을 수 없습니다."));
90+
return ResponseEntity.ok(clothService.getClothStatistics(user));
91+
}
92+
8493
}

src/main/java/com/cloop/cloop/clothes/domain/Cloth.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class Cloth {
5555

5656
@Column(nullable = false)
5757
private String imageUrl;
58+
59+
@Column(nullable = false)
60+
private int wearCount = 0;
5861
// LookCloth 중간 테이블 설정
5962
@OneToMany(mappedBy = "cloth", cascade = CascadeType.ALL, orphanRemoval = true)
6063
private List<LookCloth> lookClothList = new ArrayList<>();

src/main/java/com/cloop/cloop/clothes/dto/ClothResponse.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ public class ClothResponse {
1818
private Boolean donated;
1919
private String imageUrl;
2020
private LocalDate lastWornAt;
21+
private int wearCount;
2122
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.cloop.cloop.clothes.dto;
2+
3+
import lombok.*;
4+
5+
import java.time.LocalDate;
6+
7+
@Getter
8+
@Setter
9+
@Builder
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class ClothStatisticsResponse {
13+
private Long clothId;
14+
private String clothName;
15+
private long wearCount;
16+
private LocalDate lastWornAt;
17+
}

src/main/java/com/cloop/cloop/clothes/service/ClothService.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.cloop.cloop.clothes.dto.*;
99
import com.cloop.cloop.clothes.repository.ClothRepository;
1010
import com.cloop.cloop.clothes.repository.DonationRepository;
11+
import com.cloop.cloop.looks.domain.LookCloth;
1112
import com.fasterxml.jackson.databind.JsonNode;
1213
import com.fasterxml.jackson.databind.ObjectMapper;
1314
import lombok.RequiredArgsConstructor;
@@ -191,6 +192,7 @@ public List<ClothResponse> getAllClothes(User user) {
191192
.season(cloth.getSeason().name())
192193
.donated(cloth.getDonated())
193194
.imageUrl(cloth.getImageUrl())
195+
.wearCount(cloth.getWearCount())
194196
.lastWornAt(cloth.getLastWornAt())
195197
.build())
196198
.toList();
@@ -241,4 +243,28 @@ public Map<String, Object> markAsDonated(Long clothId) {
241243
)
242244
);
243245
}
246+
// 옷 착용 통게
247+
public List<ClothStatisticsResponse> getClothStatistics(User user) {
248+
List<Cloth> clothes = clothRepository.findByUser(user);
249+
250+
return clothes.stream()
251+
.map(cloth -> {
252+
List<LookCloth> lookClothList = cloth.getLookClothList();
253+
254+
long wearCount = lookClothList.size();
255+
LocalDate lastWornAt = lookClothList.stream()
256+
.map(lc -> lc.getLook().getCreatedAt())
257+
.max(LocalDate::compareTo)
258+
.orElse(null);
259+
260+
return ClothStatisticsResponse.builder()
261+
.clothId(cloth.getClothId())
262+
.clothName(cloth.getClothName())
263+
.wearCount(wearCount)
264+
.lastWornAt(lastWornAt)
265+
.build();
266+
})
267+
.toList();
268+
}
269+
244270
}

src/main/java/com/cloop/cloop/looks/service/LookService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ public LookResponseDto createLook(LookRequestDto lookRequestDto, String token) {
5656
if (selectedCloths.isEmpty()) {
5757
throw new IllegalArgumentException("선택된 옷이 사용자 목록에 존재하지 않습니다.");
5858
}
59+
// cloth 착용 통계 업데이트
60+
selectedCloths.forEach(cloth -> {
61+
cloth.setLastWornAt(LocalDate.now());
62+
cloth.setWearCount(cloth.getWearCount() + 1);
63+
});
5964

6065
// LookCloth 생성 (Builder 패턴 사용)
6166
List<LookCloth> lookClothList = selectedCloths.stream()

0 commit comments

Comments
 (0)