-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from 2024-pre-onboarding-backend-F/feat/conten…
…t_list_search [feat] 게시물 목록 기능 구현
- Loading branch information
Showing
8 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/main/java/wanted/media/exception/PostListCustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package wanted.media.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PostListCustomException extends RuntimeException { | ||
private final ErrorCode errorCode; | ||
|
||
public PostListCustomException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package wanted.media.post.dto; | ||
|
||
import wanted.media.post.domain.Post; | ||
import wanted.media.post.domain.Type; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
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() | ||
); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/wanted/media/post/repository/PostRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,22 @@ | ||
package wanted.media.post.repository; | ||
|
||
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 wanted.media.post.domain.Post; | ||
import wanted.media.post.domain.Type; | ||
|
||
public interface PostRepository extends JpaRepository<Post, String> { | ||
@Query("SELECT p FROM Post p " + | ||
"WHERE p.user.account = :account " + | ||
"AND (:type IS NULL OR p.type = :type) " + | ||
"AND ((:searchBy = 'title' AND LOWER(p.title) LIKE LOWER(CONCAT('%', :search, '%'))) " + | ||
"OR (:searchBy = 'content' AND LOWER(p.content) LIKE LOWER(CONCAT('%', :search, '%'))) " + | ||
"OR (:searchBy = 'title,content' AND (LOWER(p.title) LIKE LOWER(CONCAT('%', :search, '%')) " + | ||
"OR LOWER(p.content) LIKE LOWER(CONCAT('%', :search, '%')))))") | ||
Page<Post> findBySearchContaining(@Param("account") String account, @Param("type") Type type, | ||
@Param("searchBy") String searchBy, @Param("search") String search, | ||
Pageable pageable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/test/java/wanted/media/post/controller/PostControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package wanted.media.post.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@SpringBootTest | ||
@RequiredArgsConstructor | ||
@TestPropertySource(locations = "classpath:application-test.yml") | ||
class PostControllerTest { | ||
private MockMvc mockMvc; | ||
|
||
@Test | ||
void posts_list_성공() throws Exception { | ||
mockMvc.perform(get("/api/posts") | ||
.param("hashtag", "wanted") | ||
.param("type", "FACEBOOK") | ||
.param("orderBy", "createdAt") | ||
.param("sortDirection", "ASC") | ||
.param("search_by", "title") | ||
.param("search", "판교") | ||
.param("page", "0") | ||
.param("page_count", "10") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$").isNotEmpty()); // 응답이 비어있지 않음을 확인 | ||
} | ||
|
||
@Test | ||
void search_by_성공() throws Exception { | ||
mockMvc.perform(get("/api/posts") | ||
.param("hashtag", "wanted") | ||
.param("type", "FACEBOOK") | ||
.param("orderBy", "createdAt") | ||
.param("sortDirection", "ASC") | ||
.param("search_by", "invalidSearchBy") | ||
.param("page", "0") | ||
.param("page_count", "10") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isBadRequest()); // 잘못된 검색 조건에 대한 에러 확인 | ||
} | ||
|
||
@Test | ||
void 유효하지_않은_page값() throws Exception { | ||
mockMvc.perform(get("/api/posts") | ||
.param("hashtag", "wanted") | ||
.param("type", "FACEBOOK") | ||
.param("orderBy", "createdAt") | ||
.param("sortDirection", "ASC") | ||
.param("search_by", "title") | ||
.param("search", "판교") | ||
.param("page", "-1") // 유효하지 않은 페이지 번호 | ||
.param("page_count", "10") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isBadRequest()); // 페이지 값이 유효하지 않음을 확인 | ||
} | ||
|
||
@Test | ||
void 유효하지_않은_page_count_값() throws Exception { | ||
mockMvc.perform(get("/api/posts") | ||
.param("hashtag", "wanted") | ||
.param("type", "FACEBOOK") | ||
.param("orderBy", "createdAt") | ||
.param("sortDirection", "ASC") | ||
.param("search_by", "title") | ||
.param("search", "판교") | ||
.param("page", "0") | ||
.param("page_count", "-10") // 유효하지 않은 페이지 수 | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isBadRequest()); // 페이지 수가 유효하지 않음을 확인 | ||
} | ||
|
||
@Test | ||
void search_키워드가_없을_때() throws Exception { | ||
mockMvc.perform(get("/api/posts") | ||
.param("hashtag", "wanted") | ||
.param("type", "FACEBOOK") | ||
.param("orderBy", "createdAt") | ||
.param("sortDirection", "ASC") | ||
.param("search_by", "title") | ||
.param("page", "0") | ||
.param("page_count", "10") | ||
.contentType(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(jsonPath("$").isNotEmpty()); // 응답이 비어있지 않음을 확인 | ||
} | ||
} |