Skip to content

Commit

Permalink
Merge pull request #173 from likelion-backendschool/feature/post
Browse files Browse the repository at this point in the history
Feature/post
  • Loading branch information
icoo08217 authored Sep 23, 2022
2 parents 2430cd8 + 7738add commit 6c4ad14
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 22 deletions.
32 changes: 32 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
//querydsl 추가
buildscript {
ext {
queryDslVersion = "5.0.0"
}
}

plugins {
id 'org.springframework.boot' version '2.7.2'
id 'io.spring.dependency-management' version '1.0.12.RELEASE'
id 'java'
//querydsl 추가
id "com.ewerk.gradle.plugins.querydsl" version "1.0.10"
}

group = 'com.matdongsan'
Expand Down Expand Up @@ -41,8 +50,31 @@ dependencies {
// file to multipart를 위한 의존성 추가
compileOnly group: 'commons-fileupload', name: 'commons-fileupload', version: '1.4'
compileOnly group: 'commons-io', name: 'commons-io', version: '2.4'
//querydsl 추가
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}"
}

tasks.named('test') {
useJUnitPlatform()
}

//querydsl 추가 시작
def querydslDir = "$buildDir/generated/querydsl"

querydsl {
jpa = true
querydslSourcesDir = querydslDir
}

sourceSets {
main.java.srcDir querydslDir
}

configurations {
querydsl.extendsFrom compileClasspath
}

compileQuerydsl {
options.annotationProcessorPath = configurations.querydsl
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.List;

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

List<Post> findAllByPlace(Place place);

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/matdongsan/domain/post/PostRepositoryCustom.java
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 src/main/java/com/matdongsan/domain/post/PostRepositoryImpl.java
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);
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/matdongsan/domain/post/SearchType.java
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
20 changes: 20 additions & 0 deletions src/main/java/com/matdongsan/infra/QuerydslConfig.java
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);
}
}
18 changes: 16 additions & 2 deletions src/main/java/com/matdongsan/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.matdongsan.domain.place.Place;
import com.matdongsan.domain.post.Post;
import com.matdongsan.domain.post.PostRepository;
import com.matdongsan.util.image.ImageUtil;
import com.matdongsan.domain.post.PostRepositoryImpl;
import com.matdongsan.domain.post.SearchType;
import com.matdongsan.web.controller.util.image.ImageUtil;
import com.matdongsan.web.dto.posts.PostCreateDto;
import lombok.RequiredArgsConstructor;

Expand All @@ -14,6 +16,7 @@
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
Expand All @@ -34,6 +37,7 @@ public class PostService {

private final ImageUtil imageUtil;
private final PostRepository postRepository;
private final PostRepositoryImpl postRepositoryImpl;

public Post findById(Long id) {
return postRepository.findById(id).get();
Expand Down Expand Up @@ -102,7 +106,17 @@ public void delete(Long id) {
}

// 게시글 전체 조회를 페이징으로
public Page<Post> getList(Pageable pageable) {
public Page<Post> getList(String keyword , int page , String searchType ,Pageable pageable) {

searchType = searchType.toLowerCase();

if (searchType.equals(SearchType.TITLE.getKey())) {
return postRepository.searchTitle(keyword, pageable);
} else if (searchType.equals(SearchType.CONTENT.getKey())) {
return postRepository.searchContent(keyword, pageable);
} else if (searchType.equals(SearchType.AUTHOR.getKey())) {
return postRepository.searchAuthor(keyword, pageable);
}

return postRepository.findAll(pageable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.matdongsan.web.controller;


import com.matdongsan.util.image.ImageUtil;
import com.matdongsan.web.controller.util.image.ImageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.Resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.matdongsan.domain.account.Account;
import com.matdongsan.domain.account.AuthUser;
import com.matdongsan.domain.member.Member;
import com.matdongsan.domain.member.MemberAge;
import com.matdongsan.domain.post.Post;

import com.matdongsan.domain.post.PostRepository;

import com.matdongsan.domain.post.SearchType;
import com.matdongsan.domain.reply.Reply;
import com.matdongsan.service.AccountService;
import com.matdongsan.service.LikeApiService;
Expand Down Expand Up @@ -70,23 +72,19 @@ public String showDetailPost(@PathVariable long id,
return "/post/post-detail";
}

// 게시글 전체 조회
// 페이징 처리 하기
// @GetMapping("/post")
// public String showAllPosts(Model model) {
// List<Post> post = postsService.findAll();
//
// model.addAttribute("postList", post);
//
// return "/post/post-list";
// }

@GetMapping("/posts")
public String showAllPosts(Model model , @PageableDefault(sort = "id" , direction = Sort.Direction.DESC , size = 10)Pageable pageable){
public String showAllPosts(@RequestParam(defaultValue = "")String keyword,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "title")String searchType,
Model model ,
@PageableDefault(sort = "id" , direction = Sort.Direction.DESC , size = 10)Pageable pageable){

// 게시글 전체 조회
Page<Post> paging = postService.getList(pageable);
// model에 담기
model.addAttribute("searchType", SearchType.values());

// 게시글 전체 조회
Page<Post> paging = postService.getList(keyword , page , searchType , pageable);

model.addAttribute("paging" , paging);

return "/post/posts-list";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.matdongsan.util.image;
package com.matdongsan.web.controller.util.image;

import org.springframework.web.multipart.MultipartFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.matdongsan.util.image;
package com.matdongsan.web.controller.util.image;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.matdongsan.util.time;
package com.matdongsan.web.controller.util.time;

import com.matdongsan.domain.reply.ReplyTime;

Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/templates/post/posts-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,19 @@ <h5 class="modal-title" id="exampleModalLongTitle">맛집 찾기</h5>

<div class="my-4">
<h3 class="heading-title">맛집 게시글</h3>
<!-- 여기서부터 이제 뷰 단을 꾸미면 됨. -->

<!-- 검색창 -->
<div class="col-6" style="float:right">
<form class="input-group">
<select class="from-select" th:name="searchType" data-placeholder="검색어를 입력해주세요" required >
<option th:each="type : ${searchType}" th:value="${type}" th:text="${type.getValue()}"></option>
</select>
<!-- <input class="form-control" type="text" name="keyword" placeholder="검색어를 입력해주세요." th:value="${param.keyword}" >-->
<input class="form-control" type="text" name="keyword" placeholder="검색어를 입력해주세요." th:value="${param.keyword}" onclick="getSearchList()">
<button type="submit" class="btn btn-outline-secondary">검색</button>
</form>
</div>

<table class="table text-center">
<colgroup>
<col>
Expand Down

0 comments on commit 6c4ad14

Please sign in to comment.