Skip to content
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
Expand Up @@ -20,7 +20,6 @@

@RestController
@RequestMapping("/post/comment")
@RequiredArgsConstructor
public class PostCommentController {
@Autowired
private Rq rq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
import com.back.global.rsData.RsData;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -21,6 +26,19 @@ public class InformationPostController {
private final Rq rq;


@Operation(summary = "게시글 조회 - 페이징 처리")
@GetMapping
public RsData<PostPagingResponse> getPostWithPage(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@RequestParam(required = false) String keyword
) {
Page<PostDto> postPage = postService.getPosts(keyword, page,size);
PostPagingResponse resDto = PostPagingResponse.from(postPage);

return new RsData<>("200", "게시글이 조회 되었습니다.", resDto);
}

@Operation(summary = "게시글 생성")
@PostMapping
public RsData<PostCreateResponse> createPost(
Expand All @@ -34,7 +52,7 @@ public RsData<PostCreateResponse> createPost(
}

@Operation(summary = "게시글 다건 조회")
@GetMapping
@GetMapping("/all")
public RsData<List<PostAllResponse>> getAllPost() {
List<PostAllResponse> postAllResponse = postService.getAllPostResponse();

Expand Down
18 changes: 18 additions & 0 deletions back/src/main/java/com/back/domain/post/post/dto/PostDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.back.domain.post.post.dto;


import com.back.domain.post.post.entity.Post;

public record PostDto(
Long postId,
String title,
String content
) {
public static PostDto from(Post post) {
return new PostDto(
post.getId(),
post.getTitle(),
post.getContent()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.back.domain.post.post.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.data.domain.Page;

import java.util.List;

public record PostPagingResponse(
@Schema(description = "게시글 목록")
List<PostDto> posts,
@Schema(description = "현재 페이지 (0부터 시작)")
int currentPage,
@Schema(description = "총 페이지")
int totalPage,
@Schema(description = "총 개수")
long totalElements,
@Schema(description = "다음 페이지 존재 여부")
boolean hasNext
) {

public static PostPagingResponse from(Page<PostDto> page) {
return new PostPagingResponse(
page.getContent(),
page.getNumber(),
page.getTotalPages(),
page.getTotalElements(),
page.hasNext()
);
}

}

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.back.domain.post.post.repository;

import com.back.domain.post.post.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
public interface PostRepository extends JpaRepository<Post, Long> , PostRepositoryCustom{

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.back.domain.post.post.repository;

import com.back.domain.post.post.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface PostRepositoryCustom {
Page<Post> searchPosts(String keyword, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.back.domain.post.post.repository;

import com.back.domain.member.member.entity.QMember;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.entity.QPost;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import java.util.List;

@RequiredArgsConstructor
public class PostRepositoryImpl implements PostRepositoryCustom{
private final JPAQueryFactory queryFactory;

@Override
public Page<Post> searchPosts(String keyword, Pageable pageable) {
QPost post = QPost.post;
QMember member = QMember.member;

BooleanBuilder builder = new BooleanBuilder();

if(keyword != null && !keyword.isBlank()) {
builder.and(
post.title.containsIgnoreCase(keyword)
.or(post.authorName.containsIgnoreCase(keyword))
);
}

List<Post> content = queryFactory
.selectFrom(post)
.where(builder)
.orderBy(post.createDate.desc())
.offset(pageable.getOffset())// 이거뭐임
.limit(pageable.getPageSize())
.fetch();

long total = getTotal(post, builder);

return new PageImpl<>(content,pageable,total);

}

private long getTotal(QPost post, BooleanBuilder builder) {
Long totalCount = queryFactory
.select(post.count())
.from(post)
.where(builder)
.fetchOne();

return totalCount != null ? totalCount : 0L;
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.back.domain.post.post.service;

import com.back.domain.member.member.entity.Member;
import com.back.domain.mentoring.mentoring.dto.MentoringDto;
import com.back.domain.post.post.dto.PostAllResponse;
import com.back.domain.post.post.dto.PostCreateRequest;
import com.back.domain.post.post.dto.PostDto;
import com.back.domain.post.post.entity.Post;
import com.back.domain.post.post.repository.PostRepository;
import com.back.domain.post.post.repository.PostRepositoryCustom;
import com.back.global.exception.ServiceException;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -24,7 +31,7 @@ public List<Post> getAllPosts() {
return posts;
}


@Transactional
public Post createPost(PostCreateRequest postCreateRequest, Member member) {
String postTypeStr = postCreateRequest.getPostType();

Expand All @@ -47,16 +54,11 @@ public Post createPost(PostCreateRequest postCreateRequest, Member member) {
}

private void validPostType(String postTypeStr) {
boolean eq = false;

String[] validType = new String[3];
validType[0] = "INFORMATIONPOST";
validType[1] = "PRACTICEPOST";
validType[2] = "QUESTIONPOST";

for(String x : validType) if(x.equals(postTypeStr)) eq = true;

if(!eq) throw new ServiceException("400-2", "유효하지 않은 PostType입니다.");
try {
Post.PostType.valueOf(postTypeStr);
} catch (IllegalArgumentException e) {
throw new ServiceException("400-2", "유효하지 않은 PostType입니다.");
}
}


Expand All @@ -70,15 +72,15 @@ public List<PostAllResponse> getAllPostResponse() {
.map(PostAllResponse::new)
.toList();
}

@Transactional
public void removePost(Long postId, Member member) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
Long authorId = post.getMember().getId();
if(authorId != member.getId()) throw new ServiceException("400", "삭제 권한이 없습니다.");

postRepository.delete(post);
}

@Transactional
public void updatePost(long postId, Member member, @Valid PostCreateRequest postCreateRequest) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));
Long authorId = post.getMember().getId();
Expand All @@ -89,23 +91,31 @@ public void updatePost(long postId, Member member, @Valid PostCreateRequest post

postRepository.save(post);
}

@Transactional
public void likePost(long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

post.setLiked(post.getLiked()+1);
}

@Transactional
public void disLikePost(long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

post.setLiked(post.getLiked()-1);
}

@Transactional()
public int showLikeCount(long postId) {
Post post = postRepository.findById(postId).orElseThrow(() -> new ServiceException("400", "해당 Id의 게시글이 없습니다."));

int count = post.getLiked();
return count;
}


public Page<PostDto> getPosts(String keyword, int page, int size) {
Pageable pageable = PageRequest.of(page,size);


return postRepository.searchPosts(keyword, pageable).map(PostDto::from);
}
}
Loading