-
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.
#16 - Feat: 도서 등록폼에서 QueryDSL 으로 내 글의 해시태그 키워드/개수 조회, 도서 등록 기능 구현
- Loading branch information
Showing
8 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
...ain/java/com/example/mutbooks/app/postKeyword/exception/PostKeywordNotFoundException.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,7 @@ | ||
package com.example.mutbooks.app.postKeyword.exception; | ||
|
||
public class PostKeywordNotFoundException extends RuntimeException { | ||
public PostKeywordNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
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
9 changes: 9 additions & 0 deletions
9
...ain/java/com/example/mutbooks/app/postKeyword/repository/PostKeywordRepositoryCustom.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,9 @@ | ||
package com.example.mutbooks.app.postKeyword.repository; | ||
|
||
import com.example.mutbooks.app.postKeyword.dto.PostKeywordDto; | ||
|
||
import java.util.List; | ||
|
||
public interface PostKeywordRepositoryCustom { | ||
List<PostKeywordDto> getQslAllByAuthorId(Long authorId); | ||
} |
34 changes: 34 additions & 0 deletions
34
.../main/java/com/example/mutbooks/app/postKeyword/repository/PostKeywordRepositoryImpl.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,34 @@ | ||
package com.example.mutbooks.app.postKeyword.repository; | ||
|
||
import com.example.mutbooks.app.postKeyword.dto.PostKeywordDto; | ||
import com.querydsl.core.types.Projections; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
import static com.example.mutbooks.app.postHashTag.entity.QPostHashTag.postHashTag; | ||
import static com.example.mutbooks.app.postKeyword.entity.QPostKeyword.postKeyword; | ||
|
||
@RequiredArgsConstructor | ||
public class PostKeywordRepositoryImpl implements PostKeywordRepositoryCustom { | ||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public List<PostKeywordDto> getQslAllByAuthorId(Long authorId) { | ||
// 생성자를 이용해 List<PostKeywordDto> 로 반환 | ||
return jpaQueryFactory | ||
.select(Projections.constructor(PostKeywordDto.class, | ||
postKeyword.id, | ||
postKeyword.content, | ||
postHashTag.count() | ||
)) | ||
.from(postKeyword) | ||
.innerJoin(postHashTag) | ||
.on(postKeyword.eq(postHashTag.postKeyword)) | ||
.where(postHashTag.member.id.eq(authorId)) | ||
.orderBy(postKeyword.content.asc()) // 키워드 명 오름차순 정렬 | ||
.groupBy(postKeyword.id) // 키워드 중복 제거 | ||
.fetch(); | ||
} | ||
} |
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
28 changes: 27 additions & 1 deletion
28
...mutbooks/src/main/java/com/example/mutbooks/app/product/controller/ProductController.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,23 +1,49 @@ | ||
package com.example.mutbooks.app.product.controller; | ||
|
||
import com.example.mutbooks.app.base.security.dto.MemberContext; | ||
import com.example.mutbooks.app.member.entity.Member; | ||
import com.example.mutbooks.app.postKeyword.dto.PostKeywordDto; | ||
import com.example.mutbooks.app.postKeyword.service.PostKeywordService; | ||
import com.example.mutbooks.app.product.entity.Product; | ||
import com.example.mutbooks.app.product.form.ProductForm; | ||
import com.example.mutbooks.app.product.service.ProductService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import javax.validation.Valid; | ||
import java.util.List; | ||
|
||
@Controller | ||
@RequestMapping("/product") | ||
@RequiredArgsConstructor | ||
public class ProductController { | ||
private final ProductService productService; | ||
private final PostKeywordService postKeywordService; | ||
|
||
// TODO: 작가회원인지 검증 | ||
// 도서 등록폼 | ||
@PreAuthorize("isAuthenticated()") | ||
@GetMapping("/create") | ||
public String showCreate() { | ||
public String showCreate(@AuthenticationPrincipal MemberContext memberContext, Model model) { | ||
List<PostKeywordDto> postKeywords = postKeywordService.findByMemberId(memberContext.getId()); | ||
model.addAttribute("postKeywords", postKeywords); | ||
|
||
return "/product/create"; | ||
} | ||
|
||
// 도서 등록 | ||
@PreAuthorize("isAuthenticated()") | ||
@PostMapping("/create") | ||
public String create(@AuthenticationPrincipal MemberContext memberContext, @Valid ProductForm productForm) { | ||
Member author = memberContext.getMember(); | ||
Product product = productService.create(author, productForm); | ||
|
||
return "redirect:/product/" + product.getId(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...ssion/mutbooks/src/main/java/com/example/mutbooks/app/product/service/ProductService.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,11 +1,38 @@ | ||
package com.example.mutbooks.app.product.service; | ||
|
||
import com.example.mutbooks.app.member.entity.Member; | ||
import com.example.mutbooks.app.postKeyword.entity.PostKeyword; | ||
import com.example.mutbooks.app.postKeyword.service.PostKeywordService; | ||
import com.example.mutbooks.app.product.entity.Product; | ||
import com.example.mutbooks.app.product.form.ProductForm; | ||
import com.example.mutbooks.app.product.repository.ProductRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class ProductService { | ||
private final ProductRepository productRepository; | ||
private final PostKeywordService postKeywordService; | ||
|
||
@Transactional | ||
public Product create(Member author, ProductForm productForm) { | ||
PostKeyword postKeyword = postKeywordService.findById(productForm.getPostKeywordId()); | ||
|
||
Product product = Product.builder() | ||
.author(author) | ||
.postKeyword(postKeyword) | ||
.subject(productForm.getSubject()) | ||
.content(productForm.getContent()) | ||
.price(productForm.getPrice()) | ||
.build(); | ||
|
||
productRepository.save(product); | ||
|
||
// TODO : 도서 해시태그 저장 | ||
|
||
return product; | ||
} | ||
} |
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