Skip to content

Commit

Permalink
feat: reload API에 스파크 미팅/클래스 카운트 적용 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ympark99 committed Oct 10, 2023
1 parent 5babcdf commit 543cb9c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import java.util.List;

import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceDetailInfo;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceSearchDto;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceDetailInfo;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceReloadDto;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceSearchDto;
import com.onna.onnaback.domain.place.application.port.in.PlaceUseCase;
import com.onna.onnaback.domain.place.domain.PlaceType;
import com.onna.onnaback.domain.spark.domain.DurationHour;
import com.onna.onnaback.domain.spark.domain.SortType;
import com.onna.onnaback.domain.spark.domain.SparkType;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -28,6 +33,7 @@ public ResponseEntity<List<PlaceReloadDto>> reload(
@RequestParam(value = "sparkType", required = false) SparkType sparkType,
@RequestParam(value = "durationHour", required = false) DurationHour durationHour,
@RequestParam(value = "placeType", required = false) PlaceType placeType,
@RequestParam(value = "sort", required = false) SortType sortType,
@RequestParam(value = "southwestLongitude") Double southwestLongitude,
@RequestParam(value = "northeastLongitude") Double northeastLongitude,
@RequestParam(value = "southwestLatitude") Double southwestLatitude,
Expand All @@ -38,21 +44,21 @@ public ResponseEntity<List<PlaceReloadDto>> reload(
sparkType,
durationHour,
placeType,
sortType,
southwestLongitude, northeastLongitude,
southwestLatitude, northeastLatitude)
);
}

