diff --git a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java index 597528b..423dcca 100644 --- a/src/main/java/com/whyranoid/walkie/controller/CommunityController.java +++ b/src/main/java/com/whyranoid/walkie/controller/CommunityController.java @@ -1,11 +1,13 @@ package com.whyranoid.walkie.controller; import com.google.firebase.auth.FirebaseAuthException; +import com.whyranoid.walkie.dto.PostDto; import com.whyranoid.walkie.dto.PostLikeDto; import com.whyranoid.walkie.service.CommunityService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -13,6 +15,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -22,6 +25,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.IOException; +import java.util.List; @Tag(name = "community", description = "커뮤니티 API") @RestController @@ -53,10 +57,23 @@ public ResponseEntity uploadPost( @Operation(summary = "게시글에 좋아요 누르기") @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = PostLikeDto.class)), - description = "성공 시 요청에 게시글의 현재 좋아요 수를 넣어 반환, 중복 좋아요 시 좋아요 수에 -1을 넣어 반환, 실패 시 예외발생") + description = "성공 시 요청에 게시글의 현재 좋아요 수를 넣어 반환, 중복 좋아요 시 좋아요를 삭제하고 likerCount에 -1을 넣어 반환, 실패 시 예외발생") @PostMapping("/send-like") public ResponseEntity sendPostLike(@RequestBody PostLikeDto postLikeDto) { return ResponseEntity.ok(communityService.sendPostLike(postLikeDto)); } + @Operation(summary = "게시글 불러오기", description = "팔로우하는 유저들의 게시글을 가져옵니다.") + @ApiResponse(responseCode = "200", content = @Content(array = @ArraySchema(schema = @Schema(implementation = PostDto.class))), + description = "팔로우하는 유저들의 게시글 정보 리스트를 반환") + @Parameters({ + @Parameter(name = "walkieId", required = true, description = "유저 아이디", example = "123"), + @Parameter(name = "pagingSize", description = "페이징 사이즈", example = "30"), + @Parameter(name = "pagingStart", description = "페이징 오프셋", example = "0") + }) + @GetMapping("/listup-post") + public ResponseEntity> getPostList(@RequestParam Long walkieId, @RequestParam(required = false) Integer pagingSize, @RequestParam(required = false) Integer pagingStart) { + return ResponseEntity.ok(communityService.getPostList(walkieId, pagingSize, pagingStart)); + } + } diff --git a/src/main/java/com/whyranoid/walkie/controller/WalkieController.java b/src/main/java/com/whyranoid/walkie/controller/WalkieController.java index 6ed442b..8bcfb36 100644 --- a/src/main/java/com/whyranoid/walkie/controller/WalkieController.java +++ b/src/main/java/com/whyranoid/walkie/controller/WalkieController.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.controller; +import com.whyranoid.walkie.dto.PostDto; import com.whyranoid.walkie.dto.request.MyInfoRequest; import com.whyranoid.walkie.dto.request.WalkieSignUpRequest; import com.whyranoid.walkie.dto.response.MyInfoResponse; @@ -8,6 +9,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.media.ArraySchema; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -21,6 +23,8 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @Tag(name = "WalkieController") @RequiredArgsConstructor @RequestMapping("/api/walkies") @@ -73,4 +77,17 @@ public ResponseEntity changeMyInfo(@RequestParam Long walkieId, walkieService.changeMyInfo(walkieId, myInfoRequest) ); } + + @Operation(summary = "나의 게시글 불러오기", description = "내가 작성한 게시글들을 가져옵니다.") + @ApiResponse(responseCode = "200", content = @Content(array = @ArraySchema(schema = @Schema(implementation = PostDto.class))), + description = "내가 작성한 게시글 정보 리스트를 반환") + @Parameters({ + @Parameter(name = "walkieId", required = true, description = "유저 아이디", example = "123"), + @Parameter(name = "pagingSize", description = "페이징 사이즈", example = "30"), + @Parameter(name = "pagingStart", description = "페이징 오프셋", example = "0") + }) + @GetMapping("/listup-my-post") + public ResponseEntity> getPostList(@RequestParam Long walkieId, @RequestParam(required = false) Integer pagingSize, @RequestParam(required = false) Integer pagingStart) { + return ResponseEntity.ok(walkieService.getMyPostList(walkieId, pagingSize, pagingStart)); + } } diff --git a/src/main/java/com/whyranoid/walkie/dto/PostDto.java b/src/main/java/com/whyranoid/walkie/dto/PostDto.java index 525ec91..c1ac64a 100644 --- a/src/main/java/com/whyranoid/walkie/dto/PostDto.java +++ b/src/main/java/com/whyranoid/walkie/dto/PostDto.java @@ -1,10 +1,15 @@ package com.whyranoid.walkie.dto; +import com.querydsl.core.annotations.QueryProjection; +import com.whyranoid.walkie.domain.Post; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.List; +import java.util.stream.Collectors; + @Getter @NoArgsConstructor(access = AccessLevel.PROTECTED) public class PostDto { @@ -19,12 +24,12 @@ public class PostDto { private Long postId; @Schema(description = "[응답] 좋아요 여부", example = "true") - private Boolean liked; + private Boolean liked = false; - @Schema(description = "[응답] 좋아요 개수", example = "36") - private Integer likeCount; + @Schema(description = "[응답] 좋아요 누른 유저 리스트") + private List liker; - @Schema(description = "[응답] 사진파일 URI", example = "") + @Schema(description = "[응답] 사진파일 URI") private String photo; @Schema(description = "[응답] 게시글 내용", example = "오운완.") @@ -36,6 +41,19 @@ public class PostDto { @Schema(description = "[응답] 글씨색 설정", example = "0") private Integer colorMode; - @Schema(description = "[응답] 기록 데이터", example = "") + @Schema(description = "[응답] 기록 데이터") private String historyContent; + + @QueryProjection + public PostDto(Post post, Long viewerId) { + this.viewerId = viewerId; + this.posterId = post.getUser().getUserId(); + this.postId = post.getPostId(); + this.liker = post.getLiker().stream().map(WalkieDto::new).collect(Collectors.toList()); + this.photo = post.getPhoto(); + this.content = post.getContent(); + this.date = post.getDate(); + this.colorMode = post.getColorMode(); + this.historyContent = post.getHistoryContent(); + } } diff --git a/src/main/java/com/whyranoid/walkie/repository/PostRepository.java b/src/main/java/com/whyranoid/walkie/repository/PostRepository.java index 0ba8421..5718a6b 100644 --- a/src/main/java/com/whyranoid/walkie/repository/PostRepository.java +++ b/src/main/java/com/whyranoid/walkie/repository/PostRepository.java @@ -1,11 +1,12 @@ package com.whyranoid.walkie.repository; import com.whyranoid.walkie.domain.Post; +import com.whyranoid.walkie.repository.querydsl.PostRepositoryCustom; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; -public interface PostRepository extends JpaRepository { +public interface PostRepository extends JpaRepository, PostRepositoryCustom { Optional findByPostId(Long postId); } diff --git a/src/main/java/com/whyranoid/walkie/repository/querydsl/FollowRepositoryQuerydsl.java b/src/main/java/com/whyranoid/walkie/repository/querydsl/FollowRepositoryQuerydsl.java index 77c42c2..58fbc9b 100644 --- a/src/main/java/com/whyranoid/walkie/repository/querydsl/FollowRepositoryQuerydsl.java +++ b/src/main/java/com/whyranoid/walkie/repository/querydsl/FollowRepositoryQuerydsl.java @@ -1,5 +1,6 @@ package com.whyranoid.walkie.repository.querydsl; +import com.querydsl.jpa.JPQLQuery; import com.whyranoid.walkie.dto.FollowDto; import com.whyranoid.walkie.dto.WalkieDto; @@ -17,4 +18,10 @@ public interface FollowRepositoryQuerydsl { List findWalkingFollwingList(Long walkieId); + List findFollowerList(Long whoseId, boolean isFollowing, JPQLQuery followIdList); + + JPQLQuery findFollowerIdList(Long whoseId); + + JPQLQuery findFollowedIdList(Long whoseId); + } diff --git a/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java new file mode 100644 index 0000000..e241394 --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryCustom.java @@ -0,0 +1,13 @@ +package com.whyranoid.walkie.repository.querydsl; + +import com.querydsl.jpa.JPQLQuery; +import com.whyranoid.walkie.dto.PostDto; + +import java.util.List; + +public interface PostRepositoryCustom { + + List findCurrentPosts(JPQLQuery following, Long viewerId, Integer pagingSize, Integer pagingStart); + + List findMyPosts(Long viewerId, Integer pagingSize, Integer pagingStart); +} diff --git a/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java new file mode 100644 index 0000000..6b53d3a --- /dev/null +++ b/src/main/java/com/whyranoid/walkie/repository/querydsl/PostRepositoryImpl.java @@ -0,0 +1,46 @@ +package com.whyranoid.walkie.repository.querydsl; + +import com.querydsl.core.types.dsl.Expressions; +import com.querydsl.jpa.JPQLQuery; +import com.querydsl.jpa.impl.JPAQueryFactory; +import com.whyranoid.walkie.dto.PostDto; +import com.whyranoid.walkie.dto.QPostDto; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +import static com.whyranoid.walkie.domain.QPost.post; + +@RequiredArgsConstructor +public class PostRepositoryImpl implements PostRepositoryCustom { + + private final JPAQueryFactory queryFactory; + + // TODO: 나의 좋아요 여부 반영 + // TODO: 좋아요 누른 사람 목록 반영 + @Override + public List findCurrentPosts(JPQLQuery following, Long viewerId, Integer pagingSize, Integer pagingStart) { + return queryFactory + .select(new QPostDto(post, Expressions.asNumber(viewerId))) + .from(post) + .where(post.user.userId.in(following)) + .orderBy(post.date.desc()) + .offset(pagingStart) + .limit(pagingSize) + .fetch(); + } + + // TODO: 나의 좋아요 여부 반영 + // TODO: 좋아요 누른 사람 목록 반영 + @Override + public List findMyPosts(Long viewerId, Integer pagingSize, Integer pagingStart) { + return queryFactory + .select(new QPostDto(post, Expressions.asNumber(viewerId))) + .from(post) + .where(post.user.userId.eq(viewerId)) + .orderBy(post.date.desc()) + .offset(pagingStart) + .limit(pagingSize) + .fetch(); + } +} diff --git a/src/main/java/com/whyranoid/walkie/service/CommunityService.java b/src/main/java/com/whyranoid/walkie/service/CommunityService.java index 5d31f6c..a86712d 100644 --- a/src/main/java/com/whyranoid/walkie/service/CommunityService.java +++ b/src/main/java/com/whyranoid/walkie/service/CommunityService.java @@ -8,9 +8,11 @@ import com.whyranoid.walkie.domain.Post; import com.whyranoid.walkie.domain.PostLike; import com.whyranoid.walkie.domain.Walkie; +import com.whyranoid.walkie.dto.PostDto; import com.whyranoid.walkie.dto.PostLikeDto; import com.whyranoid.walkie.dto.response.ApiResponse; import com.whyranoid.walkie.repository.CommunityRepository; +import com.whyranoid.walkie.repository.FollowRepository; import com.whyranoid.walkie.repository.PostLikeRepository; import com.whyranoid.walkie.repository.PostRepository; import com.whyranoid.walkie.repository.WalkieRepository; @@ -37,6 +39,7 @@ public class CommunityService { private final WalkieRepository walkieRepository; private final PostRepository postRepository; private final PostLikeRepository postLikeRepository; + private final FollowRepository followRepository; @Value("${app.firebase-bucket-name}") private String firebaseBucket; @@ -94,4 +97,13 @@ public Long countPostLike(Long postId) { return postLikeRepository.findPostLikeCount(postId); } + + public List getPostList(Long walkieId, Integer _pagingSize, Integer _pagingStart) { + Walkie walkie = walkieRepository.findById(walkieId).orElseThrow(EntityNotFoundException::new); + + Integer pagingSize = _pagingSize == null ? 30 : _pagingSize; + Integer pagingStart = _pagingStart == null ? 0 : _pagingStart; + + return postRepository.findCurrentPosts(followRepository.findFollowedIdList(walkieId), walkieId, pagingSize, pagingStart); + } } diff --git a/src/main/java/com/whyranoid/walkie/service/WalkieService.java b/src/main/java/com/whyranoid/walkie/service/WalkieService.java index f1174fa..9b95a60 100644 --- a/src/main/java/com/whyranoid/walkie/service/WalkieService.java +++ b/src/main/java/com/whyranoid/walkie/service/WalkieService.java @@ -2,16 +2,21 @@ import com.whyranoid.walkie.domain.Agreement; import com.whyranoid.walkie.domain.Walkie; +import com.whyranoid.walkie.dto.PostDto; import com.whyranoid.walkie.dto.request.MyInfoRequest; import com.whyranoid.walkie.dto.request.WalkieSignUpRequest; import com.whyranoid.walkie.dto.response.MyInfoResponse; import com.whyranoid.walkie.dto.response.WalkieSignUpResponse; import com.whyranoid.walkie.repository.AgreementRepository; +import com.whyranoid.walkie.repository.PostRepository; import com.whyranoid.walkie.repository.WalkieRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import javax.persistence.EntityNotFoundException; +import java.util.List; + @Service @Transactional @RequiredArgsConstructor @@ -19,6 +24,7 @@ public class WalkieService { private final WalkieRepository walkieRepository; private final AgreementRepository agreementRepository; + private final PostRepository postRepository; public WalkieSignUpResponse joinWalkie(WalkieSignUpRequest walkieSignUpRequest) { if (checkNameDuplication(walkieSignUpRequest.getUserName())) { @@ -77,4 +83,13 @@ public MyInfoResponse changeMyInfo(Long walkieId, MyInfoRequest myInfoRequest) { .nickname(walkie.getUserName()) .build(); } + + public List getMyPostList(Long walkieId, Integer _pagingSize, Integer _pagingStart) { + Walkie walkie = walkieRepository.findById(walkieId).orElseThrow(EntityNotFoundException::new); + + Integer pagingSize = _pagingSize == null ? 30 : _pagingSize; + Integer pagingStart = _pagingStart == null ? 0 : _pagingStart; + + return postRepository.findMyPosts(walkieId, pagingSize, pagingStart); + } }