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 Jul 26, 2023
1 parent bf73aa1 commit 2e8075f
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 8 deletions.
23 changes: 22 additions & 1 deletion place/src/main/java/com/umc/place/common/BaseResponseStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,40 @@ public enum BaseResponseStatus {
/**
* 1000: 요청 성공
*/
SUCCESS(true, 1000, "요청에 성공하였습니다.");
SUCCESS(true, 1000, "요청에 성공하였습니다."),

/**
* 2000: Request 오류
*/
// user(2000~2099)
INVALID_USER_IDX(false, 2000, "잘못된 user Idx 입니다."),

// story(2100~2199)

// exhibition(2200~2299)
INVALID_EXHIBITION_IDX(false, 2200, "잘못된 전시회 Idx 입니다."),

// comment(2300~2399)



/**
* 3000: Response 오류
*/
// user(3000~3099)

// story(3100~3199)

// exhibition(3200~3299)

// comment(3300~3399)



/**
* 4000: DB, Server 오류
*/
DATABASE_ERROR(false, 4000, "데이터베이스 연결에 실패했습니다.");

private final boolean isSuccess;
private final int code;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package com.umc.place.exhibition.controller;

import com.umc.place.common.BaseException;
import com.umc.place.common.BaseResponse;
import com.umc.place.exhibition.dto.GetExhibitionDetailRes;
import com.umc.place.exhibition.service.ExhibitionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/exhibitions")
@RequiredArgsConstructor
public class ExhibitionController {
private final ExhibitionService exhibitionService;

/**
* [GET] 전시회 상세 조회
*/
@ResponseBody
@GetMapping("/{exhibitionIdx}/{userIdx}")
public BaseResponse<GetExhibitionDetailRes> getExhibitionDetail(@PathVariable("exhibitionIdx") Long exhibitionIdx, @PathVariable Long userIdx) { // TODO: authService 생성 후 userIdx 삭제
try {
return new BaseResponse<>(exhibitionService.getExhibitionDetail(exhibitionIdx, userIdx)); // TODO: userIdx -> authService.getUserIdx() 수정
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.umc.place.exhibition.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class GetExhibitionDetailRes {
private String exhibitionName;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime startDate;
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul")
private LocalDateTime endDate;
private String exhibitionImg;
private String operatingTime;
private String location;
private Integer fee;
private String artist;
private List<Story> stories;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public static class Story {
private Long storyIdx;
private String storyImg;
private String nickname;
private String userImg;
private Boolean heart;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
package com.umc.place.exhibition.entity;

import lombok.Getter;

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

private final int num;
private final String category;

Category(int num, String category) {
this.num = num;
this.category = category;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Entity
@Getter
@NoArgsConstructor
//@DynamicInsert
@DynamicInsert
public class Exhibition extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -36,7 +36,7 @@ public class Exhibition extends BaseEntity {
private String location;

@Column
private Long fee;
private Integer fee;

@Column(nullable = false)
private String artist;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,77 @@
package com.umc.place.exhibition.service;

import com.umc.place.common.BaseException;
import com.umc.place.exhibition.dto.GetExhibitionDetailRes;
import com.umc.place.exhibition.entity.Exhibition;
import com.umc.place.exhibition.repository.ExhibitionRepository;
import com.umc.place.story.entity.Story;
import com.umc.place.story.repository.StoryRepository;
import com.umc.place.storyLike.repository.StoryLikeRepository;
import com.umc.place.user.entity.User;
import com.umc.place.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.umc.place.common.BaseResponseStatus.*;

@Service
@RequiredArgsConstructor
public class ExhibitionService {
private final ExhibitionRepository exhibitionRepository;
private final StoryRepository storyRepository;
private final StoryLikeRepository storyLikeRepository;
private final UserRepository userRepository;

/**
* 전시회 상세 조회
* @param exhibitionIdx
* @return GetExhibitionDetailRes
* @throws BaseException
*/
public GetExhibitionDetailRes getExhibitionDetail(Long exhibitionIdx, Long userIdx) throws BaseException{
try {
Exhibition exhibition = exhibitionRepository.findById(exhibitionIdx).orElseThrow(() -> new BaseException(INVALID_EXHIBITION_IDX));

List<GetExhibitionDetailRes.Story> storyList;
if (userRepository.existsById(userIdx)) {
User user = userRepository.findById(userIdx).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
//List<GetExhibitionDetailRes.Story> storyList = getStoryList(exhibition, user);
storyList = getStoryList(exhibition, user);
} else {
storyList = getStoryListAnonymous(exhibition);
}

// TODO: S3 설정 완료 후 이미지 가져오는 부분 수정 필요
return new GetExhibitionDetailRes(exhibition.getExhibitionName(), exhibition.getStartDate(), exhibition.getEndDate(),
exhibition.getExhibitionImg(), exhibition.getOperatingTime(), exhibition.getLocation(), exhibition.getFee(), exhibition.getArtist(), storyList);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(DATABASE_ERROR);
}

}

// 회원) 스토리 조회
private List<GetExhibitionDetailRes.Story> getStoryList(Exhibition exhibition, User user) {
return storyRepository.findFirst2ByExhibition(exhibition).stream()
.map(story -> new GetExhibitionDetailRes.Story(story.getStoryIdx(), story.getStoryImg(), story.getUser().getNickname(),
story.getUser().getUserImg(), getLike(user, story))).collect(Collectors.toList());
}

private Boolean getLike(User user, Story story) {
// 좋아요 눌렀으면 True 아니면 False
return storyLikeRepository.existsByUserAndStory(user, story);
}

// 비회원) 스토리 조회
private List<GetExhibitionDetailRes.Story> getStoryListAnonymous(Exhibition exhibition) {
return storyRepository.findFirst2ByExhibition(exhibition).stream()
.map(story -> new GetExhibitionDetailRes.Story(story.getStoryIdx(), story.getStoryImg(), story.getUser().getNickname(),
story.getUser().getUserImg(), Boolean.FALSE)).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Getter
@NoArgsConstructor
@DynamicInsert
@Table(name = "exhibitionLikes")
public class ExhibitionLike extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.umc.place.story.repository;

import com.umc.place.exhibition.entity.Exhibition;
import com.umc.place.story.entity.Story;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface StoryRepository extends JpaRepository<Story, Long> {
List<Story> findFirst2ByExhibition(Exhibition exhibition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
@Getter
@NoArgsConstructor
@DynamicInsert
@Table(name = "storyLikes")
public class StoryLike extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.umc.place.storyLike.repository;

import com.umc.place.story.entity.Story;
import com.umc.place.storyLike.entity.StoryLike;
import com.umc.place.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StoryLikeRepository extends JpaRepository<StoryLike, Long> {
Boolean existsByUserAndStory(User user, Story story);
}

0 comments on commit 2e8075f

Please sign in to comment.