Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ARV-194] 공연장 메인 페이지 API 구현 #173

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.backend.allreva.hall.exception;

import com.backend.allreva.common.exception.code.ErrorCode;
import com.backend.allreva.common.exception.code.ErrorCodeInterface;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
@Getter
@RequiredArgsConstructor
public enum ConcertHallErrorCode implements ErrorCodeInterface {
CONCERT_HALL_SEARCH_NOTFOUND(HttpStatus.NO_CONTENT.value(), "CONCERT_HALL_SEARCH_NOT_FOUND", "존재하는 공연장이 없습니다");

private final Integer status;
private final String errorCode;
private final String message;

@Override
public ErrorCode getErrorCode() {
return ErrorCode.of(status, errorCode, message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.backend.allreva.hall.exception;

import com.backend.allreva.common.exception.CustomException;

public class ConcertHallNotFoundException extends CustomException {
public ConcertHallNotFoundException() {
super(ConcertHallErrorCode.CONCERT_HALL_SEARCH_NOTFOUND);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.backend.allreva.hall.infra.elasticcsearch;

import com.backend.allreva.hall.query.domain.ConcertHallDocument;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface ConcertHallSearchRepository extends
ElasticsearchRepository<ConcertHallDocument, String>,
CustomConcertHallSearchRepo {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.backend.allreva.hall.infra.elasticcsearch;

import com.backend.allreva.hall.query.domain.ConcertHallDocument;
import org.springframework.data.elasticsearch.core.SearchHits;

import java.util.List;

public interface CustomConcertHallSearchRepo {
SearchHits<ConcertHallDocument> searchMainConcertHall(
String address,
Integer minSeatSize,
List<Object> searchAfter,
int size
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.backend.allreva.hall.infra.elasticcsearch;


import co.elastic.clients.elasticsearch._types.SortOptions;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import co.elastic.clients.json.JsonData;
import com.backend.allreva.concert.exception.search.ElasticSearchException;
import com.backend.allreva.hall.query.domain.ConcertHallDocument;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.client.elc.NativeQueryBuilder;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.util.StringUtils;
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
import java.util.List;

@RequiredArgsConstructor
public class CustomConcertHallSearchRepoImpl implements CustomConcertHallSearchRepo {
private final ElasticsearchOperations elasticsearchOperations;

@Override
public SearchHits<ConcertHallDocument> searchMainConcertHall(
final String address,
final Integer minSeatSize,
final List<Object> searchAfter,
final int size) {
try {
NativeQuery searchQuery = getNativeQuery(address, minSeatSize, searchAfter, size);
return elasticsearchOperations.search(searchQuery, ConcertHallDocument.class);
} catch (ElasticSearchException e) {
throw new ElasticSearchException();
}
}

private NativeQuery getNativeQuery(
final String address,
final Integer minSeatSize,
final List<Object> searchAfter,
final int size) {

NativeQueryBuilder nativeQueryBuilder = NativeQuery.builder()
.withQuery(buildSearchQuery(address, minSeatSize))
.withPageable(PageRequest.of(0, size));

// 정렬 조건 추가
if (minSeatSize != 0) {
// seatScale이 있는 경우 우선 seatScale로 정렬
nativeQueryBuilder.withSort(SortOptions.of(s -> s
.field(f -> f
.field("seat_scale")
.order(SortOrder.Desc)
)
));
}

if (StringUtils.hasText(address)) {
// address가 있는 경우 _score로 정렬
nativeQueryBuilder.withSort(SortOptions.of(s -> s
.score(sc -> sc
.order(SortOrder.Desc)
)
));
}

// 마지막으로 항상 id로 정렬
nativeQueryBuilder.withSort(SortOptions.of(s -> s
.field(f -> f
.field("id")
.order(SortOrder.Asc)
)
));

if (searchAfter != null && !searchAfter.isEmpty()) {
nativeQueryBuilder.withSearchAfter(searchAfter);
}
return nativeQueryBuilder.build();
}

private Query buildSearchQuery(final String address, final Integer minSeatSize) {
if(!StringUtils.hasText(address)) {
return Query.of(q -> q.matchAll(m -> m));
}
return Query.of(q -> q
.bool(b -> {
BoolQuery.Builder builder = b
.must(m -> m
.match(mt -> mt
.field("address.mixed")
.query(address)
.fuzziness("AUTO")
)
);

if (minSeatSize != 0) {
builder.filter(f -> f
.range(r -> r
.field("seat_scale")
.gte(JsonData.of(minSeatSize))
)
);
}

return builder;
})
);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,57 @@
package com.backend.allreva.hall.query.application;

import com.backend.allreva.hall.command.domain.ConcertHallRepository;
import com.backend.allreva.hall.exception.ConcertHallNotFoundException;
import com.backend.allreva.hall.infra.elasticcsearch.ConcertHallSearchRepository;
import com.backend.allreva.hall.query.application.response.ConcertHallDetailResponse;
import com.backend.allreva.hall.query.application.response.ConcertHallMainResponse;
import com.backend.allreva.hall.query.application.response.ConcertHallThumbnail;
import com.backend.allreva.hall.query.domain.ConcertHallDocument;
import lombok.RequiredArgsConstructor;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class ConcertHallQueryService {

private final ConcertHallRepository concertHallRepository;
private final ConcertHallSearchRepository concertHallSearchRepository;

public ConcertHallDetailResponse findDetailByHallCode(final String hallCode) {
return concertHallRepository.findDetailByHallCode(hallCode);
}

public ConcertHallMainResponse getConcertHallMain(
final String address,
final int seatScale,
final List<Object> searchAfter,
final int size
) {
SearchHits<ConcertHallDocument> searchHits = concertHallSearchRepository.searchMainConcertHall(
address,
seatScale,
searchAfter,
size + 1
);
List<ConcertHallThumbnail> concertHall = searchHits.getSearchHits().stream()
.map(SearchHit::getContent)
.map(ConcertHallThumbnail::from)
.limit(size)
.toList();

if(concertHall.isEmpty()) {
throw new ConcertHallNotFoundException();
}

boolean hasNext = searchHits.getSearchHits().size() > size;
List<Object> nextSearchAfter = hasNext ?
searchHits.getSearchHits().get(size -1).getSortValues() : null;
return ConcertHallMainResponse.from(concertHall, nextSearchAfter);


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.backend.allreva.hall.query.application.response;

import java.util.List;

public record ConcertHallMainResponse(
List<ConcertHallThumbnail> concertHallThumbnails,
List<Object> searchAfter
) {
public static ConcertHallMainResponse from(final List<ConcertHallThumbnail> thumbnails, final List<Object> searchAfter) {
return new ConcertHallMainResponse(thumbnails, searchAfter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.backend.allreva.hall.query.application.response;

import com.backend.allreva.hall.command.domain.value.ConvenienceInfo;
import com.backend.allreva.hall.query.domain.ConcertHallDocument;

public record ConcertHallThumbnail(
String id,
String name,
String address,
int seatScale,
ConvenienceInfo convenienceInfo

) {
public static ConcertHallThumbnail from(final ConcertHallDocument document) {
ConvenienceInfo convenienceInfo = ConvenienceInfo.builder()
.hasParkingLot(document.getParking())
.hasRestaurant(document.getRestaurant())
.hasCafe(document.getCafe())
.hasStore(document.getStore())
.hasDisabledParking(document.getParkBarrier())
.hasDisabledToilet(document.getRestBarrier())
.hasElevator(document.getElevBarrier())
.hasRunway(document.getRunwBarrier())
.build();
return new ConcertHallThumbnail(
document.getId(),
document.getName(),
document.getAddress(),
document.getSeatScale(),
convenienceInfo
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.backend.allreva.hall.query.domain;

import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.elasticsearch.annotations.*;

@Document(indexName = "concerts_hall")
@Setting(settingPath = "elasticsearch/mappings/es-settings.json")
@Mapping(mappingPath = "elasticsearch/mappings/concert_hall-mapping.json")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ConcertHallDocument {
@Id
private String id;

@Field(type = FieldType.Boolean, name = "cafe")
private Boolean cafe;

@Field(type = FieldType.Boolean, name = "park_barrier")
private Boolean parkBarrier;

@Field(type = FieldType.Boolean, name = "rest_barrier")
private Boolean restBarrier;

@Field(type = FieldType.Boolean, name = "elev_barrier")
private Boolean elevBarrier;

@Field(type = FieldType.Boolean, name = "parking")
private Boolean parking;

@Field(type = FieldType.Boolean, name = "restaurant")
private Boolean restaurant;

@Field(type = FieldType.Boolean, name = "runw_barrier")
private Boolean runwBarrier;

@Field(type = FieldType.Boolean, name = "store")
private Boolean store;

@Field(type = FieldType.Text, name = "address", analyzer = "korean_mixed")
private String address;

@Field(type = FieldType.Double, name = "latitude")
private Double latitude;

@Field(type = FieldType.Double, name = "longitude")
private Double longitude;

@Field(type = FieldType.Text , name = "name" , analyzer = "korean_mixed")
private String name;

@Field(type = FieldType.Integer, name = "seat_scale")
private Integer seatScale;

@Field(type = FieldType.Double, name = "star")
private Double star;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.backend.allreva.common.dto.Response;
import com.backend.allreva.hall.query.application.ConcertHallQueryService;
import com.backend.allreva.hall.query.application.response.ConcertHallDetailResponse;
import com.backend.allreva.hall.query.application.response.ConcertHallMainResponse;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
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.RestController;
import org.springframework.web.bind.annotation.*;

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

@RequiredArgsConstructor
@RequestMapping("/api/v1/concert-halls")
Expand All @@ -25,4 +28,30 @@ public Response<ConcertHallDetailResponse> findHallDetailByHallCode(
ConcertHallDetailResponse details = concertHallQueryService.findDetailByHallCode(hallCode);
return Response.onSuccess(details);
}

@Operation(
summary = "공연장 메인 api 입니다.",
description = "searchAfter1, searchAfter2에 " +
"이전 SearchAfter에 있는 값들을 순서대로 넣어주어야 합니다."
)
@GetMapping("/list")
public Response<ConcertHallMainResponse> getConcertHallList(
@RequestParam(defaultValue = "") final String address,
@RequestParam(defaultValue = "0") final int seatScale,
@RequestParam(defaultValue = "7") final int pageSize,
@RequestParam(required = false) final String searchAfter1,
@RequestParam(required = false) final String searchAfter2,
@RequestParam(required = false) final String searchAfter3
) {
List<Object> searchAfter = Stream.of(searchAfter1, searchAfter2, searchAfter3)
.filter(Objects::nonNull)
.collect(Collectors.toList());
ConcertHallMainResponse concertHallMain = concertHallQueryService.getConcertHallMain(
address,
seatScale,
searchAfter,
pageSize
);
return Response.onSuccess(concertHallMain);
}
}
Loading
Loading