Skip to content

Commit

Permalink
Merge pull request #209 from tukcom2023CD/BE/feat/#205
Browse files Browse the repository at this point in the history
BE/feat/#205 게시물 타입으로 필터링 조회
  • Loading branch information
kimhalin authored Jun 8, 2023
2 parents 7f5a1f6 + 29532e6 commit c26273a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rising.backend.domain.post.controller;

import com.rising.backend.domain.post.domain.PostType;
import com.rising.backend.domain.post.dto.PostDto;
import com.rising.backend.domain.post.service.PostService;
import com.rising.backend.domain.user.domain.User;
Expand Down Expand Up @@ -42,13 +43,15 @@ public ResponseEntity<ResultResponse> create(
}

@GetMapping
public ResponseEntity<ResultResponse> getList(@PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) final Pageable pageable) {
List<PostDto.PostGetListResponse> list = postService.pageList(pageable);
return ResponseEntity.ok(ResultResponse.of(ResultCode.POST_PAGINATION_SUCCESS, list));
public ResponseEntity<ResultResponse> getList(@RequestParam("enumValue") PostType postType, @PageableDefault(size = 10, sort = "createdAt", direction = Sort.Direction.DESC) final Pageable pageable) {
List<PostDto.PostGetListResponse> result = null;
if (postType == null)
result = postService.pageList(pageable);
else
result = postService.getPostsByType(postType, pageable);
return ResponseEntity.ok(ResultResponse.of(ResultCode.POST_PAGINATION_SUCCESS, result));
}



@LoginRequired
@GetMapping("/{postId}/session")
public ResponseEntity<ResultResponse> getSession(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class Post extends BaseEntity {

@NotNull
@Enumerated(EnumType.STRING)
private PostType type;
private PostType postType;

@ManyToMany
@JoinTable(name = "POST_TAG",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Post toPostEntity(PostCreateRequest postCreate, User loginUser) {
.title(postCreate.getTitle())
.videoUrl(null)
.sessionUrl(uuidConverter.toBase64(UUID.randomUUID()))
.type(postCreate.getType()).build();
.postType(postCreate.getType()).build();
}

public PostDto.PostDetailResponse toPostDto(Post post, List<String> tags) {
Expand All @@ -39,7 +39,7 @@ public PostDto.PostDetailResponse toPostDto(Post post, List<String> tags) {
.title(post.getTitle())
.content(post.getContent())
.videoUrl(post.getVideoUrl())
.type(post.getType())
.type(post.getPostType())
.tags(tags)
.created_at(post.getCreatedAt().toLocalDate())
.build();
Expand Down Expand Up @@ -68,7 +68,7 @@ public PostGetListResponse toPostListResponse(Post post) {
.content(post.getContent())
.created_at(post.getCreatedAt().toLocalDate())
.title(post.getTitle())
.type(post.getType())
.type(post.getPostType())
.tags(TagtoString(post.getTag()))
.commentCount(commentRepository.countByPost_Id(post.getId()))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.rising.backend.domain.post.repository;

import com.rising.backend.domain.post.domain.Post;
import com.rising.backend.domain.post.domain.PostType;
import com.rising.backend.domain.post.domain.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
Expand All @@ -11,4 +14,6 @@ public interface PostRepository extends JpaRepository<Post, Long> {
List<Post> findByUserId(Long userId);

List<Tag> findTagById(Long id);

Page<Post> findByPostType(PostType postType, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rising.backend.domain.post.service;

import com.rising.backend.domain.post.domain.Post;
import com.rising.backend.domain.post.domain.PostType;
import com.rising.backend.domain.post.domain.Tag;
import com.rising.backend.domain.post.dto.PostDto;
import com.rising.backend.domain.post.mapper.PostMapper;
Expand Down Expand Up @@ -57,6 +58,11 @@ public List<PostGetListResponse> pageList(Pageable pageable) {
return postMapper.toDtoPageList(postList).getContent();
}

public List<PostGetListResponse> getPostsByType(PostType postType, Pageable pageable) {
Page<Post> posts = postRepository.findByPostType(postType, pageable);
return postMapper.toDtoPageList(posts).getContent();
}

public String getSessionUrl(Long postId, User user) {
Post post = findPostById(postId);
if (!checkIsAuthor(post, user)) {
Expand Down

0 comments on commit c26273a

Please sign in to comment.