Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/11-getHome] 홈 API 수정 #48

Merged
merged 13 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.umc.place.home.controller;

import com.umc.place.common.BaseException;
import com.umc.place.common.BaseResponse;
import com.umc.place.home.dto.GetHomeUserLikeDetailRes;
import com.umc.place.home.dto.GetHomeUserRecentDetailRes;
import com.umc.place.home.dto.GetHomeUserViewDetailRes;
import com.umc.place.home.service.HomeService;
import com.umc.place.user.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -16,20 +18,37 @@
@RequestMapping("/home")
public class HomeController {
private final HomeService homeService;
private final AuthService authService;

@ResponseBody
@GetMapping("userRecent")
public BaseResponse<GetHomeUserRecentDetailRes> getHomeUserRecentDetail(){
return new BaseResponse<>(homeService.getHomeUserRecentDetail());
public BaseResponse<GetHomeUserRecentDetailRes> getHomeUserRecentDetail() throws BaseException {

if (authService.isMember()){ //로그인이 된 경우
Long userIdx = authService.getUserIdx(); // 로그인을 한 유저 id 찾기
return new BaseResponse<>(homeService.getHomeUserRecentDetail(userIdx));
}else{
return new BaseResponse<>(homeService.getHomeUserRecentDetail());
}
}
@ResponseBody
@GetMapping("userView")
public BaseResponse<GetHomeUserViewDetailRes> getHomeUserViewDetailRes(){
return new BaseResponse<>(homeService.getHomeUserViewDetailRes());
public BaseResponse<GetHomeUserViewDetailRes> getHomeUserViewDetailRes() throws BaseException {
if (authService.isMember()) { //로그인이 된 경우
Long userIdx = authService.getUserIdx(); // 로그인을 한 유저 id 찾기
return new BaseResponse<>(homeService.getHomeUserViewDetailRes(userIdx));
}else{
return new BaseResponse<>(homeService.getHomeUserViewDetailRes());
}
}
@ResponseBody
@GetMapping("userLike")
public BaseResponse<GetHomeUserLikeDetailRes> getHomeUserLikeDetailRes(){
return new BaseResponse<>(homeService.getHomeUserLikeDetailRes());
public BaseResponse<GetHomeUserLikeDetailRes> getHomeUserLikeDetailRes() throws BaseException {
if (authService.isMember()) { //로그인이 된 경우
Long userIdx = authService.getUserIdx(); // 로그인을 한 유저 id 찾기
return new BaseResponse<>(homeService.getHomeUserLikeDetailRes(userIdx));
}else{
return new BaseResponse<>(homeService.getHomeUserLikeDetailRes());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ public class GetHomeUserLikeDetailRes {
@Getter
public static class UserLikeList{
private String exhibitionImg;
private String exhibitionName;
private String userImg;
private String location;
private String artist;
private Boolean isLike;
private Boolean isLogin;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public class GetHomeUserRecentDetailRes {
@Setter
public static class UserRecentList{
private String exhibitionImg;
private String exhibitionName;
private String userImg;
private String location;
private String artist;
private Boolean isLike;
private Boolean isLogin;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public class GetHomeUserViewDetailRes {
@Setter
public static class UserViewList{
private String exhibitionImg;
private String exhibitionName;
private String userImg;
private String location;
private String artist;
private Boolean isLike;
private Boolean isLogin;
}

}
99 changes: 83 additions & 16 deletions place/src/main/java/com/umc/place/home/service/HomeService.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,109 @@
package com.umc.place.home.service;

import com.umc.place.common.BaseException;
import com.umc.place.exhibition.entity.Exhibition;
import com.umc.place.exhibition.repository.ExhibitionRepository;
import com.umc.place.home.dto.GetHomeUserLikeDetailRes;
import com.umc.place.home.dto.GetHomeUserRecentDetailRes;
import com.umc.place.home.dto.GetHomeUserViewDetailRes;
import com.umc.place.story.entity.Story;
import com.umc.place.story.repository.StoryLikeRepository;
import com.umc.place.story.repository.StoryRepository;
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.INVALID_USER_IDX;

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

public GetHomeUserRecentDetailRes getHomeUserRecentDetail() { //로그인을 안하고 들어온경우
List<Story> stories = storyRepository.findTop4ByOrderByCreatedDateDesc();
List<GetHomeUserRecentDetailRes.UserRecentList> getUserRecentList = stories.stream()
.map(userRecent -> new GetHomeUserRecentDetailRes.UserRecentList(userRecent.getExhibition().getExhibitionImg(),
userRecent.getUser().getUserImg(),
userRecent.getExhibition().getLocation(),
userRecent.getUser().getNickname(),
false,//하트를 보여주지않는다
false))// 로그인을 안한상태
.collect(Collectors.toList());
return new GetHomeUserRecentDetailRes(getUserRecentList);
}

public GetHomeUserRecentDetailRes getHomeUserRecentDetail(Long userId) throws BaseException { //로그인을 하고 들어온 경우
User user = userRepository.findById(userId).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
List<Story> stories = storyRepository.findTop4ByOrderByCreatedDateDesc();
List<GetHomeUserRecentDetailRes.UserRecentList> getUserRecentList = stories.stream()
.map(userRecent -> new GetHomeUserRecentDetailRes.UserRecentList(userRecent.getExhibition().getExhibitionImg(),
userRecent.getUser().getUserImg(),
userRecent.getExhibition().getLocation(),
userRecent.getUser().getNickname(),
storyLikeRepository.existsByUserAndStory(user,userRecent),
true)) // 하트를 눌렀는지 확인
.collect(Collectors.toList());
return new GetHomeUserRecentDetailRes(getUserRecentList);
}

public GetHomeUserRecentDetailRes getHomeUserRecentDetail() {
List<Exhibition> userRecentList = exhibitionRepository.findTop4ByOrderByCreatedDateDesc();
List<GetHomeUserRecentDetailRes.UserRecentList> getuserRecentList = userRecentList.stream()
.map(userRecent -> new GetHomeUserRecentDetailRes.UserRecentList(userRecent.getExhibitionImg(), userRecent.getExhibitionName(), userRecent.getLocation(), userRecent.getArtist()))
public GetHomeUserViewDetailRes getHomeUserViewDetailRes() { //로그인을 안하고 들어온경우
List<Story> userViewList = storyRepository.findTop4ByOrderByViewCountDesc();
List<GetHomeUserViewDetailRes.UserViewList> getUserViewList = userViewList.stream()
.map(userView -> new GetHomeUserViewDetailRes.UserViewList(userView.getExhibition().getExhibitionImg(),
userView.getUser().getUserImg(),
userView.getExhibition().getLocation(),
userView.getUser().getNickname(),
false,
false))
.collect(Collectors.toList());
return new GetHomeUserRecentDetailRes(getuserRecentList);
return new GetHomeUserViewDetailRes(getUserViewList);
}

public GetHomeUserViewDetailRes getHomeUserViewDetailRes() {
List<Exhibition> userViewList = exhibitionRepository.findTop4ByOrderByViewCountDesc();
List<GetHomeUserViewDetailRes.UserViewList> getuserViewList = userViewList.stream()
.map(userView -> new GetHomeUserViewDetailRes.UserViewList(userView.getExhibitionImg(), userView.getExhibitionName(), userView.getLocation(), userView.getArtist()))
public GetHomeUserViewDetailRes getHomeUserViewDetailRes(Long userId) throws BaseException { //로그인을 하고 들어온경우
User user = userRepository.findById(userId).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
List<Story> userViewList = storyRepository.findTop4ByOrderByViewCountDesc();
List<GetHomeUserViewDetailRes.UserViewList> getUserViewList = userViewList.stream()
.map(userView -> new GetHomeUserViewDetailRes.UserViewList(userView.getExhibition().getExhibitionImg(),
userView.getUser().getUserImg(),
userView.getExhibition().getLocation(),
userView.getUser().getNickname(),
storyLikeRepository.existsByUserAndStory(user,userView),
true))
.collect(Collectors.toList());
return new GetHomeUserViewDetailRes(getuserViewList);
return new GetHomeUserViewDetailRes(getUserViewList);
}

public GetHomeUserLikeDetailRes getHomeUserLikeDetailRes() {
List<Exhibition> userLikeList = exhibitionRepository.findTop4ByOrderByLikeCountDesc();
List<GetHomeUserLikeDetailRes.UserLikeList> getuserLikeList = userLikeList.stream()
.map(userLike -> new GetHomeUserLikeDetailRes.UserLikeList(userLike.getExhibitionImg(), userLike.getExhibitionName(), userLike.getLocation(), userLike.getArtist()))
public GetHomeUserLikeDetailRes getHomeUserLikeDetailRes() { //로그인을 안하고 들어온경우
List<Story> userLikeList = storyRepository.findTop4ByOrderByLikeCountDesc();
List<GetHomeUserLikeDetailRes.UserLikeList> getLikeViewList = userLikeList.stream()
.map(userLike -> new GetHomeUserLikeDetailRes.UserLikeList(userLike.getExhibition().getExhibitionImg(),
userLike.getUser().getUserImg(),
userLike.getExhibition().getLocation(),
userLike.getUser().getNickname(),
false,
false))
.collect(Collectors.toList());
return new GetHomeUserLikeDetailRes(getuserLikeList);
return new GetHomeUserLikeDetailRes(getLikeViewList);
}
public GetHomeUserLikeDetailRes getHomeUserLikeDetailRes(Long userId) throws BaseException { //로그인을 하고 들어온경우
User user = userRepository.findById(userId).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
List<Story> userLikeList = storyRepository.findTop4ByOrderByLikeCountDesc();
List<GetHomeUserLikeDetailRes.UserLikeList> getLikeViewList = userLikeList.stream()
.map(userLike -> new GetHomeUserLikeDetailRes.UserLikeList(userLike.getExhibition().getExhibitionImg(),
userLike.getUser().getUserImg(),
userLike.getExhibition().getLocation(),
userLike.getUser().getNickname(),
storyLikeRepository.existsByUserAndStory(user,userLike),
true))
.collect(Collectors.toList());
return new GetHomeUserLikeDetailRes(getLikeViewList);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ public BaseResponse<StoryUploadResponseDto> getStoryUploadView() {
return new BaseResponse<>(e.getStatus());
}
}
}
}
9 changes: 9 additions & 0 deletions place/src/main/java/com/umc/place/story/entity/Story.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public class Story extends BaseEntity {
@OneToMany(mappedBy = "story") // 양방향 매핑
private List<StoryLike> likes = new ArrayList<>();

private int viewCount; // 조회수
private int likeCount; // 좋아요 수
@Builder
public Story(User user, Exhibition exhibition, String storyImg) {
this.user = user;
Expand All @@ -52,4 +54,11 @@ public Story(User user, Exhibition exhibition, String storyImg) {
public void setStoryImg(String storyImg){
this.storyImg = storyImg;
}

public void setViewCount(int viewCount){
this.viewCount = viewCount;
}
public void setLikeCount(int viewCount){
this.likeCount = likeCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public interface StoryRepository extends JpaRepository<Story, Long> {
Optional<Story> findFirstByUserOrderByCreatedDateDesc(User user);

List<Story> findByUserOrderByCreatedDateDesc(User user);
List<Story> findTop4ByOrderByCreatedDateDesc();
List<Story> findTop4ByOrderByViewCountDesc();
List<Story> findTop4ByOrderByLikeCountDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public StoryDetailResponseDto getStoryDetail(Long storyIdx, Long userId) throws
.map(comment -> new CommentResDto(comment))
.collect(Collectors.toList());


//조회수 증가
findStoryById.setViewCount(findStoryById.getViewCount()+1);

if (storyLikeRepository.existsByUserAndStory(findUserById, findStoryById)){ // 좋아요를 누르면
findStoryById.setLikeCount(findStoryById.getLikeCount()+1); // 조회수 증가
}

return StoryDetailResponseDto.builder()
.storyImg(findStoryById.getStoryImg())
.exhibitionAddress(findStoryById.getExhibition().getLocation())
Expand Down