Skip to content

Commit

Permalink
[Feat] 게시글 차단 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hen715 committed Jan 22, 2025
1 parent 8c16062 commit e9f8ec7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public ResponseEntity<ResponseDto<Integer>> scrapPost(@AuthenticationPrincipal M
@GetMapping("")
public ResponseEntity<ResponseDto<ListResponseDto<PostListResponseDto>>> getAllPost(@RequestParam(required = false) String category, @RequestParam(required = false,defaultValue = "date") String sort
,@RequestParam(required = false,defaultValue = "1") @Min(1) int page,@AuthenticationPrincipal Member member ){
return ResponseEntity.ok(ResponseDto.of(postService.getAllPost(category, sort,page),"모든 게시글 가져오기 성공"));
return ResponseEntity.ok(ResponseDto.of(postService.getAllPost(category, sort,page,member),"모든 게시글 가져오기 성공"));
}


Expand Down Expand Up @@ -174,7 +174,7 @@ public ResponseEntity<ResponseDto<List<PostListResponseDto>>> getPostForMain(){
})
@GetMapping("/mobile")
public ResponseEntity<ResponseDto<List<PostListResponseDto>>> getPostForMobile(@RequestParam(required = false) Long lastPostId, @RequestParam(required = false) String category, @AuthenticationPrincipal Member member){
return ResponseEntity.ok(ResponseDto.of(postService.getPostForInf(lastPostId,category),"모바일용 게시글 리스트 가져오기 성공"));
return ResponseEntity.ok(ResponseDto.of(postService.getPostForInf(lastPostId,category,member),"모바일용 게시글 리스트 가져오기 성공"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;
Expand All @@ -17,11 +18,6 @@ public interface PostRepository extends JpaRepository<Post,Long> {
Optional<Post> findByIdAndIsDeletedFalse(Long id);
@Query("SELECT p FROM Post p LEFT JOIN FETCH p.member m WHERE p.id = :id AND p.isDeleted = false")
Optional<Post> findByIdAndIsDeletedFalseWithPostMember(Long id);
Page<Post> findAllByCategoryAndIsDeletedFalse(String category,Pageable pageable);

Page<Post> findAllByIsDeletedFalse(Pageable pageable);

List<Post> findAllByIdLessThanAndIsDeletedFalse(Long id, Pageable pageable);
List<Post> findByCategoryAndIdLessThanAndIsDeletedFalse(String category,Long id,Pageable pageable);
long count();
List<Post> findAllByMemberAndIsDeletedFalse(Member member, Sort sort);
Expand All @@ -44,5 +40,10 @@ public interface PostRepository extends JpaRepository<Post,Long> {
Optional<Post> findByIdWithLock(Long id);


@Query(" SELECT p FROM Post p WHERE p.isDeleted = false AND (:category IS NULL OR p.category = :category) AND (:postIds IS NULL OR p.id NOT IN :postIds)")
Page<Post> findAllByCategoryExcludingPostIds(@Param("category") String category, @Param("postIds") List<Long> postIds, Pageable pageable);

@Query(" SELECT p FROM Post p WHERE p.isDeleted = false AND (:category IS NULL OR p.category = :category) AND (:id IS NULL OR p.id < :id) AND (:postIds IS NULL OR p.id NOT IN :postIds)")
List<Post> findFilteredPosts(@Param("category") String category, @Param("id") Long id, @Param("postIds") List<Long> postIds, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import kr.inuappcenterportal.inuportal.domain.postLike.model.PostLike;
import kr.inuappcenterportal.inuportal.domain.postLike.repository.LikePostRepository;
import kr.inuappcenterportal.inuportal.domain.reply.service.ReplyService;
import kr.inuappcenterportal.inuportal.domain.report.repository.ReportRepository;
import kr.inuappcenterportal.inuportal.domain.scrap.model.Scrap;
import kr.inuappcenterportal.inuportal.domain.scrap.repository.ScrapRepository;
import kr.inuappcenterportal.inuportal.global.dto.ListResponseDto;
Expand Down Expand Up @@ -45,6 +46,7 @@ public class PostService {
private final CategoryRepository categoryRepository;
private final RedisService redisService;
private final ImageService imageService;
private final ReportRepository reportRepository;

@Value("${postImagePath}")
private String path;
Expand Down Expand Up @@ -105,6 +107,9 @@ public void delete(Long memberId, Long postId) throws IOException {
@Transactional
public PostResponseDto getPost(Long postId,Member member,String address){
Post post = postRepository.findByIdAndIsDeletedFalseWithPostMember(postId).orElseThrow(()->new MyException(MyErrorCode.POST_NOT_FOUND));
if(member!=null&&reportRepository.existsByPostIdAndMemberId(postId,member.getId())){
throw new MyException(MyErrorCode.BANNED_POST);
}
if(redisService.isFirstConnect(address,postId,"post")){
redisService.insertAddress(address,postId,"post");
post.upViewCount();
Expand Down Expand Up @@ -143,19 +148,18 @@ public PostResponseDto getPost(Long postId,Member member,String address){


@Transactional(readOnly = true)
public ListResponseDto<PostListResponseDto> getAllPost(String category, String sort, int page){
public ListResponseDto<PostListResponseDto> getAllPost(String category, String sort, int page, Member member){
Pageable pageable = PageRequest.of(page>0?--page:page,8,sortData(sort));
Page<Post> dto;
if(category==null){
dto = postRepository.findAllByIsDeletedFalse(pageable);
List<Long> postIds = null;
if (category != null && !categoryRepository.existsByCategory(category)) {
throw new MyException(MyErrorCode.CATEGORY_NOT_FOUND);
}
else{
if(!categoryRepository.existsByCategory(category)){
throw new MyException(MyErrorCode.CATEGORY_NOT_FOUND);
}
dto = postRepository.findAllByCategoryAndIsDeletedFalse(category, pageable);
if(member!=null) {
postIds = reportRepository.findPostIdsByMemberId(member.getId());
}
long total = dto.getTotalElements();
dto = postRepository.findAllByCategoryExcludingPostIds(category,postIds,pageable);
long total = dto.getTotalElements();
long pages = dto.getTotalPages();
List<PostListResponseDto> posts = dto.stream()
.map(this::getPostListResponseDto)
Expand Down Expand Up @@ -293,19 +297,20 @@ public ListResponseDto<PostListResponseDto> searchInScrap(Member member, String


@Transactional(readOnly = true)
public List<PostListResponseDto> getPostForInf(Long lastPostId,String category){
public List<PostListResponseDto> getPostForInf(Long lastPostId,String category, Member member){
Pageable pageable = PageRequest.of(0,8,sortData("date"));
if(lastPostId==null&&category==null){
return postRepository.findAllByIsDeletedFalse(pageable).stream().map(this::getPostListResponseDto).collect(Collectors.toList());
List<Long> postIds = null;
if(member!=null){
postIds = reportRepository.findPostIdsByMemberId(member.getId());
}
else if(lastPostId == null){
return postRepository.findAllByCategoryAndIsDeletedFalse(category,pageable).stream().map(this::getPostListResponseDto).collect(Collectors.toList());
if(lastPostId==null){
return postRepository.findAllByCategoryExcludingPostIds(category,postIds,pageable).stream().map(this::getPostListResponseDto).collect(Collectors.toList());
}
else if(category!=null){
return postRepository.findByCategoryAndIdLessThanAndIsDeletedFalse(category,lastPostId,pageable).stream().map(this::getPostListResponseDto).collect(Collectors.toList());
}
else {
return postRepository.findAllByIdLessThanAndIsDeletedFalse(lastPostId, pageable).stream().map(this::getPostListResponseDto).collect(Collectors.toList());
return postRepository.findFilteredPosts(category,lastPostId,postIds, pageable).stream().map(this::getPostListResponseDto).collect(Collectors.toList());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface ReportRepository extends JpaRepository<Report,Long> {

Page<Report> findAllBy(Pageable pageable);
boolean existsByPostIdAndMemberId(Long postId, Long memberId);
@Query("SELECT r.postId FROM Report r WHERE r.memberId = :memberId")
List<Long> findPostIdsByMemberId(@Param("memberId") Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public enum MyErrorCode {
DUPLICATE_RESERVATION(HttpStatus.BAD_REQUEST, "동일한 사용자의 예약이 존재합니다."),
RESERVATION_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 예약"),
HAS_NOT_RESERVATION_AUTHORIZATION(HttpStatus.NOT_FOUND, "이 예약에 대한 삭제 권한이 없습니다."),
LOST_PROPERTY_NOT_FOUND(HttpStatus.NOT_FOUND, "이 예약에 대한 삭제 권한이 없습니다.");
LOST_PROPERTY_NOT_FOUND(HttpStatus.NOT_FOUND, "이 예약에 대한 삭제 권한이 없습니다."),
BANNED_POST(HttpStatus.NOT_FOUND,"차단한 게시글입니다.");

private final HttpStatus status;
private final String message;
Expand Down

0 comments on commit e9f8ec7

Please sign in to comment.