-
Notifications
You must be signed in to change notification settings - Fork 0
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 #173 from likelion-backendschool/feature/post
Feature/post
- Loading branch information
Showing
13 changed files
with
228 additions
and
22 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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/matdongsan/domain/post/PostRepositoryCustom.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,12 @@ | ||
package com.matdongsan.domain.post; | ||
|
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface PostRepositoryCustom { | ||
Page<Post> searchTitle(String keyword, Pageable pageable); | ||
|
||
Page<Post> searchContent(String keyword, Pageable pageable); | ||
|
||
Page<Post> searchAuthor(String keyword, Pageable pageable); | ||
} |
100 changes: 100 additions & 0 deletions
100
src/main/java/com/matdongsan/domain/post/PostRepositoryImpl.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,100 @@ | ||
package com.matdongsan.domain.post; | ||
|
||
import com.querydsl.core.types.Order; | ||
import com.querydsl.core.types.OrderSpecifier; | ||
import com.querydsl.core.types.dsl.PathBuilder; | ||
import com.querydsl.jpa.impl.JPAQuery; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
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.support.PageableExecutionUtils; | ||
|
||
import java.util.List; | ||
|
||
import static com.matdongsan.domain.post.QPost.post; | ||
|
||
@RequiredArgsConstructor | ||
public class PostRepositoryImpl implements PostRepositoryCustom{ | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
// 제목 | ||
@Override | ||
public Page<Post> searchTitle(String keyword, Pageable pageable) { | ||
JPAQuery<Post> postQuery = jpaQueryFactory | ||
.selectFrom(post) | ||
.where( | ||
post.title.contains(keyword) | ||
) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()); | ||
|
||
for (Sort.Order o : pageable.getSort()) { // ? | ||
PathBuilder pathBuilder = new PathBuilder(post.getType(), post.getMetadata()); | ||
postQuery.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC, pathBuilder.get(o.getProperty()))); | ||
} | ||
|
||
List<Post> posts = postQuery.fetch(); | ||
|
||
JPAQuery<Long> postCountQuery = jpaQueryFactory | ||
.select(post.count()) | ||
.from(post) | ||
.where(post.title.contains(keyword)); | ||
|
||
return PageableExecutionUtils.getPage(posts, pageable, postCountQuery::fetchOne); | ||
} | ||
|
||
// 내용 검색 | ||
@Override | ||
public Page<Post> searchContent(String keyword, Pageable pageable) { | ||
JPAQuery<Post> postQuery = jpaQueryFactory | ||
.selectFrom(post) | ||
.where( | ||
post.content.contains(keyword) | ||
) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()); | ||
|
||
for (Sort.Order o : pageable.getSort()) { // ? | ||
PathBuilder pathBuilder = new PathBuilder(post.getType(), post.getMetadata()); | ||
postQuery.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC, pathBuilder.get(o.getProperty()))); | ||
} | ||
|
||
List<Post> posts = postQuery.fetch(); | ||
|
||
JPAQuery<Long> postCountQuery = jpaQueryFactory | ||
.select(post.count()) | ||
.from(post) | ||
.where(post.content.contains(keyword)); | ||
|
||
return PageableExecutionUtils.getPage(posts, pageable, postCountQuery::fetchOne); | ||
} | ||
|
||
// 작성자 | ||
@Override | ||
public Page<Post> searchAuthor(String keyword, Pageable pageable) { | ||
JPAQuery<Post> postQuery = jpaQueryFactory | ||
.selectFrom(post) | ||
.where( | ||
post.author.nickname.contains(keyword) | ||
) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()); | ||
|
||
for (Sort.Order o : pageable.getSort()) { // ? | ||
PathBuilder pathBuilder = new PathBuilder(post.getType(), post.getMetadata()); | ||
postQuery.orderBy(new OrderSpecifier(o.isAscending() ? Order.ASC : Order.DESC, pathBuilder.get(o.getProperty()))); | ||
} | ||
|
||
List<Post> posts = postQuery.fetch(); | ||
|
||
JPAQuery<Long> postCountQuery = jpaQueryFactory | ||
.select(post.count()) | ||
.from(post) | ||
.where(post.author.nickname.contains(keyword)); | ||
|
||
return PageableExecutionUtils.getPage(posts, pageable, postCountQuery::fetchOne); | ||
} | ||
} |
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,18 @@ | ||
package com.matdongsan.domain.post; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum SearchType { | ||
TITLE ("title" , "제목"), | ||
CONTENT("content" , "내용") , | ||
AUTHOR("author" , "글쓴이"); | ||
|
||
private final String key; | ||
private final String value; | ||
|
||
} | ||
|
||
// 이넘타입을 받아서 string 으로 만들어주는 converter |
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,20 @@ | ||
package com.matdongsan.infra; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.persistence.PersistenceContext; | ||
|
||
@Configuration | ||
public class QuerydslConfig { | ||
@PersistenceContext | ||
private EntityManager em; | ||
|
||
// JPAQueryFactory Bean 등록 | ||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(em); | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/main/java/com/matdongsan/web/controller/ImageController.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
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
2 changes: 1 addition & 1 deletion
2
.../com/matdongsan/util/image/ImageUtil.java → .../web/controller/util/image/ImageUtil.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
2 changes: 1 addition & 1 deletion
2
...matdongsan/util/image/LocalImageUtil.java → ...controller/util/image/LocalImageUtil.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
2 changes: 1 addition & 1 deletion
2
...m/matdongsan/util/time/TimeConverter.java → ...b/controller/util/time/TimeConverter.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
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