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

[feat] 게시물 목록 기능 구현 #30

Merged
merged 13 commits into from
Aug 26, 2024
Merged

[feat] 게시물 목록 기능 구현 #30

merged 13 commits into from
Aug 26, 2024

Conversation

K-0joo
Copy link
Contributor

@K-0joo K-0joo commented Aug 26, 2024

Issue

PR 타입(하나 이상의 PR 타입을 선택해주세요)

  • 기능 추가
  • 기능 삭제
  • 버그 수정
  • 의존성, 환경 변수, 빌드 관련 코드 업데이트

반영 브랜치

feat/content_list_search -> dev

변경 사항

  • 파일명, 변수명 Content -> Post로 변경
  • 게시물 목록 기능 구현(필터링, 검색, 페이지네이션)
  • 에러 핸들링 코드 추가
  • 테스트 코드 추가

테스트 결과

Request

HTTP : GET
URL: /api/posts?hashtag=wanted&type=FACEBOOK&orderBy=createdAt&sortDirection=ASC&search_by=title&search=판교&page=0&page_count=10

Response : 성공시

[
    {
        "id": "1",
        "likeCount": 5,
        "type": "FACEBOOK",
        "title": "판교 나들이",
        "content": "오늘은 판교에 와보았어요. 미래의 직장이 될 곳들이 많이...",
        "hashtags": "#wanted, #pangyo",
        "viewCount": 54,
        "shareCount": 30,
        "updatedAt": "2024-08-23T00:00:00",
        "createdAt": "2024-08-20T00:00:00",
        "account": "wanted"
    }
]

Response : 실패시

  • 500 에러

  • 잘못된 정렬 기준, 정렬 방향일 때

{
    "statusCode": 500,
    "message": "서버 내부 오류가 발생했습니다."
}
  • 404 에러

  • 존재하지 않는 account, 검색어 일 때

  • 페이지 수가 너무 클 때

{
    "statusCode": 404,
    "message": "존재하지 않는 엔티티입니다."
}

@K-0joo K-0joo self-assigned this Aug 26, 2024
@K-0joo K-0joo added the feat label Aug 26, 2024

//System.out.println("PostDtos: " + postDtos);

return postDtos;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

헉 보내고 나니 ResponseEntity로 구현하는게 기억나서..나중에 다시 코드 리뷰 받고 refactor로 다시 고치겠습니다 죄송합니다 😭


@Service
public class PostService {
@Autowired
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Autowired 빼고 @Service 어노테이션 밑에 @RequiredArgsConstructor 사용해주시면 더 깔끔할 것 같슴당


return postRepository.findBySearchContaining(account, type, searchBy, search, pageable);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 공백 제거해주세요~

Sort sort = Sort.by(Sort.Direction.fromString(sortDirection), orderBy);
Pageable pageable = PageRequest.of(page, pageCount, sort);

// if (search == null || search.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주석처리한 로직은 사용하지 않으시는건가요?

.map(PostDto::allPosts)
.collect(Collectors.toList());

//System.out.println("PostDtos: " + postDtos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사용하지 않는 로직은 제거해서 올려주시면 됩니당 41라인 공백도 제거해 주세요~!

// 예시: 검색 조건에 맞는 게시물이 없는 경우 예외 처리
Page<Post> posts = postRepository.findBySearchContaining(account, type, searchBy, search, pageable);
if (posts.isEmpty()) {
throw new CustomException(ErrorCode.ENTITY_NOT_FOUND);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기는 CustomException보단 NotFoundException을 사용하시는게 맞는것같아요
35라인에 불필요한 공백 제거해주세요!

INSERT INTO contents(like_count, type, title, content, hashtags, view_count, share_count, updated_at, created_at,
user_id)
VALUES (30, 'INSTAGRAM', '판교 나들이', '오늘은 판교에 와보았어요. 미래의 직장이 될 곳들이 많이...', 'wanted', 24, 7, '2024-08-23', '2024-08-20',
'wanted');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EOL이 안지켜졌네요 !

List<PostDto> posts = responseEntity.getBody();
assertThat(posts).isNotEmpty();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 EOL이 안지켜졌네요!


// 예시: 검색 조건에 맞는 게시물이 없는 경우 예외 처리
Page<Post> posts = postRepository.findBySearchContaining(account, type, searchBy, search, pageable);
if (posts.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 게시물이 없으면 에러를 던지는게 아니라 그냥 빈 Post를 반환하는것도 좋은거 같은데 어떻게 생각하세용?


@Service
public class PostService {
@Autowired
Copy link
Contributor

@pie2457 pie2457 Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 @Autowired 를 제거하고 @RequiredArgsConstructor를 달아주시면 좋을 것 같습니당

@NoArgsConstructor
@Getter
@Setter
public class PostDto {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

record 클래스로 바꾸시면 더 깔끔할 것 같습니다!

public record PostDto(
    String id,
    Long likeCount,
    Type type,
    String title,
    String content,
    String hashtags,
    Long viewCount,
    Long shareCount,
    LocalDateTime updatedAt,
    LocalDateTime createdAt,
    String account) {
    
    public static PostDto allPosts(Post post) {
        return new PostDto(
            post.getId(),
            post.getLikeCount(),
            post.getType(),
            post.getTitle(),
            post.getContent(),
            post.getHashtags(),
            post.getViewCount(),
            post.getShareCount(),
            post.getUpdatedAt(),
            post.getCreatedAt(),
            post.getUser().getAccount()
        );
    }
}

@pie2457 pie2457 linked an issue Aug 26, 2024 that may be closed by this pull request
4 tasks
Copy link
Contributor

@jw427 jw427 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다!

Copy link
Contributor

@jeongeungyeong jeongeungyeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

public class PostService {
private PostRepository postRepository;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final을 붙여주셔야 @RequiredArgsConstructor가 동작하는 걸로 알고 있어용

Copy link
Contributor

@rhaehf rhaehf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다!

@K-0joo K-0joo merged commit 5bd1017 into dev Aug 26, 2024
@K-0joo K-0joo deleted the feat/content_list_search branch August 26, 2024 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

게시물 목록 기능
6 participants