-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from Team-Shaka/feature/29
Feature/29 게시글 상세조회 API 구현
- Loading branch information
Showing
18 changed files
with
237 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/treehouse/server/api/post/business/PostMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package treehouse.server.api.post.business; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import treehouse.server.api.post.presentation.dto.PostResponseDTO; | ||
import treehouse.server.api.member.business.MemberMapper; | ||
import treehouse.server.global.common.TimeFormatter; | ||
import treehouse.server.global.entity.post.Post; | ||
import treehouse.server.global.entity.post.PostImage; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PostMapper { | ||
|
||
public static PostResponseDTO.getPostDetails toGetPostDetails(Post post) { | ||
return PostResponseDTO.getPostDetails.builder() | ||
.memberProfile(MemberMapper.toGetMemberProfile(post.getWriter())) | ||
.postId(post.getId()) | ||
.context(post.getContent()) | ||
.pictureUrlList(post.getPostImageList().stream() | ||
.map(PostImage::getImageUrl).toList() | ||
) | ||
// .reactionList() // Reaction 기능 개발 이후 수정 | ||
.postedAt(TimeFormatter.format(post.getCreatedAt())) | ||
.build(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/treehouse/server/api/post/business/PostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package treehouse.server.api.post.business; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import treehouse.server.api.post.implement.PostCommandAdapter; | ||
import treehouse.server.api.post.implement.PostQueryAdapter; | ||
import treehouse.server.api.post.presentation.dto.PostResponseDTO; | ||
import treehouse.server.global.entity.User.User; | ||
import treehouse.server.global.entity.post.Post; | ||
|
||
@Service | ||
@AllArgsConstructor | ||
@Slf4j | ||
public class PostService { | ||
|
||
private final PostCommandAdapter postCommandAdapter; | ||
private final PostQueryAdapter postQueryAdapter; | ||
|
||
/** | ||
* 게시글 상세조회 | ||
* @param user | ||
* @param postId | ||
* @param treehouseId - 게시글 정보에 표시할 memberBranch을 계산하고 감정표현의 isPushed 상태를 반환하기 위해 user와 treehouseId 사용 | ||
* @return PostResponseDTO.getPostDetails | ||
*/ | ||
@Transactional(readOnly = true) | ||
public PostResponseDTO.getPostDetails getPostDetails(User user, Long postId, Long treehouseId){ | ||
Post post = postQueryAdapter.findById(postId); | ||
return PostMapper.toGetPostDetails(post); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/treehouse/server/api/post/implement/PostCommandAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package treehouse.server.api.post.implement; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import treehouse.server.global.annotations.Adapter; | ||
|
||
@Adapter | ||
@RequiredArgsConstructor | ||
public class PostCommandAdapter { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/treehouse/server/api/post/implement/PostQueryAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package treehouse.server.api.post.implement; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import treehouse.server.api.post.persistence.PostRepository; | ||
import treehouse.server.global.annotations.Adapter; | ||
import treehouse.server.global.entity.post.Post; | ||
import treehouse.server.global.exception.GlobalErrorCode; | ||
import treehouse.server.global.exception.ThrowClass.PostException; | ||
|
||
@Adapter | ||
@RequiredArgsConstructor | ||
public class PostQueryAdapter { | ||
|
||
private final PostRepository postRepository; | ||
|
||
public Post findById(Long postId) { | ||
return postRepository.findById(postId).orElseThrow(() -> new PostException(GlobalErrorCode.POST_NOT_FOUND)); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/treehouse/server/api/post/persistence/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package treehouse.server.api.post.persistence; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import treehouse.server.global.entity.post.Post; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/treehouse/server/api/post/presentation/PostApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package treehouse.server.api.post.presentation; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.validation.annotation.Validated; | ||
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 treehouse.server.api.post.business.PostService; | ||
import treehouse.server.api.post.presentation.dto.PostResponseDTO; | ||
import treehouse.server.global.common.CommonResponse; | ||
import treehouse.server.global.entity.User.User; | ||
import treehouse.server.global.security.handler.annotation.AuthMember; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@Validated | ||
@Tag(name = "\uD83D\uDCF0 Feed API", description = "트리하우스 피드 관련 API 입니다. 게시글 작성, 피드 조회 등의 API가 포함됩니다.") | ||
@RequestMapping("/treehouses/{treehouseId}/feeds") | ||
public class PostApi { | ||
|
||
private final PostService postService; | ||
|
||
@GetMapping("/posts/{postId}") | ||
@Operation(summary = "게시글 상세조회 \uD83D\uDD11✅", description = "특정 게시글의 상세정보를 조회합니다.") | ||
public CommonResponse<PostResponseDTO.getPostDetails> getPostDetails( | ||
@PathVariable Long treehouseId, | ||
@PathVariable Long postId, | ||
@AuthMember @Parameter(hidden = true) User user | ||
){ | ||
return CommonResponse.onSuccess(postService.getPostDetails(user, postId, treehouseId)); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/treehouse/server/api/post/presentation/dto/PostResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package treehouse.server.api.post.presentation.dto; | ||
|
||
import lombok.*; | ||
import treehouse.server.api.member.presentation.dto.MemberResponseDTO; | ||
|
||
import java.util.List; | ||
|
||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class PostResponseDTO { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class getPostDetails { | ||
|
||
private MemberResponseDTO.getMemberProfile memberProfile; | ||
private Long postId; | ||
private String context; | ||
private List<String> pictureUrlList; | ||
// private List<ReactionResponseDto.getReaction> reactionList; Reaction 기능 개발 이후 적용 | ||
private String postedAt; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/treehouse/server/global/common/TimeFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package treehouse.server.global.common; | ||
|
||
import java.time.Duration; | ||
import java.time.LocalDateTime; | ||
|
||
// 특정 게시글/댓글의 작성 시간을 현재 시간과 비교하여 문자열로 변환하는 유틸리티 클래스 | ||
public class TimeFormatter { | ||
|
||
private TimeFormatter() { | ||
} | ||
|
||
public static String format(LocalDateTime createdAt) { | ||
LocalDateTime now = LocalDateTime.now(); | ||
Duration duration = Duration.between(createdAt, now); | ||
|
||
long seconds = duration.getSeconds(); | ||
long minutes = duration.toMinutes(); | ||
long hours = duration.toHours(); | ||
long days = duration.toDays(); | ||
|
||
if (seconds < 60) { | ||
return seconds + "초 전"; | ||
} else if (minutes < 60) { | ||
return minutes + "분 전"; | ||
} else if (hours < 24) { | ||
return hours + "시간 전"; | ||
} else { | ||
return days + "일 전"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/treehouse/server/global/exception/ThrowClass/PostException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package treehouse.server.global.exception.ThrowClass; | ||
|
||
import treehouse.server.global.exception.BaseErrorCode; | ||
|
||
public class PostException extends GeneralException{ | ||
|
||
public PostException(BaseErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |