Skip to content

Commit

Permalink
[#17] feat: 메인페이지 가져오기 api
Browse files Browse the repository at this point in the history
  • Loading branch information
mmihye committed Jun 2, 2024
1 parent 85ff0aa commit 5596f09
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Journey.Together.domain.place.controller;

import Journey.Together.domain.member.dto.LoginReq;
import Journey.Together.domain.member.dto.MemberRes;
import Journey.Together.domain.member.service.MemberService;
import Journey.Together.domain.place.dto.response.MainRes;
import Journey.Together.domain.place.service.PlaceService;
import Journey.Together.global.common.ApiResponse;
import Journey.Together.global.exception.Success;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/place")
@Tag(name = "Place", description = "여행지 정보 API")
public class PlaceController {

private final PlaceService placeService;

@GetMapping("/main")
public ApiResponse<MainRes> getMain(@RequestHeader("Authorization") String accesstoken,
@RequestParam String areacode, @RequestParam String sigungucode) {
return ApiResponse.success(Success.GET_MAIN_SUCCESS, placeService.getMainPage(areacode, sigungucode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Journey.Together.domain.place.dto.response;

import java.util.List;

public record MainRes(
List<PlaceRes> recommendPlaceList,
List<PlaceRes> aroundPlaceList
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package Journey.Together.domain.place.dto.response;

import Journey.Together.domain.place.entity.Place;

import java.util.List;

public record PlaceRes(
Long placeId,
String name,
String image,
List<String> disability,
String address
) {
public static PlaceRes of(Place place, List<String> disability){

return new PlaceRes(place.getId(), place.getName(), place.getFirstImg(), disability,place.getAddress());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class DisabilityCategory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class DisabilityPlaceCategory {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class DisabilitySubCategory {
@Id
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/Journey/Together/domain/place/entity/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

@Entity
@Getter
@Table(name = "place")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Place {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Journey.Together.domain.place.repository;

import Journey.Together.domain.place.entity.DisabilityPlaceCategory;
import Journey.Together.domain.place.entity.Place;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface DisabilityPlaceCategoryRepository extends JpaRepository<DisabilityPlaceCategory, Long> {

List<DisabilityPlaceCategory> findAllByPlace(Place place);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Journey.Together.domain.place.repository;


import Journey.Together.domain.place.entity.Place;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PlaceRepository extends JpaRepository<Place, Long> {

@Query(value = "SELECT * FROM place p ORDER BY RAND() LIMIT :count", nativeQuery = true)
List<Place> findRandomProducts(@Param("count") int count);

@Query(value = "SELECT * FROM place p WHERE area_code = :areacode AND sigungu_code = :sigungucode " +
"ORDER BY RAND() LIMIT :count", nativeQuery = true)
List<Place> findAroundProducts(@Param("areacode") String areacode, @Param("sigungucode") String sigungucode,
@Param("count") int count);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package Journey.Together.domain.place.service;

import Journey.Together.domain.place.dto.response.MainRes;
import Journey.Together.domain.place.dto.response.PlaceRes;
import Journey.Together.domain.place.entity.DisabilityPlaceCategory;
import Journey.Together.domain.place.entity.Place;
import Journey.Together.domain.place.repository.DisabilityPlaceCategoryRepository;
import Journey.Together.domain.place.repository.PlaceRepository;
import Journey.Together.global.common.ApiResponse;
import Journey.Together.global.exception.Success;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import software.amazon.awssdk.services.s3.endpoints.internal.Value;

import java.util.*;

@Service
@RequiredArgsConstructor
public class PlaceService {
private final PlaceRepository placeRepository;
private final DisabilityPlaceCategoryRepository disabilityPlaceCategoryRepository;
private final Integer recommnedPlaceNum = 4;
private final Integer aroundPlaceNum = 2;

// 메인페이지 가져오기
public MainRes getMainPage(String areacode, String sigungucode){

List<Place> recommondPlaces = placeRepository.findRandomProducts(recommnedPlaceNum);
List<Place> aroundPlaces = placeRepository.findAroundProducts(areacode, sigungucode, aroundPlaceNum);


return new MainRes(getPlaceRes(recommondPlaces), getPlaceRes(aroundPlaces));
}

private List<PlaceRes> getPlaceRes(List<Place> list){
List<PlaceRes> placeList = new ArrayList<>();

for(Place place : list){
Set<String> disability = new HashSet<>();
disabilityPlaceCategoryRepository.findAllByPlace(place)
.forEach(disabilityPlaceCategory -> {
disability.add(disabilityPlaceCategory.getSubCategory().getCategory().getId().toString());
});
placeList.add(PlaceRes.of(place, new ArrayList<>(disability)));
}

return placeList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum Success {
/**
* 200 OK
*/
GET_USER_MAIN_SUCCESS(HttpStatus.OK, "메인 페이지 유저 정보 조회 성공"),
GET_MAIN_SUCCESS(HttpStatus.OK, "메인 페이지 정보 조회 성공"),
GET_MYPAGE_SUCCESS(HttpStatus.OK, "마이 페이지 조회 성공"),
GET_LINKS_SUCCESS(HttpStatus.OK, "이주의 링크 조회 성공"),
GET_SITES_SUCCESS(HttpStatus.OK, "추천 사이트 조회 성공"),
Expand Down

0 comments on commit 5596f09

Please sign in to comment.