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

[api] 타인 게시글 목록 가져오기, 내 게시글 목록 가져오기 #29

Merged
merged 2 commits into from
Oct 5, 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,18 +1,21 @@
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;
import io.swagger.v3.oas.annotations.tags.Tag;
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;
Expand All @@ -22,6 +25,7 @@
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Tag(name = "community", description = "커뮤니티 API")
@RestController
Expand Down Expand Up @@ -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<PostLikeDto> 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<List<PostDto>> getPostList(@RequestParam Long walkieId, @RequestParam(required = false) Integer pagingSize, @RequestParam(required = false) Integer pagingStart) {
return ResponseEntity.ok(communityService.getPostList(walkieId, pagingSize, pagingStart));
}

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -73,4 +77,17 @@ public ResponseEntity<MyInfoResponse> 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<List<PostDto>> getPostList(@RequestParam Long walkieId, @RequestParam(required = false) Integer pagingSize, @RequestParam(required = false) Integer pagingStart) {
return ResponseEntity.ok(walkieService.getMyPostList(walkieId, pagingSize, pagingStart));
}
}
28 changes: 23 additions & 5 deletions src/main/java/com/whyranoid/walkie/dto/PostDto.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<WalkieDto> liker;

@Schema(description = "[응답] 사진파일 URI", example = "")
@Schema(description = "[응답] 사진파일 URI")
private String photo;

@Schema(description = "[응답] 게시글 내용", example = "오운완.")
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<Post, Long> {
public interface PostRepository extends JpaRepository<Post, Long>, PostRepositoryCustom {

Optional<Post> findByPostId(Long postId);
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,4 +18,10 @@ public interface FollowRepositoryQuerydsl {

List<WalkieDto> findWalkingFollwingList(Long walkieId);

List<WalkieDto> findFollowerList(Long whoseId, boolean isFollowing, JPQLQuery<Long> followIdList);

JPQLQuery<Long> findFollowerIdList(Long whoseId);

JPQLQuery<Long> findFollowedIdList(Long whoseId);

}
Original file line number Diff line number Diff line change
@@ -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<PostDto> findCurrentPosts(JPQLQuery<Long> following, Long viewerId, Integer pagingSize, Integer pagingStart);

List<PostDto> findMyPosts(Long viewerId, Integer pagingSize, Integer pagingStart);
}
Original file line number Diff line number Diff line change
@@ -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<PostDto> findCurrentPosts(JPQLQuery<Long> 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<PostDto> 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();
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/whyranoid/walkie/service/CommunityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -94,4 +97,13 @@ public Long countPostLike(Long postId) {

return postLikeRepository.findPostLikeCount(postId);
}

public List<PostDto> 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);
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/whyranoid/walkie/service/WalkieService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@

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
public class WalkieService {

private final WalkieRepository walkieRepository;
private final AgreementRepository agreementRepository;
private final PostRepository postRepository;

public WalkieSignUpResponse joinWalkie(WalkieSignUpRequest walkieSignUpRequest) {
if (checkNameDuplication(walkieSignUpRequest.getUserName())) {
Expand Down Expand Up @@ -77,4 +83,13 @@ public MyInfoResponse changeMyInfo(Long walkieId, MyInfoRequest myInfoRequest) {
.nickname(walkie.getUserName())
.build();
}

public List<PostDto> 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);
}
}