diff --git a/place/src/main/java/com/umc/place/home/controller/HomeController.java b/place/src/main/java/com/umc/place/home/controller/HomeController.java index ad7550b..6c1c6ca 100644 --- a/place/src/main/java/com/umc/place/home/controller/HomeController.java +++ b/place/src/main/java/com/umc/place/home/controller/HomeController.java @@ -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; @@ -16,20 +18,37 @@ @RequestMapping("/home") public class HomeController { private final HomeService homeService; + private final AuthService authService; @ResponseBody @GetMapping("userRecent") - public BaseResponse getHomeUserRecentDetail(){ - return new BaseResponse<>(homeService.getHomeUserRecentDetail()); + public BaseResponse 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(){ - return new BaseResponse<>(homeService.getHomeUserViewDetailRes()); + public BaseResponse 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(){ - return new BaseResponse<>(homeService.getHomeUserLikeDetailRes()); + public BaseResponse getHomeUserLikeDetailRes() throws BaseException { + if (authService.isMember()) { //로그인이 된 경우 + Long userIdx = authService.getUserIdx(); // 로그인을 한 유저 id 찾기 + return new BaseResponse<>(homeService.getHomeUserLikeDetailRes(userIdx)); + }else{ + return new BaseResponse<>(homeService.getHomeUserLikeDetailRes()); + } } } diff --git a/place/src/main/java/com/umc/place/home/dto/GetHomeUserLikeDetailRes.java b/place/src/main/java/com/umc/place/home/dto/GetHomeUserLikeDetailRes.java index cca788d..12add1c 100644 --- a/place/src/main/java/com/umc/place/home/dto/GetHomeUserLikeDetailRes.java +++ b/place/src/main/java/com/umc/place/home/dto/GetHomeUserLikeDetailRes.java @@ -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; } } diff --git a/place/src/main/java/com/umc/place/home/dto/GetHomeUserRecentDetailRes.java b/place/src/main/java/com/umc/place/home/dto/GetHomeUserRecentDetailRes.java index 3cb77a7..9a0829f 100644 --- a/place/src/main/java/com/umc/place/home/dto/GetHomeUserRecentDetailRes.java +++ b/place/src/main/java/com/umc/place/home/dto/GetHomeUserRecentDetailRes.java @@ -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; } } diff --git a/place/src/main/java/com/umc/place/home/dto/GetHomeUserViewDetailRes.java b/place/src/main/java/com/umc/place/home/dto/GetHomeUserViewDetailRes.java index 3c8c661..8098840 100644 --- a/place/src/main/java/com/umc/place/home/dto/GetHomeUserViewDetailRes.java +++ b/place/src/main/java/com/umc/place/home/dto/GetHomeUserViewDetailRes.java @@ -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; } } diff --git a/place/src/main/java/com/umc/place/home/service/HomeService.java b/place/src/main/java/com/umc/place/home/service/HomeService.java index 05c227f..34fe2ee 100644 --- a/place/src/main/java/com/umc/place/home/service/HomeService.java +++ b/place/src/main/java/com/umc/place/home/service/HomeService.java @@ -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 stories = storyRepository.findTop4ByOrderByCreatedDateDesc(); + List 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 stories = storyRepository.findTop4ByOrderByCreatedDateDesc(); + List 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 userRecentList = exhibitionRepository.findTop4ByOrderByCreatedDateDesc(); - List getuserRecentList = userRecentList.stream() - .map(userRecent -> new GetHomeUserRecentDetailRes.UserRecentList(userRecent.getExhibitionImg(), userRecent.getExhibitionName(), userRecent.getLocation(), userRecent.getArtist())) + public GetHomeUserViewDetailRes getHomeUserViewDetailRes() { //로그인을 안하고 들어온경우 + List userViewList = storyRepository.findTop4ByOrderByViewCountDesc(); + List 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 userViewList = exhibitionRepository.findTop4ByOrderByViewCountDesc(); - List 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 userViewList = storyRepository.findTop4ByOrderByViewCountDesc(); + List 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 userLikeList = exhibitionRepository.findTop4ByOrderByLikeCountDesc(); - List getuserLikeList = userLikeList.stream() - .map(userLike -> new GetHomeUserLikeDetailRes.UserLikeList(userLike.getExhibitionImg(), userLike.getExhibitionName(), userLike.getLocation(), userLike.getArtist())) + public GetHomeUserLikeDetailRes getHomeUserLikeDetailRes() { //로그인을 안하고 들어온경우 + List userLikeList = storyRepository.findTop4ByOrderByLikeCountDesc(); + List 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 userLikeList = storyRepository.findTop4ByOrderByLikeCountDesc(); + List 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); + } + } diff --git a/place/src/main/java/com/umc/place/story/controller/StoryController.java b/place/src/main/java/com/umc/place/story/controller/StoryController.java index 19c315f..91dd416 100644 --- a/place/src/main/java/com/umc/place/story/controller/StoryController.java +++ b/place/src/main/java/com/umc/place/story/controller/StoryController.java @@ -125,4 +125,4 @@ public BaseResponse getStoryUploadView() { return new BaseResponse<>(e.getStatus()); } } -} +} \ No newline at end of file diff --git a/place/src/main/java/com/umc/place/story/entity/Story.java b/place/src/main/java/com/umc/place/story/entity/Story.java index 03aaff5..efea686 100644 --- a/place/src/main/java/com/umc/place/story/entity/Story.java +++ b/place/src/main/java/com/umc/place/story/entity/Story.java @@ -43,6 +43,8 @@ public class Story extends BaseEntity { @OneToMany(mappedBy = "story") // 양방향 매핑 private List likes = new ArrayList<>(); + private int viewCount; // 조회수 + private int likeCount; // 좋아요 수 @Builder public Story(User user, Exhibition exhibition, String storyImg) { this.user = user; @@ -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; + } } diff --git a/place/src/main/java/com/umc/place/story/repository/StoryRepository.java b/place/src/main/java/com/umc/place/story/repository/StoryRepository.java index 3091486..e0f8b24 100644 --- a/place/src/main/java/com/umc/place/story/repository/StoryRepository.java +++ b/place/src/main/java/com/umc/place/story/repository/StoryRepository.java @@ -16,4 +16,7 @@ public interface StoryRepository extends JpaRepository { Optional findFirstByUserOrderByCreatedDateDesc(User user); List findByUserOrderByCreatedDateDesc(User user); + List findTop4ByOrderByCreatedDateDesc(); + List findTop4ByOrderByViewCountDesc(); + List findTop4ByOrderByLikeCountDesc(); } diff --git a/place/src/main/java/com/umc/place/story/service/StoryService.java b/place/src/main/java/com/umc/place/story/service/StoryService.java index cb1865a..46d7210 100644 --- a/place/src/main/java/com/umc/place/story/service/StoryService.java +++ b/place/src/main/java/com/umc/place/story/service/StoryService.java @@ -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())