Skip to content

Commit

Permalink
Refactor/#78 multiple selection city (#81)
Browse files Browse the repository at this point in the history
* refactor: in 사용하여 지역 중복 선택 가능하도록 변경

* del: 필요없는 예외처리 삭제
  • Loading branch information
onpyeong authored Aug 16, 2023
1 parent 416530e commit 5b4604b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ protected ResponseEntity<ApiResponseDto<?>> handleSophyJwtException(SophyJwtExce
return ResponseEntity.status(e.getHttpStatus())
.body(ApiResponseDto.error(e.getHttpStatus(), e.getMessage()));
}
}
}
22 changes: 14 additions & 8 deletions src/main/java/org/sophy/sophy/controller/BooktalkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.common.dto.ApiResponseDto;
import org.sophy.sophy.domain.dto.booktalk.request.BooktalkParticipationRequestDto;
Expand All @@ -15,10 +17,14 @@
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand All @@ -38,13 +44,13 @@ public ApiResponseDto<BooktalkDetailResponseDto> getBooktalkDetail(
booktalkService.getBooktalkDetail(booktalkId));
}

@GetMapping("/search/{city}")
@GetMapping("/search")
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "지역으로 북토크 조회")
public ApiResponseDto<List<BooktalkResponseDto>> getPlacesByCity(
@Parameter(description = "지역 이름", example = "UIJEONGBU_DONG") @Valid @PathVariable(name = "city") City city) {
public ApiResponseDto<List<BooktalkResponseDto>> getBooktalksByCities(
@Parameter(description = "지역 이름", example = "UIJEONGBU_DONG") @Valid @RequestParam(name = "cities") List<City> cities) {
return ApiResponseDto.success(SuccessStatus.GET_BOOKTALKS_BY_CITY_SUCCESS,
booktalkService.getBooktalksByCity(city));
booktalkService.getBooktalksByCities(cities));
}

@PostMapping("/participation")
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/sophy/sophy/controller/PlaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.common.dto.ApiResponseDto;
import org.sophy.sophy.controller.dto.response.PlaceResponseDto;
Expand All @@ -14,10 +16,13 @@
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
Expand All @@ -38,11 +43,11 @@ public ApiResponseDto<PlaceResponseDto> createPlace(
placeService.createPlace(placeRequestDto, user.getUsername()));
}

@GetMapping("/search/{city}")
@GetMapping("/search")
@Operation(summary = "지역으로 공간 조회")
public ApiResponseDto<List<PlaceResponseDto>> getPlacesByCity(
@Parameter(description = "지역 이름", example = "UIJEONGBU_DONG") @Valid @PathVariable(name = "city") City city) {
public ApiResponseDto<List<PlaceResponseDto>> getPlacesByCities(
@Parameter(description = "지역 이름", example = "UIJEONGBU_DONG") @Valid @RequestParam(name = "cities") List<City> cities) {
return ApiResponseDto.success(SuccessStatus.GET_PLACES_BY_CITY_SUCCESS,
placeService.getPlacesByCity(city));
placeService.getPlacesByCities(cities));
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package org.sophy.sophy.infrastructure;

import org.sophy.sophy.domain.enumerate.City;
import java.util.List;
import org.sophy.sophy.domain.Place;
import org.sophy.sophy.domain.enumerate.City;
import org.sophy.sophy.exception.ErrorStatus;
import org.sophy.sophy.exception.model.NotFoundException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PlaceRepository extends JpaRepository<Place, Long> {
List<Place> findAllByCity(City city);

List<Place> findByCityIn(List<City> cities);

List<Place> findAll();

default Place getPlaceById(Long placeId) {
return this.findById(placeId)
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_PLACE_EXCEPTION, ErrorStatus.NOT_FOUND_PLACE_EXCEPTION.getMessage()));
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_PLACE_EXCEPTION,
ErrorStatus.NOT_FOUND_PLACE_EXCEPTION.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ List<BooktalkResponseDto> findBooktalkResponseDto(
+ " from Booktalk b"
+ " join b.member m"
+ " join b.place p"
+ " where b.city = :city"
+ " where b.city in :cities"
+ " and b.booktalkStatus = :booktalkStatus"
+ " order by b.startDate")
List<BooktalkResponseDto> findBooktalkResponseDto(@Param("city") City city,
List<BooktalkResponseDto> findBooktalkResponseDto(@Param("cities") List<City> cities,
@Param("booktalkStatus") BooktalkStatus booktalkStatus); //count 쿼리는 매우 무거운데 이거를 참가자 리스트를 불러와 count를 세는게 맞나?

//sql의 in절을 통해 모집중과 모집 마감 북토크들을 한번에 조회
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/sophy/sophy/service/BooktalkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,18 @@ public List<BooktalkDeadlineUpcomingDto> getBooktalkDeadlineUpcoming() {

//지역으로 북토크 조회
@Transactional
public List<BooktalkResponseDto> getBooktalksByCity(City city) {
public List<BooktalkResponseDto> getBooktalksByCities(List<City> cities) {

List<BooktalkResponseDto> booktalkList;
/*
@Query를 통해 Dto로 직접 조회 및 정렬까지 구현 가능할 듯
Dto로 직접 조회할 땐 페이징이 불가능 한 문제가 생길 수 있지만 여기선 ToMany관계가 없어서 가능할 듯
*/
if (city.equals(City.UIJEONGBU_SI)) { //모집중인 북토크만 조회
if (cities.contains(City.UIJEONGBU_SI)) { //모집중인 북토크만 조회
booktalkList = booktalkQueryRepository.findBooktalkResponseDto(
BooktalkStatus.RECRUITING);
} else {
booktalkList = booktalkQueryRepository.findBooktalkResponseDto(city,
booktalkList = booktalkQueryRepository.findBooktalkResponseDto(cities,
BooktalkStatus.RECRUITING);
}

Expand Down
11 changes: 5 additions & 6 deletions src/main/java/org/sophy/sophy/service/PlaceService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.sophy.sophy.service;

import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.sophy.sophy.controller.dto.response.PlaceResponseDto;
import org.sophy.sophy.domain.Member;
Expand All @@ -15,9 +17,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PlaceService {
Expand All @@ -42,14 +41,14 @@ public PlaceResponseDto createPlace(PlaceRequestDto placeRequestDto, String emai
return PlaceResponseDto.of(placeRepository.save(place));
}

public List<PlaceResponseDto> getPlacesByCity(City city) {
public List<PlaceResponseDto> getPlacesByCities(List<City> cities) {
//의정부 시이면 전체 조회
if (city == City.UIJEONGBU_SI) {
if (cities.contains(City.UIJEONGBU_SI)) {
return placeRepository.findAll().stream()
.map(PlaceResponseDto::of)
.collect(Collectors.toList());
}
return placeRepository.findAllByCity(city).stream()
return placeRepository.findByCityIn(cities).stream()
.map(PlaceResponseDto::of)
.collect(Collectors.toList());
}
Expand Down

0 comments on commit 5b4604b

Please sign in to comment.