@Operation(description = "장소 키워드 검색 API")
@GetMapping("/search/{value}")
public ResponseEntity<List<PlaceSearchDto>> searchPlace(@PathVariable("value")String value){
public ResponseEntity<List<PlaceSearchDto>> searchPlace(@PathVariable("value") String value) {
return ResponseEntity.ok().body(this.placeUseCase.searchPlace(value));
}

@Operation(description = "장소 상세조회")
@GetMapping("/{placeId}")
public ResponseEntity<PlaceDetailInfo> getPlaceDetail(@PathVariable("placeId") Long placeId)
{
public ResponseEntity<PlaceDetailInfo> getPlaceDetail(@PathVariable("placeId") Long placeId) {
return ResponseEntity.ok().body(this.placeUseCase.getPlaceInfo(placeId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.onna.onnaback.domain.place.domain.Place;
import com.onna.onnaback.domain.place.domain.PlaceType;
import com.onna.onnaback.domain.spark.domain.DurationHour;
import com.onna.onnaback.domain.spark.domain.RecruitType;
import com.onna.onnaback.domain.spark.domain.SortType;
import com.onna.onnaback.domain.spark.domain.Spark;
import com.onna.onnaback.domain.spark.domain.SparkType;

Expand All @@ -32,6 +34,7 @@ public class PlacePersistenceAdapter implements LoadPlacePort {

@Override
public List<PlaceReloadDto> getMarkers(SparkType sparkType, DurationHour durationHour, PlaceType placeType,
SortType sortType,
Double southwestLongitude, Double northeastLongitude,
Double southwestLatitude, Double northeastLatitude) {
Specification<Place> spec = Specification.where(null);
Expand All @@ -48,6 +51,11 @@ public List<PlaceReloadDto> getMarkers(SparkType sparkType, DurationHour duratio
spec = spec.and(hasPlaceType(placeType));
}

// 모집중
if (sortType == SortType.RECRUITING) {
spec = spec.and(hasRecruiting());
}

spec = spec.and(hasLocationBetween(southwestLongitude, northeastLongitude, southwestLatitude,
northeastLatitude));

Expand All @@ -59,7 +67,7 @@ public List<PlaceReloadDto> getMarkers(SparkType sparkType, DurationHour duratio

for (Place place : places) {
if (!uniquePlaceIds.contains(place.getPlaceId())) {
Long sparkCount = calculateSparkCount(place);
Long sparkCount = calculateSparkCount(place, sparkType, durationHour, sortType);
result.add(new PlaceReloadDto(place.getPlaceId(),
place.getPlaceType(),
place.getLongitude(),
Expand All @@ -72,8 +80,29 @@ public List<PlaceReloadDto> getMarkers(SparkType sparkType, DurationHour duratio
return result;
}

private Long calculateSparkCount(Place place) {
return (long) place.getSparkList().size();
// 필터에 맞는 스파크 클래스/미팅 개수 구함
private Long calculateSparkCount(Place place, SparkType sparkType,
DurationHour durationHour,
SortType sortType) {
List<Spark> sparks = place.getSparkList();
Long sparkCnt = (long) place.getSparkList().size();

for (Spark spark : sparks) {
if (sparkType != null && spark.getType() != sparkType) {
sparkCnt--;
continue;
}
if (durationHour != null && spark.getDurationHour() != durationHour) {
sparkCnt--;
continue;
}
// 모집중
if (sortType == SortType.RECRUITING && spark.getRecruitType() != RecruitType.RECRUITING) {
sparkCnt--;
}
}

return sparkCnt;
}

@Override
Expand Down Expand Up @@ -124,6 +153,18 @@ private Specification<Place> hasPlaceType(PlaceType placeType) {
return (root, query, criteriaBuilder) -> criteriaBuilder.equal(root.get("placeType"), placeType);
}

private Specification<Place> hasRecruiting() {
return (root, query, criteriaBuilder) -> {
List<Predicate> predicates = new ArrayList<>();

Join<Place, Spark> sparkJoin = root.join("sparkList");

predicates.add(criteriaBuilder.equal(sparkJoin.get("recruitType"), RecruitType.RECRUITING));

return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
};
}

private Specification<Place> hasLocationBetween(Double southwestLongitude, Double northeastLongitude,
Double southwestLatitude, Double northeastLatitude) {
return (root, query, criteriaBuilder) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceDetailInfo;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceReloadDto;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceResponse;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceSearchDto;
import com.onna.onnaback.domain.place.domain.Place;
import com.onna.onnaback.domain.place.domain.PlaceType;
import com.onna.onnaback.domain.spark.domain.DurationHour;
import com.onna.onnaback.domain.spark.domain.SortType;
import com.onna.onnaback.domain.spark.domain.SparkType;

public interface PlaceUseCase {
List<PlaceReloadDto> reload(
SparkType sparkType, DurationHour durationHour, PlaceType placeType,
Double southwestLongitude, Double northeastLongitude,
SortType sortType, Double southwestLongitude, Double northeastLongitude,
Double southwestLatitude, Double northeastLatitude
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import java.util.List;
import java.util.Optional;

import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceReloadDto;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceReloadDto;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceSearchDto;
import com.onna.onnaback.domain.place.domain.Place;
import com.onna.onnaback.domain.place.domain.PlaceType;
import com.onna.onnaback.domain.spark.domain.DurationHour;
import com.onna.onnaback.domain.spark.domain.SortType;
import com.onna.onnaback.domain.spark.domain.SparkType;

public interface LoadPlacePort {

List<PlaceReloadDto> getMarkers(
SparkType sparkType, DurationHour durationHour, PlaceType placeType,
Double southwestLongitude, Double northeastLongitude,
SortType sortType, Double southwestLongitude, Double northeastLongitude,
Double southwestLatitude, Double northeastLatitude
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.onna.onnaback.domain.place.application.service;

import java.util.List;
import java.util.Optional;

import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceDetailInfo;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceResponse;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceSearchDto;
import com.onna.onnaback.global.exception.BaseException;
import com.onna.onnaback.global.exception.ErrorCode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceDetailInfo;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceReloadDto;
import com.onna.onnaback.domain.place.adapter.in.web.response.PlaceSearchDto;
import com.onna.onnaback.domain.place.application.port.in.PlaceUseCase;
import com.onna.onnaback.domain.place.application.port.out.LoadPlacePort;
import com.onna.onnaback.domain.place.domain.Place;
import com.onna.onnaback.domain.place.domain.PlaceType;
import com.onna.onnaback.domain.spark.domain.DurationHour;
import com.onna.onnaback.domain.spark.domain.SortType;
import com.onna.onnaback.domain.spark.domain.SparkType;
import com.onna.onnaback.global.exception.BaseException;
import com.onna.onnaback.global.exception.ErrorCode;

import lombok.RequiredArgsConstructor;

Expand All @@ -30,15 +29,15 @@ public class PlaceService implements PlaceUseCase {
@Override
public List<PlaceReloadDto> reload(
SparkType sparkType, DurationHour durationHour, PlaceType placeType,
Double southwestLongitude, Double northeastLongitude,
SortType sortType, Double southwestLongitude, Double northeastLongitude,
Double southwestLatitude, Double northeastLatitude
) {
// todo: 부산 외곽의 경우 에러 처리

// 스파크 클래스/미팅 카운트 가져오기
return loadPlacePort.getMarkers(
sparkType, durationHour, placeType,
southwestLongitude, northeastLongitude,
sortType, southwestLongitude, northeastLongitude,
southwestLatitude, northeastLatitude);
}

Expand All @@ -54,17 +53,17 @@ public List<PlaceSearchDto> searchPlace(String value) {

@Override
public PlaceDetailInfo getPlaceInfo(Long placeId) {
Place place = loadPlacePort.getById(placeId).orElseThrow(()-> new BaseException(ErrorCode.NOT_FOUND));
Place place = loadPlacePort.getById(placeId).orElseThrow(() -> new BaseException(ErrorCode.NOT_FOUND));
return PlaceDetailInfo.builder()
.id(placeId)
.detailInfo(place.getDetailInfo())
.img(place.getImg())
.detailAddress(place.getDetailAddress())
.businessHour(place.getBusinessHour())
.description(place.getDescription())
.name(place.getName())
.phoneNum(place.getPhoneNum())
.build();
.id(placeId)
.detailInfo(place.getDetailInfo())
.img(place.getImg())
.detailAddress(place.getDetailAddress())
.businessHour(place.getBusinessHour())
.description(place.getDescription())
.name(place.getName())
.phoneNum(place.getPhoneNum())
.build();
}

}

0 comments on commit 543cb9c

Please sign in to comment.