Skip to content

Commit

Permalink
Merge pull request #326 from woowacourse-teams/dev/be
Browse files Browse the repository at this point in the history
[BE] 검색 & 전체 조회 기능 병합
  • Loading branch information
jminkkk authored Aug 7, 2024
2 parents d4d1dd7 + 8272db6 commit 0ed803c
Show file tree
Hide file tree
Showing 16 changed files with 360 additions and 435 deletions.
6 changes: 3 additions & 3 deletions backend/src/main/java/codezap/category/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
@AllArgsConstructor
@Getter
@Table(
uniqueConstraints={
uniqueConstraints = {
@UniqueConstraint(
name="name_with_member",
columnNames={"member_id", "name"}
name = "name_with_member",
columnNames = {"member_id", "name"}
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class ApiErrorResponsesCustomizer implements OperationCustomizer {
@Override
public Operation customize(Operation operation, HandlerMethod handlerMethod) {
if (handlerMethod.hasMethodAnnotation(ApiErrorResponse.class)) {
ApiErrorResponse apiErrorResponse = Objects.requireNonNull(handlerMethod.getMethodAnnotation(ApiErrorResponse.class));
ApiErrorResponse apiErrorResponse = Objects.requireNonNull(
handlerMethod.getMethodAnnotation(ApiErrorResponse.class));
ApiResponses responses = operation.getResponses();
String statusCode = String.valueOf(apiErrorResponse.status().value());
responses.addApiResponse(statusCode, makeFailResponse(apiErrorResponse));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ public interface SpringDocMemberController {
@ApiResponse(
responseCode = "200",
description = "로그인 성공",
headers = {@Header(name = "Set-Cookie", description = "base64(${email}:${password}); path=\"/\"; HttpOnly; Secure;")}
headers = {
@Header(name = "Set-Cookie", description = "base64(${email}:${password}); path=\"/\"; HttpOnly; "
+ "Secure;")}
)
@ApiResponse(
responseCode = "400",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void validateMemberId(MemberDto memberDto, Long memberId) {
throw new CodeZapException(HttpStatus.UNAUTHORIZED, "다른 사람의 템플릿은 확인할 수 없습니다.");
}

if(!memberRepository.existsById(memberId)) {
if (!memberRepository.existsById(memberId)) {
throw new CodeZapException(HttpStatus.UNAUTHORIZED, "로그인 정보가 잘못되었습니다.");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package codezap.template.controller;

import org.springframework.data.domain.Pageable;
import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand All @@ -11,7 +11,6 @@
import codezap.member.dto.MemberDto;
import codezap.template.dto.request.CreateTemplateRequest;
import codezap.template.dto.request.UpdateTemplateRequest;
import codezap.template.dto.response.FindAllMyTemplatesResponse;
import codezap.template.dto.response.ExploreTemplatesResponse;
import codezap.template.dto.response.FindAllTemplatesResponse;
import codezap.template.dto.response.FindTemplateResponse;
Expand Down Expand Up @@ -44,20 +43,28 @@ public interface SpringDocTemplateController {
})
ResponseEntity<Void> create(CreateTemplateRequest createTemplateRequest, MemberDto memberDto);

@Operation(summary = "템플릿 목록 조회", description = """
조건에 맞는 모든 템플릿을 조회합니다.
필터링 조건은 작성자 Id, 카테고리 Id, 태그 목록을 사용할 수 있습니다.
@Operation(summary = "템플릿 검색", description = """
필터링 조건에 맞는 모든 템플릿을 조회합니다.
- 필터링 조건
- 멤버 ID
- 검색 키워드 (템플릿 제목, 템플릿 설명, 스니펫 파일명, 소스 코드)
- 카테고리 ID
- 태그 ID
- 정렬 방식
- 최신순 (createdAt,asc)
- 오래된순 (createdAt,desc)
조회 조건으로 페이지 인덱스, 한 페이지에 들어갈 최대 템플릿의 개수를 변경할 수 있습니다.
페이지 인덱스는 1, 템플릿 개수는 20개가 기본 값입니다.
""")
@ApiResponse(responseCode = "200", description = "템플릿 단건 조회 성공",
content = {@Content(schema = @Schema(implementation = ExploreTemplatesResponse.class))})
ResponseEntity<FindAllTemplatesResponse> getTemplates(
//Long memberId,
Integer pageNumber,
Integer pageSize,
MemberDto memberDto,
Long memberId,
String keyword,
Long categoryId,
List<String> tagNames
List<Long> tagIds,
Pageable pageable
);

@Operation(summary = "템플릿 단건 조회", description = "해당하는 식별자의 템플릿을 조회합니다.")
Expand All @@ -69,13 +76,6 @@ ResponseEntity<FindAllTemplatesResponse> getTemplates(
})
ResponseEntity<FindTemplateResponse> getTemplateById(Long id, MemberDto memberDto);

@Operation(summary = "템플릿 토픽 검색", description = "토픽이 포함된 템플릿들을 검색합니다.")
@ApiResponse(responseCode = "200", description = "템플릿 토픽 검색 성공",
content = {@Content(schema = @Schema(implementation = FindAllTemplatesResponse.class))})
ResponseEntity<FindAllMyTemplatesResponse> getMyTemplatesContainTopic(
MemberDto memberDto, Long memberId, String topic, Pageable pageable
);

@Operation(summary = "템플릿 수정", description = "해당하는 식별자의 템플릿을 수정합니다.")
@ApiResponse(responseCode = "200", description = "템플릿 수정 성공")
@ApiErrorResponse(status = HttpStatus.BAD_REQUEST, instance = "/templates/1", errorCases = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.apache.commons.lang3.NotImplementedException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.query.Param;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -24,23 +21,19 @@
import codezap.member.dto.MemberDto;
import codezap.template.dto.request.CreateTemplateRequest;
import codezap.template.dto.request.UpdateTemplateRequest;
import codezap.template.dto.response.FindAllMyTemplatesResponse;
import codezap.template.dto.response.ExploreTemplatesResponse;
import codezap.template.dto.response.FindAllTemplatesResponse;
import codezap.template.dto.response.FindTemplateResponse;
import codezap.template.service.MyTemplateFacadeService;
import codezap.template.service.TemplateService;

@RestController
@RequestMapping("/templates")
public class TemplateController implements SpringDocTemplateController {

private final TemplateService templateService;
private final MyTemplateFacadeService myTemplateFacadeService;

public TemplateController(TemplateService templateService, MyTemplateFacadeService myTemplateFacadeService) {
public TemplateController(TemplateService templateService) {
this.templateService = templateService;
this.myTemplateFacadeService = myTemplateFacadeService;
}

@PostMapping
Expand All @@ -55,14 +48,16 @@ public ResponseEntity<Void> create(

@GetMapping
public ResponseEntity<FindAllTemplatesResponse> getTemplates(
//@RequestParam Long memberId,
@RequestParam(required = false, defaultValue = "1") Integer pageNumber,
@RequestParam(required = false, defaultValue = "20") Integer pageSize,
@BasicAuthentication MemberDto memberDto,
@RequestParam Long memberId,
@RequestParam String keyword,
@RequestParam(required = false) Long categoryId,
@RequestParam(required = false) List<String> tags
@RequestParam(required = false) List<Long> tagIds,
@PageableDefault(size = 20, page = 1) Pageable pageable
) {

return ResponseEntity.ok(templateService.findAllBy(PageRequest.of(pageNumber - 1, pageSize), categoryId, tags));
FindAllTemplatesResponse response =
templateService.findAllBy(memberId, keyword, categoryId, tagIds, pageable);
return ResponseEntity.ok(response);
}

@GetMapping("/explore")
Expand All @@ -71,20 +66,10 @@ public ResponseEntity<ExploreTemplatesResponse> explore() {
}

@GetMapping("/{id}")
public ResponseEntity<FindTemplateResponse> getTemplateById(@PathVariable Long id, @BasicAuthentication MemberDto memberDto) {
return ResponseEntity.ok(templateService.findByIdAndMember(id, memberDto));
}

@GetMapping("/search")
public ResponseEntity<FindAllMyTemplatesResponse> getMyTemplatesContainTopic(
@BasicAuthentication MemberDto memberDto,
@RequestParam("memberId") Long memberId,
@RequestParam("topic") String topic,
@PageableDefault Pageable pageable
public ResponseEntity<FindTemplateResponse> getTemplateById(@PathVariable Long id,
@BasicAuthentication MemberDto memberDto
) {
FindAllMyTemplatesResponse response = myTemplateFacadeService
.searchMyTemplatesContainTopic(memberDto, memberId, topic, pageable);
return ResponseEntity.ok(response);
return ResponseEntity.ok(templateService.findByIdAndMember(id, memberDto));
}

@PostMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;

import codezap.category.domain.Category;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ default Tag fetchById(Long id) {

Optional<Tag> findByName(String name);

List<Tag> findByNameIn(List<String> tagNames);
List<Tag> findByIdIn(List<Long> tagIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -27,26 +26,75 @@ default Template fetchById(Long id) {
FROM Template t JOIN Snippet s ON t.id = s.template.id
WHERE t.member.id = :memberId AND
(
t.title LIKE :topic
OR s.filename LIKE :topic
OR s.content LIKE :topic
OR t.description LIKE :topic
t.title LIKE :keyword
OR s.filename LIKE :keyword
OR s.content LIKE :keyword
OR t.description LIKE :keyword
)
""")
Page<Template> searchByTopic(@Param("memberId") Long memberId, @Param("topic") String topic, Pageable pageable);
Page<Template> searchBy(
@Param("memberId") Long memberId,
@Param("keyword") String keyword,
Pageable pageable
);

Page<Template> findBy(Pageable pageable);

Page<Template> findByCategoryId(Pageable pageable, Long categoryId);

Page<Template> findByIdIn(PageRequest pageRequest, List<Long> templateIds);

Page<Template> findByIdInAndCategoryId(PageRequest pageRequest, List<Long> templateIds, Long categoryId);

long countByCategoryId(Long categoryId);
@Query("""
SELECT DISTINCT t
FROM Template t JOIN Snippet s ON t.id = s.template.id
WHERE t.member.id = :memberId AND
t.id in :templateIds AND
(
t.title LIKE :keyword
OR s.filename LIKE :keyword
OR s.content LIKE :keyword
OR t.description LIKE :keyword
)
""")
Page<Template> searchBy(
@Param("memberId") Long memberId,
@Param("keyword") String keyword,
@Param("templateIds") List<Long> templateIds,
Pageable pageable
);

long countByIdInAndCategoryId(List<Long> templateIds, Long categoryId);
@Query("""
SELECT DISTINCT t
FROM Template t JOIN Snippet s ON t.id = s.template.id
WHERE t.member.id = :memberId AND
t.category.id = :categoryId AND
(
t.title LIKE :keyword
OR s.filename LIKE :keyword
OR s.content LIKE :keyword
OR t.description LIKE :keyword
)
""")
Page<Template> searchBy(
@Param("memberId") Long memberId,
@Param("keyword") String keyword,
@Param("categoryId") Long categoryId,
Pageable pageable
);

long countByIdIn(List<Long> templateIds);
@Query("""
SELECT DISTINCT t
FROM Template t JOIN Snippet s ON t.id = s.template.id
WHERE t.member.id = :memberId AND
t.category.id = :categoryId AND
t.id in :templateIds AND
(
t.title LIKE :keyword
OR s.filename LIKE :keyword
OR s.content LIKE :keyword
OR t.description LIKE :keyword
)
""")
Page<Template> searchBy(
@Param("memberId") Long memberId,
@Param("keyword") String keyword,
@Param("categoryId") Long categoryId,
@Param("templateIds") List<Long> templateIds,
Pageable pageable
);

}
Loading

0 comments on commit 0ed803c

Please sign in to comment.