Skip to content

Commit

Permalink
Merge pull request #39 from AlongTheBlue/develop
Browse files Browse the repository at this point in the history
[Feat] CustomPage 사용해서 category 정보 추가
  • Loading branch information
MoonInbae authored Oct 13, 2024
2 parents 0243017 + c29764e commit 5fd4222
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 5 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ dependencies {
implementation 'org.json:json:20240303'
testImplementation 'io.projectreactor:reactor-test'
implementation 'io.netty:netty-resolver-dns-native-macos:4.1.86.Final:osx-x86_64'

// Spring Boot Jackson 의존성 추가
implementation 'org.springframework.boot:spring-boot-starter-json'

implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.2'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.15.2'
implementation 'com.fasterxml.jackson.core:jackson-core:2.15.2'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.alongtheblue.alongtheblue_server.global.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.alongtheblue.alongtheblue_server.global.data.global.CustomPage;
import org.alongtheblue.alongtheblue_server.global.data.global.CustomPageMixIn;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
// MixIn 등록
mapper.addMixIn(CustomPage.class, CustomPageMixIn.class);
return mapper;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.alongtheblue.alongtheblue_server.global.data.global;

public enum Category {
RESTAURANT("restaurant"),
CAFE("cafe"),
ACCOMMODATION("accommodation"),
TOURDATA("tourData");

private final String value;

Category(String value) {
this.value = value;
}

public String getValue() {
return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.alongtheblue.alongtheblue_server.global.data.global;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.List;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class CustomPage<T> extends PageImpl<T> {
@JsonProperty("category")
private String category;

public CustomPage() {
super(List.of());
}
public CustomPage(List<T> content, Pageable pageable, long total, String category) {
super(content, pageable, total);
this.category = category;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.alongtheblue.alongtheblue_server.global.data.global;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.List;

public abstract class CustomPageMixIn<T> extends PageImpl<T> {
@JsonProperty("category")
private String category;

public CustomPageMixIn(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;

Expand All @@ -26,6 +27,7 @@ public class Restaurant {
private String yMap;

@OneToMany(mappedBy = "restaurant", cascade = CascadeType.ALL)
@ToString.Exclude
@JsonManagedReference
List<RestaurantImage> images;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.RequiredArgsConstructor;
import org.alongtheblue.alongtheblue_server.global.common.response.ApiResponse;
import org.alongtheblue.alongtheblue_server.global.data.global.CustomPage;
import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.DetailResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.HomeResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.dto.response.PartRestaurantResponseDto;
Expand Down Expand Up @@ -44,8 +45,8 @@ public void saveRestaurantsImages() {

// TODO 페이지네이션 구현 필요
@GetMapping("/detail/all")
public ApiResponse<Page<RestaurantSimpleInformation>> retrieveAll(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
public ApiResponse<CustomPage<RestaurantSimpleInformation>> retrieveAll(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
return restaurantService.retrieveAll(page, size);
}
// public ApiResponse<List<RestaurantResponseDto>> getAll(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Entity
@Data
Expand All @@ -17,6 +18,7 @@ public class RestaurantImage {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "restaurant")
@ToString.Exclude
@JsonBackReference
private Restaurant restaurant;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.alongtheblue.alongtheblue_server.global.common.response.ApiResponse;
import org.alongtheblue.alongtheblue_server.global.data.global.Category;
import org.alongtheblue.alongtheblue_server.global.data.global.CustomPage;
import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.DetailResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.HomeResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.dto.response.PartRestaurantResponseDto;
Expand Down Expand Up @@ -220,12 +222,23 @@ private Mono<Map<String, String>> restdayAndInfo(Restaurant restaurant) {
}

// //딱 음식 눌렀을 때 식당 목록
public ApiResponse<Page<RestaurantSimpleInformation>> retrieveAll(int page, int size) {
public ApiResponse<CustomPage<RestaurantSimpleInformation>> retrieveAll(int page, int size) {
Pageable pageable = PageRequest.of(page, size);

// 1. Restaurant 기준으로 페이징 처리된 데이터를 조회
Page<RestaurantSimpleInformation> restaurantPage = restaurantRepository.findAllSimple(pageable);

// CustomPage 객체로 변환 (기존 페이지네이션 정보와 category를 함께 담음)
CustomPage<RestaurantSimpleInformation> customPage = new CustomPage<>(
restaurantPage.getContent(), pageable, restaurantPage.getTotalElements(), Category.RESTAURANT.getValue());

// 로깅 추가: CustomPage의 상태 확인
System.out.println("CustomPage category: " + customPage.getCategory());

// ApiResponse로 반환
return ApiResponse.ok("음식점 목록을 성공적으로 조회했습니다.", customPage);
}

// Pageable pageable = PageRequest.of(page, size);
//
// // 1. Restaurant 기준으로 페이징 처리된 데이터를 조회
Expand Down Expand Up @@ -259,7 +272,7 @@ public ApiResponse<Page<RestaurantSimpleInformation>> retrieveAll(int page, int
// groupedRestaurantList, pageable, restaurantPage.getTotalElements());

// 4. 결과를 ApiResponse로 반환
return ApiResponse.ok("음식점 목록을 성공적으로 조회했습니다.", restaurantPage);
// return ApiResponse.ok("음식점 목록을 성공적으로 조회했습니다.", restaurantPage);
// 1. 전체 데이터에서 이미지를 가지고 있는 데이터만 조회
// Page<RestaurantSimpleInformation> restaurantPage = restaurantRepository.findAllSimple(pageable);
//
Expand All @@ -274,7 +287,7 @@ public ApiResponse<Page<RestaurantSimpleInformation>> retrieveAll(int page, int
//
// // 4. 결과를 ApiResponse로 반환
// return ApiResponse.ok("이미지가 있는 음식점 목록을 성공적으로 조회했습니다.", pagedResult);
}
// }
// public List<RestaurantResponseDto> getAll() {
// List<Restaurant> restaurants = restaurantRepository.findAll();
// List<RestaurantResponseDto> dtos = new ArrayList<>();
Expand Down

0 comments on commit 5fd4222

Please sign in to comment.