Skip to content

Commit

Permalink
#7 fix: StoryDetailResDto
Browse files Browse the repository at this point in the history
  • Loading branch information
5jisoo committed Jul 29, 2023
1 parent e97cf48 commit f1dd30a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
11 changes: 9 additions & 2 deletions place/src/main/java/com/umc/place/comment/dto/CommentResDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.umc.place.comment.dto;

import com.umc.place.comment.entity.Comment;
import lombok.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Data
@NoArgsConstructor
Expand All @@ -11,17 +15,20 @@ public class CommentResDto {
private String userImg;
private String userNickName;
private String content;
private LocalDateTime createdDate;

@Builder
public CommentResDto(String userImg, String userName, String content) {
public CommentResDto(String userImg, String userName, String content, LocalDateTime createdDate) {
this.userImg = userImg;
this.userNickName = userName;
this.content = content;
this.createdDate = createdDate;
}

public CommentResDto(Comment comment) {
this.userImg = comment.getUser().getUserImg();
this.userNickName = comment.getUser().getNickname();
this.content = comment.getContent();
this.createdDate = comment.getCreatedDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.umc.place.story.dto.StoryDetailResponseDto;
import com.umc.place.story.service.StoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
Expand All @@ -18,9 +15,9 @@ public class StoryController {
private final StoryService storyService;

@GetMapping("/{storyIdx}")
public BaseResponse<StoryDetailResponseDto> getStoryDetail(@PathVariable Long storyIdx) {
public BaseResponse<StoryDetailResponseDto> getStoryDetail(@PathVariable Long storyIdx, @RequestParam Long userId) {
try {
return new BaseResponse<>(storyService.getStoryDetail(storyIdx));
return new BaseResponse<>(storyService.getStoryDetail(storyIdx, userId));
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.umc.place.story.dto;

import com.umc.place.comment.dto.CommentResDto;
import lombok.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,16 +12,23 @@
@Data
@NoArgsConstructor
public class StoryDetailResponseDto {
private String exhibitionImg;
private String exhibitionName;
private String exhibitionAddress;

private String storyOwnerImg;
private List<CommentResDto> comments = new ArrayList<>();

// like 부분이 추가될 듯 함!!!
private Boolean isLiked;

@Builder
public StoryDetailResponseDto(String exhibitionName, String exhibitionAddress, List<CommentResDto> comments) {
public StoryDetailResponseDto(String exhibitionImg, String exhibitionName, String exhibitionAddress,
String storyOwnerImg, List<CommentResDto> comments, Boolean isLiked) {
this.exhibitionImg = exhibitionImg;
this.exhibitionName = exhibitionName;
this.exhibitionAddress = exhibitionAddress;
this.storyOwnerImg = storyOwnerImg;
this.comments = comments;
this.isLiked = isLiked;
}
}

This file was deleted.

15 changes: 12 additions & 3 deletions place/src/main/java/com/umc/place/story/service/StoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,44 @@
import com.umc.place.common.BaseException;
import com.umc.place.story.dto.StoryDetailResponseDto;
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.DATABASE_ERROR;
import static com.umc.place.common.BaseResponseStatus.INVALID_STORY_IDX;
import static com.umc.place.common.BaseResponseStatus.*;

@Service
@RequiredArgsConstructor
public class StoryService {

private final StoryRepository storyRepository;
private final StoryLikeRepository storyLikeRepository;
private final UserRepository userRepository;

public StoryDetailResponseDto getStoryDetail(Long storyIdx) throws BaseException {
public StoryDetailResponseDto getStoryDetail(Long storyIdx, Long userId) throws BaseException {
try {
Story findStoryById
= storyRepository.findById(storyIdx).orElseThrow(() -> new BaseException(INVALID_STORY_IDX));

User findUserById = userRepository.findById(userId).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

List<CommentResDto> commentDtos = findStoryById.getComments().stream()
.map(comment -> new CommentResDto(comment))
.collect(Collectors.toList());

return StoryDetailResponseDto.builder()
.exhibitionImg(findStoryById.getExhibition().getExhibitionImg())
.exhibitionAddress(findStoryById.getExhibition().getLocation())
.exhibitionName(findStoryById.getExhibition().getExhibitionName())
.storyOwnerImg(findUserById.getUserImg())
.comments(commentDtos)
.isLiked(storyLikeRepository.existsByUserAndStory(findUserById, findStoryById))
.build();
} catch (BaseException e) {
throw e;
Expand Down

0 comments on commit f1dd30a

Please sign in to comment.