Skip to content

Commit

Permalink
[FEAT] 필터링 기능 추가 진행중
Browse files Browse the repository at this point in the history
- Specification 객체 만들어서 진행중
  • Loading branch information
eunsol-an committed Dec 27, 2022
1 parent accbec0 commit 7a79bb6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.cloneweek.hanghaebnb.service.RoomService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -34,19 +35,19 @@ public ResponseEntity<ResponseMsgDto> createRoom(@RequestPart(value = "data") Ro
//숙소 전체 조회
@GetMapping("/rooms") // size '/api/rooms?page=0&size=3'
public ResponseEntity<List<RoomResponseDto>> getRooms(@AuthenticationPrincipal UserDetailsImpl userDetails,
Pageable pageable) {
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable) {
return ResponseEntity.ok(roomService.getRooms(pageable, userDetails.getUser()));
}

//숙소 키워드 조회
@GetMapping("/rooms/search") // '/api/rooms/search?keyword=제목&page=0&size=2'
public ResponseEntity<List<RoomResponseDto>> search(@AuthenticationPrincipal UserDetailsImpl userDetails,
@PageableDefault Pageable pageable,
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
String keyword){
return ResponseEntity.ok(roomService.search(keyword, pageable, userDetails.getUser()));
}

//숙수 정보 수정
//숙소 정보 수정
@CrossOrigin
@PatchMapping("/rooms/{roomId}")
public ResponseEntity<RoomResponseDto> updateRoom(@PathVariable Long roomId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
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.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface RoomRepository extends JpaRepository<Room, Long> {
public interface RoomRepository extends JpaRepository<Room, Long>, JpaSpecificationExecutor<Room> {
Page<Room> findAll(Pageable pageable);
// List<Room> findAllByOrderByCreatedAtDesc();

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/cloneweek/hanghaebnb/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public List<RoomResponseDto> getRooms(Pageable pageable, User user) {

// 페이징 처리
// 작성날짜 순으로 정렬
Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");

pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
// Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");
//
// pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
// Page<Room> rooms = roomRepository.findAll(pageable);

Page<Room> roomList = roomRepository.findAll(pageable);
Expand All @@ -73,9 +73,9 @@ public List<RoomResponseDto> getRooms(Pageable pageable, User user) {
public List<RoomResponseDto> search(String keyword, Pageable pageable, User user) {
// 페이징 처리
// 작성날짜 순으로 정렬
Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");

pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);
// Sort sort = Sort.by(Sort.Direction.DESC, "createdAt");
//
// pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), sort);

Page<Room> roomList = roomRepository.findByTitleContaining(keyword, pageable);
List<RoomResponseDto> roomResponseDtos = new ArrayList<>();
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/cloneweek/hanghaebnb/service/RoomSpecs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.cloneweek.hanghaebnb.service;

import com.cloneweek.hanghaebnb.entity.Room;
import org.springframework.data.jpa.domain.Specification;

public class RoomSpecs {
public static Specification<Room> whitType(String type) {
return (Specification<Room>) ((root, query ,builder) ->
builder.equal(root.get("type"), type)
);
}

public enum SearchKey {

}
}

0 comments on commit 7a79bb6

Please sign in to comment.