Skip to content

Commit

Permalink
#5 feat: 전시회 목록 조회 API-전체 목록, 카테고리 기반 조회 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JoongHyun-Kim committed Aug 3, 2023
1 parent d73bebd commit 1463199
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum BaseResponseStatus {

// exhibition(2200~2299)
INVALID_EXHIBITION_IDX(false, 2200, "잘못된 전시회 Idx 입니다."),
INVALID_CATEGORY(false, 2201, "잘못된 카테고리입니다."),

// comment(2300~2399)

Expand All @@ -32,6 +33,7 @@ public enum BaseResponseStatus {
// story(3100~3199)

// exhibition(3200~3299)
NULL_EXHIBITION(false, 3200, "전시회가 없습니다."),

// comment(3300~3399)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import com.umc.place.common.BaseException;
import com.umc.place.common.BaseResponse;
import com.umc.place.exhibition.dto.GetExhibitionDetailRes;
import com.umc.place.exhibition.dto.GetExhibitionsRes;
import com.umc.place.exhibition.service.ExhibitionService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import static com.umc.place.common.BaseResponseStatus.SUCCESS;
Expand All @@ -15,6 +19,24 @@
public class ExhibitionController {
private final ExhibitionService exhibitionService;

/**
* [GET]전시회 목록 조회
*/
@ResponseBody
@GetMapping("")
public BaseResponse<Page<GetExhibitionsRes>> getExhibitions(@RequestParam(required = false) String categoryName,
@PageableDefault(size = 20) Pageable page) // request param "sort="
{
try {
if (categoryName.isBlank()) {
return new BaseResponse<>(exhibitionService.getExhibitions(page)); // 전체 목록 조회 .getContent() 추가 시 content만 가져오기 가능
} else return new BaseResponse<>(exhibitionService.getExhibitionsByCategory(categoryName, page)); // 카테고리 기반 조회

} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

/**
* [GET] 전시회 상세 조회
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.place.exhibition.dto;

import lombok.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GetExhibitionsRes {
private Long exhibitionIdx;
private String exhibitionName;
private String exhibitionImg;
private String location;
}
27 changes: 17 additions & 10 deletions place/src/main/java/com/umc/place/exhibition/entity/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

import lombok.Getter;

import java.util.Arrays;

@Getter
public enum Category {
DESIGN(1, "디자인"),
INTERACTIVE(2, "체험형"),
PHOTO(3, "사진"),
INSTALLATION(4, "설치미술"),
ETC(5, "기타");
DESIGN("디자인"),
INTERACTIVE("체험형"),
PHOTO("사진"),
INSTALLATION("설치미술"),
ETC("기타");

private final int num;
private final String category;
private final String categoryName;

Category(int num, String category) {
this.num = num;
this.category = category;
Category(String categoryName) {
this.categoryName = categoryName;
}

// public static Category getCategoryByCategoryName(String categoryInput){
// return Arrays.stream(Category.values())
// .filter(category -> category.getCategoryName().equals(categoryInput))
// .findAny()
// .orElse(null);
// }
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.umc.place.exhibition.repository;

import com.umc.place.exhibition.entity.Category;
import com.umc.place.exhibition.entity.Exhibition;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ExhibitionRepository extends JpaRepository<Exhibition, Long> {
Page<Exhibition> findByCategory(Category category, Pageable pageable); // 카테고리 기반 전체 조회(페이징)
Page<Exhibition> findAll(Pageable pageable); // 전체 조회(페이징)
Boolean existsByCategory(Category category);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.umc.place.common.BaseException;
import com.umc.place.exhibition.dto.GetExhibitionDetailRes;
import com.umc.place.exhibition.dto.GetExhibitionsRes;
import com.umc.place.exhibition.entity.Category;
import com.umc.place.exhibition.entity.Exhibition;
import com.umc.place.exhibition.entity.ExhibitionLike;
import com.umc.place.exhibition.repository.ExhibitionRepository;
Expand All @@ -12,6 +14,8 @@
import com.umc.place.user.entity.User;
import com.umc.place.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -31,6 +35,52 @@ public class ExhibitionService {
private final UserRepository userRepository;
private final ExhibitionLikeRepository exhibitionLikeRepository;

/**
*전시회 전체 목록 조회(최신순,조회수순,좋아요수순 정렬)
*@paramuserIdx
*@return
*@throwsBaseException
*/
public Page<GetExhibitionsRes> getExhibitions(Pageable page) throws BaseException {
try {
return exhibitionRepository.findAll(page).map(exhibition -> new GetExhibitionsRes(exhibition.getExhibitionIdx(),
exhibition.getExhibitionName(), exhibition.getExhibitionImg(), exhibition.getLocation()));
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}

/**
*카테고리 기반 전시회 목록 조회(최신순,조회수순,좋아요수순 정렬)
*@paramcategoryIdx
*@paramuserIdx
*@return
*@throwsBaseException
*/
public Page<GetExhibitionsRes> getExhibitionsByCategory(String categoryName, Pageable page) throws BaseException {
try {
//Category category = Category.getCategoryByCategoryName(categoryName);
Category category = Category.valueOf(categoryName);
if (category != null) {
boolean exhibitionExists = exhibitionRepository.existsByCategory(category);
System.out.println("categoryName = " + categoryName);
if (exhibitionExists) {
Page<Exhibition> exhibitionPage = exhibitionRepository.findByCategory(category, page);
return exhibitionPage.map(exhibition -> new GetExhibitionsRes(
exhibition.getExhibitionIdx(),
exhibition.getExhibitionName(),
exhibition.getExhibitionImg(),
exhibition.getLocation()));
} else throw new BaseException(NULL_EXHIBITION);
} else throw new BaseException(INVALID_CATEGORY);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}
}


/**
* 전시회 상세 조회
* @param exhibitionIdx
Expand Down

0 comments on commit 1463199

Please sign in to comment.