Skip to content

Commit

Permalink
Merge pull request #36 from AlongTheBlue/develop
Browse files Browse the repository at this point in the history
[Feat] Restaurant&Cafe 페이지네이션 작업중
  • Loading branch information
MoonInbae authored Oct 13, 2024
2 parents 23c880f + bb9a685 commit 1393d99
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Cafe {

@OneToMany(mappedBy = "cafe", cascade = CascadeType.ALL)
@JsonManagedReference
List<CafeImage> cafeImages;
List<CafeImage> images;

public Cafe(String contentId, String title, String addr, String xMap, String yMap) {
this.contentId = contentId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import org.alongtheblue.alongtheblue_server.global.common.response.ApiResponse;
import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.DetailResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.global.dto.response.HomeResponseDto;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;
Expand Down Expand Up @@ -47,8 +45,9 @@ public void saveCafesImages() {


@GetMapping("/detail/all")
public List<CafeDTO> getAll() {
return cafeService.getAll();
public ApiResponse<Page<CafeSimpleInformation>> retrieveAll(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
return cafeService.retrieveAll(page, size);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.alongtheblue.alongtheblue_server.global.data.cafe;

import org.alongtheblue.alongtheblue_server.global.data.restaurant.Restaurant;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantImage;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package org.alongtheblue.alongtheblue_server.global.data.cafe;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface CafeRepository extends JpaRepository<Cafe, Long> {
@Query("SELECT c.contentId AS contentId, c.title AS title, c.addr AS address, c.images AS images FROM Cafe c")
Page<CafeSimpleInformation> findAllSimple(Pageable pageable);

List<Cafe> findByTitleContaining(String keyword);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
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.accommodation.Accommodation;
import org.alongtheblue.alongtheblue_server.global.data.cafe.dto.PartCafeResponseDto;
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.RestaurantSimpleInformation;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantSimpleInformationImpl;
import org.alongtheblue.alongtheblue_server.global.data.weather.WeatherResponseDto;
import org.alongtheblue.alongtheblue_server.global.data.weather.WeatherService;
import org.alongtheblue.alongtheblue_server.global.gpt.OpenAIService;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -203,24 +205,46 @@ private Cafe parseCafe(JsonNode itemNode) {
return new Cafe(contentId, title, addr, x, y);
}

public List<CafeDTO> getAll() {
List<Cafe> cafes= cafeRepository.findAll();
CafeDTO dto= new CafeDTO();
List<CafeDTO> dtos= new ArrayList<>();
for(Cafe cafe: cafes){
List<CafeImage> imgs= cafeImageRepository.findBycafe(cafe);
List<String> urls= new ArrayList<>();
for(CafeImage img : imgs) urls.add(img.getOriginimgurl());
dto.setAddress(cafe.getAddr().substring(8));
dto.setContentid(cafe.getContentId());
dto.setTitle(cafe.getTitle());
dto.setImgUrls(urls);
dtos.add(dto);
dto.setIntroduction(cafe.getIntroduction());
dto.setInfoCenter(cafe.getInfoCenter());
dto.setRestDate(cafe.getRestDate());
}
return dtos;
public ApiResponse<Page<CafeSimpleInformation>> retrieveAll(int page, int size) {
Pageable pageable = PageRequest.of(page, size);

// 1. Cafe 기준으로 페이징 처리된 데이터를 조회
Page<CafeSimpleInformation> cafePage = cafeRepository.findAllSimple(pageable);

// 2. CafeSimpleInformation으로 변환하여 이미지 그룹화
List<CafeSimpleInformation> groupedCafeList = cafePage.getContent().stream()
.map(cafe -> new CafeSimpleInformationImpl(
cafe.getContentId(),
cafe.getTitle(),
cafe.getAddress(),
cafe.getImages() // 이미지를 그룹화하지 않고 그대로 넣음
))
.collect(Collectors.toList());

// 3. Restaurant 기준으로 페이징을 다시 적용하여 반환
Page<CafeSimpleInformation> pagedResult = new PageImpl<>(
groupedCafeList, pageable, cafePage.getTotalElements());

// 4. 결과를 ApiResponse로 반환
return ApiResponse.ok("카페 목록을 성공적으로 조회했습니다.", pagedResult);

// List<Cafe> cafes= cafeRepository.findAll();
// CafeDTO dto= new CafeDTO();
// List<CafeDTO> dtos= new ArrayList<>();
// for(Cafe cafe: cafes){
// List<CafeImage> imgs= cafeImageRepository.findBycafe(cafe);
// List<String> urls= new ArrayList<>();
// for(CafeImage img : imgs) urls.add(img.getOriginimgurl());
// dto.setAddress(cafe.getAddr().substring(8));
// dto.setContentid(cafe.getContentId());
// dto.setTitle(cafe.getTitle());
// dto.setImgUrls(urls);
// dtos.add(dto);
// dto.setIntroduction(cafe.getIntroduction());
// dto.setInfoCenter(cafe.getInfoCenter());
// dto.setRestDate(cafe.getRestDate());
// }
// return dtos;
}

public List<CafeDTO> homeCafe() {
Expand Down Expand Up @@ -395,7 +419,7 @@ public ApiResponse<List<PartCafeResponseDto>> getCafesHome() {
arr[0] + " " + arr[1],
cafe.getTitle(),
cafe.getContentId(),
cafe.getCafeImages().isEmpty() ? null : cafe.getCafeImages().get(0).getOriginimgurl(),
cafe.getImages().isEmpty() ? null : cafe.getImages().get(0).getOriginimgurl(),
cafe.getXMap(),
cafe.getYMap(),
"cafe"
Expand All @@ -415,7 +439,7 @@ public ApiResponse<List<PartCafeResponseDto>> getCafesByKeyword(String keyword)
arr[0] + " " + arr[1],
cafe.getTitle(),
cafe.getContentId(),
cafe.getCafeImages().isEmpty() ? null : cafe.getCafeImages().get(0).getOriginimgurl(),
cafe.getImages().isEmpty() ? null : cafe.getImages().get(0).getOriginimgurl(),
cafe.getXMap(),
cafe.getYMap(),
"cafe"
Expand All @@ -438,14 +462,14 @@ public ApiResponse<List<HomeResponseDto>> getHomeCafeList() {

// 이미지를 가진 레코드만 필터링하여 DTO로 변환
List<HomeResponseDto> filteredList = cafePage.getContent().stream()
.filter(cafe -> !cafe.getCafeImages().isEmpty()) // 이미지를 가진 레코드만 필터링
.filter(cafe -> !cafe.getImages().isEmpty()) // 이미지를 가진 레코드만 필터링
.map(cafe -> {
String[] arr = cafe.getAddr().substring(8).split(" ");
return new HomeResponseDto(
cafe.getContentId(),
cafe.getTitle(),
arr[0] + " " + arr[1],
cafe.getCafeImages().get(0).getOriginimgurl() // 첫 번째 이미지 가져오기
cafe.getImages().get(0).getOriginimgurl() // 첫 번째 이미지 가져오기
);
})
.toList();
Expand All @@ -470,7 +494,7 @@ public ApiResponse<DetailResponseDto> getCafeDetail(String id) {
weather.temperature(),
cafe.getInfoCenter(),
cafe.getIntroduction(),
cafe.getCafeImages().get(0).getOriginimgurl(),
cafe.getImages().get(0).getOriginimgurl(),
cafe.getXMap(),
cafe.getYMap()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.alongtheblue.alongtheblue_server.global.data.cafe;

import java.util.List;

public interface CafeSimpleInformation {
String getContentId();
String getTitle();
String getAddress();
List<CafeImage> getImages();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.alongtheblue.alongtheblue_server.global.data.cafe;

import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantImage;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantSimpleInformation;

import java.util.List;

public class CafeSimpleInformationImpl implements CafeSimpleInformation {

private String contentId;
private String title;
private String address;
private List<CafeImage> images;

public CafeSimpleInformationImpl(String contentId, String title, String address, List<CafeImage> images) {
this.contentId = contentId;
this.title = title;
this.address = address;
this.images = images;
}

@Override
public String getContentId() {
return contentId;
}

@Override
public String getTitle() {
return title;
}

@Override
public String getAddress() {
return address;
}

@Override
public List<CafeImage> getImages() {
return images;
}

public void addImages(List<CafeImage> newImages) {
this.images.addAll(newImages); // 기존 이미지 리스트에 새로운 이미지 추가
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Restaurant {

private String contentId;
private String title;
private String addr;
private String address;
@Column(columnDefinition = "TEXT")
private String introduction;
private String restDate;
Expand All @@ -29,10 +29,10 @@ public class Restaurant {
@JsonManagedReference
List<RestaurantImage> images;

public Restaurant(String contentId, String title, String addr, String xMap, String yMap) {
public Restaurant(String contentId, String title, String address, String xMap, String yMap) {
this.contentId = contentId;
this.title = title;
this.addr = addr;
this.address = address;
this.xMap = xMap;
this.yMap = yMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void saveRestaurantsImages() {

// TODO 페이지네이션 구현 필요
@GetMapping("/detail/all")
public ApiResponse<Page<RestaurantSimpleInformation>> getAll(@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size) {
public ApiResponse<Page<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 @@ -282,7 +282,7 @@ public ApiResponse<List<PartRestaurantResponseDto>> homerestaurant() {
continue;
}
Restaurant restaurant = optionalRestaurant.get();
String[] arr = restaurant.getAddr().substring(8).split(" ");
String[] arr = restaurant.getAddress().substring(8).split(" ");
// restaurant.setAddr(arr[0] + " " + arr[1]);
PartRestaurantResponseDto responseDto = new PartRestaurantResponseDto(
arr[0] + " " + arr[1],
Expand Down Expand Up @@ -404,7 +404,7 @@ public ApiResponse<Restaurant> getRestaurant(Long id) {
return ApiResponse.withError(ErrorCode.INVALID_RESTAURANT_ID);
}
Restaurant restaurant= optionalRestaurant.get();
restaurant.setAddr(restaurant.getAddr().substring(8));
restaurant.setAddress(restaurant.getAddress().substring(8));
return ApiResponse.ok("음식점 정보를 성공적으로 조회했습니다.", restaurant);
// RestaurantDTO dto= new RestaurantDTO();
// List<RestaurantImage> imgs= restaurantImageRepository.findByrestaurant(restaurant);
Expand All @@ -426,7 +426,7 @@ public ApiResponse<List<PartRestaurantResponseDto>> getRestaurantsByKeyword(Stri
List<Restaurant> optionalRestaurants = restaurantRepository.findByTitleContaining(keyword);
List<PartRestaurantResponseDto> partRestaurantResponseDtoList = new ArrayList<>();
for(Restaurant restaurant: optionalRestaurants) {
String[] arr = restaurant.getAddr().substring(8).split(" ");
String[] arr = restaurant.getAddress().substring(8).split(" ");
PartRestaurantResponseDto restaurantResponseDto = new PartRestaurantResponseDto(
arr[0] + " " + arr[1],
restaurant.getTitle(),
Expand Down Expand Up @@ -456,7 +456,7 @@ public ApiResponse<List<HomeResponseDto>> getHomeRestaurant() {
List<HomeResponseDto> filteredList = restaurantPage.getContent().stream()
.filter(restaurant -> !restaurant.getImages().isEmpty()) // 이미지를 가진 레코드만 필터링
.map(restaurant -> {
String[] arr = restaurant.getAddr().substring(8).split(" ");
String[] arr = restaurant.getAddress().substring(8).split(" ");
return new HomeResponseDto(
restaurant.getContentId(),
restaurant.getTitle(),
Expand All @@ -474,11 +474,11 @@ public ApiResponse<List<HomeResponseDto>> getHomeRestaurant() {

public ApiResponse<DetailResponseDto> getRestaurantDetail(String id) {
Restaurant restaurant = findByContentId(id);
WeatherResponseDto weather = weatherService.getWeatherByAddress(restaurant.getAddr());
WeatherResponseDto weather = weatherService.getWeatherByAddress(restaurant.getAddress());
DetailResponseDto detailResponseDto = new DetailResponseDto(
restaurant.getContentId(),
restaurant.getTitle(),
restaurant.getAddr(),
restaurant.getAddress(),
restaurant.getRestDate(),
weather.weatherCondition(),
weather.temperature(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import org.alongtheblue.alongtheblue_server.global.data.accommodation.Accommodation;
import org.alongtheblue.alongtheblue_server.global.data.accommodation.AccommodationRepository;
import org.alongtheblue.alongtheblue_server.global.data.alongBlues.BlueRepository;
import org.alongtheblue.alongtheblue_server.global.data.alongBlues.BlueService;
import org.alongtheblue.alongtheblue_server.global.data.blue.Blue;
import org.alongtheblue.alongtheblue_server.global.data.cafe.Cafe;
import org.alongtheblue.alongtheblue_server.global.data.cafe.CafeRepository;
import org.alongtheblue.alongtheblue_server.global.data.cafe.CafeService;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.Restaurant;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantRepository;
import org.alongtheblue.alongtheblue_server.global.data.restaurant.RestaurantService;
import org.alongtheblue.alongtheblue_server.global.data.tourData.TourData;
import org.alongtheblue.alongtheblue_server.global.data.tourData.TourDataRepository;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -81,7 +78,7 @@ private SearchResponseDto getSearchResponseDto(Restaurant restaurant) {
return new SearchResponseDto(
restaurant.getContentId(),
restaurant.getTitle(),
restaurant.getAddr(),
restaurant.getAddress(),
restaurant.getXMap(),
restaurant.getYMap(),
"restaurant"
Expand Down

0 comments on commit 1393d99

Please sign in to comment.