Skip to content

Commit 60dab1f

Browse files
authored
Merge pull request #52 from JocketDan/refactor/review-logic
Refactor/review logic
2 parents 5f89193 + bb92405 commit 60dab1f

27 files changed

Lines changed: 532 additions & 112 deletions

review/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ dependencies {
4747
// Jackson
4848
implementation 'com.fasterxml.jackson.core:jackson-databind'
4949
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
50-
51-
implementation project(':util')
5250
}
5351

5452
tasks.named('test') {

review/src/main/java/com/jocketdan/review/config/CorsConfig.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.jocketdan.review.config;
2+
3+
import com.jocketdan.review.kafka.OpenApiInfoResponseEvent;
4+
import org.apache.kafka.clients.consumer.ConsumerConfig;
5+
import org.apache.kafka.common.serialization.StringDeserializer;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.context.annotation.Bean;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
10+
import org.springframework.kafka.core.ConsumerFactory;
11+
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
12+
import org.springframework.kafka.support.serializer.JsonDeserializer;
13+
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
17+
@Configuration
18+
public class KafkaConsumerConfig {
19+
20+
@Value("${spring.kafka.bootstrap-servers}")
21+
private String bootstrapServers;
22+
23+
@Bean
24+
public ConsumerFactory<String, OpenApiInfoResponseEvent> openApiInfoResponseConsumerFactory() {
25+
Map<String, Object> config = new HashMap<>();
26+
config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
27+
config.put(ConsumerConfig.GROUP_ID_CONFIG, "review-service-group");
28+
config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
29+
config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
30+
config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
31+
32+
config.put(JsonDeserializer.TRUSTED_PACKAGES, "*");
33+
config.put(JsonDeserializer.VALUE_DEFAULT_TYPE, OpenApiInfoResponseEvent.class.getName());
34+
config.put(JsonDeserializer.USE_TYPE_INFO_HEADERS, false);
35+
36+
return new DefaultKafkaConsumerFactory<>(config);
37+
}
38+
39+
@Bean
40+
public ConcurrentKafkaListenerContainerFactory<String, OpenApiInfoResponseEvent>
41+
openApiInfoResponseKafkaListenerContainerFactory() {
42+
ConcurrentKafkaListenerContainerFactory<String, OpenApiInfoResponseEvent> factory =
43+
new ConcurrentKafkaListenerContainerFactory<>();
44+
factory.setConsumerFactory(openApiInfoResponseConsumerFactory());
45+
return factory;
46+
}
47+
}

review/src/main/java/com/jocketdan/review/config/KafkaProducerConfig.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jocketdan.review.config;
22

33
import com.jocketdan.review.dto.ReviewRatingEvent;
4+
import com.jocketdan.review.kafka.OpenApiInfoRequestEvent;
45
import org.apache.kafka.clients.producer.ProducerConfig;
56
import org.apache.kafka.common.serialization.StringSerializer;
67
import org.springframework.beans.factory.annotation.Value;
@@ -21,7 +22,7 @@ public class KafkaProducerConfig {
2122
private String bootstrapServers;
2223

2324
@Bean
24-
public ProducerFactory<String, ReviewRatingEvent> producerFactory() {
25+
public ProducerFactory<String, ReviewRatingEvent> reviewRatingProducerFactory() {
2526
Map<String, Object> config = new HashMap<>();
2627
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
2728
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
@@ -30,7 +31,21 @@ public ProducerFactory<String, ReviewRatingEvent> producerFactory() {
3031
}
3132

3233
@Bean
33-
public KafkaTemplate<String, ReviewRatingEvent> kafkaTemplate() {
34-
return new KafkaTemplate<>(producerFactory());
34+
public KafkaTemplate<String, ReviewRatingEvent> reviewRatingKafkaTemplate() {
35+
return new KafkaTemplate<>(reviewRatingProducerFactory());
36+
}
37+
38+
@Bean
39+
public ProducerFactory<String, OpenApiInfoRequestEvent> openApiInfoRequestProducerFactory() {
40+
Map<String, Object> config = new HashMap<>();
41+
config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
42+
config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
43+
config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
44+
return new DefaultKafkaProducerFactory<>(config);
45+
}
46+
47+
@Bean
48+
public KafkaTemplate<String, OpenApiInfoRequestEvent> openApiInfoRequestKafkaTemplate() {
49+
return new KafkaTemplate<>(openApiInfoRequestProducerFactory());
3550
}
3651
}

review/src/main/java/com/jocketdan/review/controller/ReviewController.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,17 @@ public class ReviewController {
4545
```
4646
"""
4747
)
48-
@PostMapping(value = "/{placeId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
48+
@PostMapping(value = "/{contentId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
4949
public ResponseEntity<ReviewResponseDTO> createReview(
50-
@PathVariable Long placeId,
50+
@PathVariable String contentId,
5151
@RequestPart("dto") String reviewDtoString,
5252
@RequestPart(value = "images", required = false) List<MultipartFile> images,
5353
Authentication authentication) throws JsonProcessingException {
5454

5555
ReviewRequestDTO dto = objectMapper.readValue(reviewDtoString, ReviewRequestDTO.class);
5656

5757
String email = authentication.getName();
58-
ReviewResponseDTO response = reviewService.createReview(dto, images, placeId, email);
58+
ReviewResponseDTO response = reviewService.createReview(dto, images, contentId, email);
5959
return ResponseEntity.status(HttpStatus.CREATED).body(response);
6060
}
6161

@@ -77,9 +77,9 @@ public ResponseEntity<ReviewResponseDTO> createReview(
7777
- **R**: 별점 높은 순
7878
"""
7979
)
80-
@GetMapping("/{placeId}")
80+
@GetMapping("/{contentId}")
8181
public ResponseEntity<ContentReviewResponseDTO> getReviewsByPlaceId(
82-
@PathVariable Long placeId,
82+
@PathVariable String contentId,
8383
@RequestParam(defaultValue = "10") int numOfRows,
8484
@RequestParam(defaultValue = "1") int pageNo,
8585
@RequestParam(defaultValue = "C") String arrange,
@@ -88,7 +88,7 @@ public ResponseEntity<ContentReviewResponseDTO> getReviewsByPlaceId(
8888
Sort sort = createReviewSort(arrange);
8989
Pageable pageable = PageRequest.of(pageNo - 1, numOfRows, sort);
9090

91-
ContentReviewResponseDTO response = reviewService.readReviews(placeId, lang, pageable);
91+
ContentReviewResponseDTO response = reviewService.readReviews(contentId, lang, pageable);
9292
return ResponseEntity.ok(response);
9393
}
9494

review/src/main/java/com/jocketdan/review/dto/ContentReviewResponseDTO.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
public class ContentReviewResponseDTO {
1313

1414
PaginatedResponseDTO<ReviewResponseDTO> reviews;
15-
Long placeId;
15+
String contentId;
1616
double averageRating;
1717

18-
public ContentReviewResponseDTO(Page<ReviewResponseDTO> reviewPage, Long placeId, double averageRating) {
18+
public ContentReviewResponseDTO(Page<ReviewResponseDTO> reviewPage, String contentId, double averageRating) {
1919
this.reviews = PaginatedResponseDTO.of(reviewPage);
20-
this.placeId = placeId;
20+
this.contentId = contentId;
2121
this.averageRating = averageRating;
2222
}
2323
}

review/src/main/java/com/jocketdan/review/dto/ReviewRatingEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@AllArgsConstructor
1212
@FieldDefaults(level = AccessLevel.PRIVATE)
1313
public class ReviewRatingEvent {
14-
Long placeId;
14+
String contentId;
1515
Double averageRating;
1616
Integer reviewCount;
1717
}

review/src/main/java/com/jocketdan/review/dto/ReviewRequestDTO.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class ReviewRequestDTO {
1818
@Schema(description = "별점 (1-5점)", defaultValue = "0", example = "5")
1919
private int rating;
2020

21-
public Review toEntity(Long placeId, String email, List<String> imageUrls) {
22-
Review review = new Review(email, placeId, this.content, this.rating);
21+
public Review toEntity(String contentId, String email, List<String> imageUrls) {
22+
Review review = new Review(email, contentId, this.content, this.rating);
2323

2424
if (imageUrls != null && !imageUrls.isEmpty()) {
2525
List<ReviewImage> reviewImages = imageUrls.stream()

review/src/main/java/com/jocketdan/review/dto/ReviewWithPlaceResponseDTO.java

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.jocketdan.review.dto;
22

3+
import com.jocketdan.review.entity.OpenApiInfoCache;
34
import com.jocketdan.review.entity.Review;
45
import com.jocketdan.review.entity.ReviewImage;
56
import lombok.*;
@@ -15,17 +16,58 @@
1516
public class ReviewWithPlaceResponseDTO {
1617

1718
Long id;
18-
Long placeId;
1919
String email;
2020
String content;
2121
int rating;
2222
List<String> imageUrls;
2323

24-
public static ReviewWithPlaceResponseDTO from(Review review) {
24+
String contentId;
25+
String placeTitle;
26+
String placeImage;
27+
String contentTypeId;
28+
String addr1;
29+
String addr2;
30+
String areaCode;
31+
String sigunguCode;
32+
33+
public static ReviewWithPlaceResponseDTO from(Review review, OpenApiInfoCache placeInfo) {
2534
List<String> urls = review.getImages().stream()
2635
.map(ReviewImage::getImageUrl)
2736
.toList();
2837

29-
return new ReviewWithPlaceResponseDTO(review.getId(), review.getPlaceId(), review.getEmail(), review.getContent(), review.getRating(), urls);
38+
if (placeInfo == null) {
39+
return new ReviewWithPlaceResponseDTO(
40+
review.getId(),
41+
review.getEmail(),
42+
review.getContent(),
43+
review.getRating(),
44+
urls,
45+
"[삭제되거나 알 수 없는 장소]",
46+
"[삭제되거나 알 수 없는 장소]",
47+
null,
48+
null,
49+
null,
50+
null,
51+
null,
52+
null
53+
);
54+
}
55+
56+
return new ReviewWithPlaceResponseDTO(
57+
review.getId(),
58+
review.getEmail(),
59+
review.getContent(),
60+
review.getRating(),
61+
urls,
62+
placeInfo.getContentId(),
63+
placeInfo.getTitle(),
64+
placeInfo.getFirstImage(),
65+
placeInfo.getContentTypeId(),
66+
placeInfo.getAddr1(),
67+
placeInfo.getAddr2(),
68+
placeInfo.getAreaCode(),
69+
placeInfo.getSigunguCode()
70+
);
3071
}
72+
3173
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jocketdan.review.entity;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
import lombok.AccessLevel;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import lombok.experimental.FieldDefaults;
10+
11+
import java.time.LocalDateTime;
12+
13+
@Entity
14+
@Getter
15+
@NoArgsConstructor(access = AccessLevel.PROTECTED)
16+
@AllArgsConstructor
17+
@FieldDefaults(level = AccessLevel.PRIVATE)
18+
public class OpenApiInfoCache {
19+
20+
@Id
21+
String contentId;
22+
23+
String title;
24+
String firstImage;
25+
String contentTypeId;
26+
String addr1;
27+
String addr2;
28+
String areaCode;
29+
String sigunguCode;
30+
31+
LocalDateTime cachedAt;
32+
33+
public void update(String title, String firstImage, String contentTypeId, String addr1, String addr2, String areaCode, String sigunguCode) {
34+
this.title = title;
35+
this.firstImage = firstImage;
36+
this.contentTypeId = contentTypeId;
37+
this.addr1 = addr1;
38+
this.addr2 = addr2;
39+
this.areaCode = areaCode;
40+
this.sigunguCode = sigunguCode;
41+
this.cachedAt = LocalDateTime.now();
42+
}
43+
}

0 commit comments

Comments
 (0)