Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -26,4 +26,5 @@ public class ExhibitDetailResponse {
private Double hallLatitude;
private Double hallLongitude;

private boolean isFavorite;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.atdev.artrip.domain.exhibit.data.Exhibit;
import org.atdev.artrip.domain.exhibit.reponse.ExhibitDetailResponse;
import org.atdev.artrip.domain.exhibit.repository.ExhibitRepository;
import org.atdev.artrip.domain.favortie.repository.FavoriteExhibitRepository;
import org.atdev.artrip.domain.home.converter.HomeConverter;
import org.atdev.artrip.global.apipayload.code.status.ExhibitError;
import org.atdev.artrip.global.apipayload.exception.GeneralException;
Expand All @@ -15,14 +16,20 @@ public class ExhibitService {

private final ExhibitRepository exhibitRepository;
private final HomeConverter homeConverter;
private final FavoriteExhibitRepository favoriteExhibitRepository;


public ExhibitDetailResponse getExhibitDetail(Long exhibitId) {
public ExhibitDetailResponse getExhibitDetail(Long exhibitId, Long userId) {

Exhibit exhibit = exhibitRepository.findById(exhibitId)
.orElseThrow(() -> new GeneralException(ExhibitError._EXHIBIT_NOT_FOUND));

return homeConverter.toHomeExhibitResponse(exhibit);
boolean isFavorite = false;
if (userId != null ) {
isFavorite = favoriteExhibitRepository.existsActive(userId, exhibitId);
}

return homeConverter.toHomeExhibitResponse(exhibit, isFavorite);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -26,7 +28,9 @@ public class ExhibitController {
private final HomeService homeService;
private final ExhibitService exhibitService;


private Long getUserId(UserDetails userDetails) {
return userDetails != null ? Long.parseLong(userDetails.getUsername()) : null;
}
@Operation(summary = "장르 조회", description = "키워드 장르 데이터 전체 조회")
@ApiErrorResponses(
common = {CommonError._BAD_REQUEST, CommonError._UNAUTHORIZED},
Expand All @@ -45,9 +49,11 @@ public ResponseEntity<CommonResponse<List<String>>> getGenres(){
)
@GetMapping("/{id}")
public ResponseEntity<CommonResponse<ExhibitDetailResponse>> getExhibit(
@PathVariable Long id){
@PathVariable Long id,
@AuthenticationPrincipal UserDetails userDetails) {

ExhibitDetailResponse exhibit= exhibitService.getExhibitDetail(id);
Long userId = getUserId(userDetails);
ExhibitDetailResponse exhibit= exhibitService.getExhibitDetail(id, userId);

return ResponseEntity.ok(CommonResponse.onSuccess(exhibit));
}
Expand Down Expand Up @@ -86,9 +92,10 @@ public ResponseEntity<CommonResponse<List<String>>> getDomestic(){
@PostMapping("/filter")
public ResponseEntity<FilterResponse> getDomesticFilter(@RequestBody ExhibitFilterRequest dto,
@RequestParam(required = false) Long cursor,
@PageableDefault(size = 20) Pageable pageable){

FilterResponse exhibits = homeService.getFilterExhibit(dto, pageable, cursor);
@PageableDefault(size = 20) Pageable pageable,
@AuthenticationPrincipal UserDetails userDetails) {
Long userId = getUserId(userDetails);
FilterResponse exhibits = homeService.getFilterExhibit(dto, pageable, cursor,userId);

return ResponseEntity.ok(exhibits);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface ExhibitHallRepository extends JpaRepository<ExhibitHall, Long>
@Query("SELECT DISTINCT e.country FROM ExhibitHall e WHERE e.country <> '한국'")
List<String> findAllOverseasCountries();

@Query("SELECT DISTINCT e.region FROM ExhibitHall e WHERE e.country = '한국'")
@Query("SELECT DISTINCT e.region FROM ExhibitHall e WHERE e.country IN ('한국', '대한민국')")
List<String> findAllDomesticRegions();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public interface FavoriteExhibitRepository extends JpaRepository<FavoriteExhibit, Long> {
@Query("""
Expand Down Expand Up @@ -86,4 +87,11 @@ List<String> findDatesByYearMonth(
AND f.exhibit.exhibitId = :exhibitId
""")
Optional<FavoriteExhibit> findByUserAndExhibit(@Param("userId") Long userId, @Param("exhibitId") Long exhibitId);

@Query("""
SELECT f.exhibit.exhibitId
FROM FavoriteExhibit f
WHERE f.user.userId = :userId AND f.status = true
""")
Set<Long> findActiveExhibitIds(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private FavoriteResponse toFavoriteResponse(FavoriteExhibit favorite) {
.title(exhibit.getTitle())
.posterUrl(exhibit.getPosterUrl())
.exhibitStatus(exhibit.getStatus())
.favoriteStatus(favorite.isStatus())
.active(favorite.isStatus())
.exhibitPeriod(period)
.exhibitHallName(hall != null ? hall.getName() : null )
.country(hall != null ? hall.getCountry() : null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class FavoriteResponse {
private String title;
private String posterUrl;
private Status exhibitStatus;
private boolean favoriteStatus;
private boolean active;
private String exhibitPeriod;
private String exhibitHallName;
private String country;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class HomeConverter {

private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");

public FilterResponse toFilterResponse(Slice<Exhibit> slice) {
public FilterResponse toFilterResponse(Slice<Exhibit> slice, Set<Long> favorites) {

List<HomeListResponse> postDtos = slice.getContent()
.stream()
.map(this::toHomeExhibitListResponse)
.map(exhibit -> toHomeExhibitListResponse(exhibit, favorites.contains(exhibit.getExhibitId())))
.toList();

Long nextCursor = slice.hasNext()
Expand All @@ -35,7 +35,7 @@ public FilterResponse toFilterResponse(Slice<Exhibit> slice) {
}


public HomeListResponse toHomeExhibitListResponse(Exhibit exhibit){
public HomeListResponse toHomeExhibitListResponse(Exhibit exhibit, boolean isFavorite) {

String period = exhibit.getStartDate().format(formatter) + " - " + exhibit.getEndDate().format(formatter);

Expand All @@ -45,10 +45,11 @@ public HomeListResponse toHomeExhibitListResponse(Exhibit exhibit){
.posterUrl(exhibit.getPosterUrl())
.status(exhibit.getStatus())
.exhibitPeriod(period)
.isFavorite(isFavorite)
.build();
}

public ExhibitDetailResponse toHomeExhibitResponse(Exhibit exhibit) {
public ExhibitDetailResponse toHomeExhibitResponse(Exhibit exhibit, boolean isFavorite) {

var hall = exhibit.getExhibitHall();
String period = exhibit.getStartDate().format(formatter) + " - " + exhibit.getEndDate().format(formatter);
Expand All @@ -64,13 +65,13 @@ public ExhibitDetailResponse toHomeExhibitResponse(Exhibit exhibit) {
.ticketUrl(exhibit.getTicketUrl())
.status(exhibit.getStatus())
.exhibitPeriod(period)

.hallName(hall != null ? hall.getName() : null)// exhibit과 exhibithall이 연결되어있지않아도 체크 가능
.hallAddress(hall != null ? hall.getAddress() : null)
.hallOpeningHours(hall != null ? hall.getOpeningHours() : null)
.hallPhone(hall != null ? hall.getPhone() : null)
.hallLatitude(lat)
.hallLongitude(lng)
.isFavorite(isFavorite)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,19 @@ public class HomeListResponse {
private String countryName;
private String regionName;

private boolean isFavorite;

public HomeListResponse(Long exhibit_id, String title, String posterUrl, Status status,
String exhibitPeriod, String hallName, String countryName, String regionName) {
this.exhibit_id = exhibit_id;
this.title = title;
this.posterUrl = posterUrl;
this.status = status;
this.exhibitPeriod = exhibitPeriod;
this.hallName = hallName;
this.countryName = countryName;
this.regionName = regionName;
this.isFavorite = false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.atdev.artrip.domain.exhibit.data.Exhibit;
import org.atdev.artrip.domain.exhibit.web.dto.request.ExhibitFilterRequest;
import org.atdev.artrip.domain.exhibitHall.repository.ExhibitHallRepository;
import org.atdev.artrip.domain.favortie.repository.FavoriteExhibitRepository;
import org.atdev.artrip.domain.home.converter.HomeConverter;
import org.atdev.artrip.domain.home.response.FilterResponse;

Expand All @@ -22,7 +23,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
Expand All @@ -33,8 +36,14 @@ public class HomeService {
private final ExhibitHallRepository exhibitHallRepository;
private final UserRepository userRepository;
private final HomeConverter homeConverter;
private final FavoriteExhibitRepository favoriteExhibitRepository;


private Set<Long> getFavoriteIds(Long userId) {
if (userId == null) {
return Collections.emptySet();
}
return favoriteExhibitRepository.findActiveExhibitIds(userId);
}

// // 큐레이션 전시
// public List<HomeExhibitResponse> getCuratedExhibits() {
Expand All @@ -44,6 +53,9 @@ public class HomeService {
// .toList();
// }

private void setFavorites(List<HomeListResponse> result, Set<Long> favoriteIds) {
result.forEach(r -> r.setFavorite(favoriteIds.contains(r.getExhibit_id())));
}

// 장르 전체 조회
public List<String> getAllGenres() {
Expand All @@ -62,11 +74,11 @@ public List<String> getDomestic(){


//필터 전체 조회
public FilterResponse getFilterExhibit(ExhibitFilterRequest dto, Pageable pageable, Long cursorId) {
public FilterResponse getFilterExhibit(ExhibitFilterRequest dto, Pageable pageable, Long cursorId, Long userId) {

Slice<Exhibit> slice = exhibitRepository.findExhibitByFilters(dto, pageable, cursorId);

return homeConverter.toFilterResponse(slice);
Set<Long> favoriteIds = getFavoriteIds(userId);
return homeConverter.toFilterResponse(slice, favoriteIds);
}

// 사용자 맞춤 전시 랜덤 추천
Expand All @@ -89,6 +101,9 @@ public List<HomeListResponse> getRandomPersonalized(Long userId, PersonalizedReq

List<HomeListResponse> results = exhibitRepository.findRandomExhibits(filter);

Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
Expand All @@ -100,11 +115,14 @@ public List<HomeListResponse> getRandomPersonalized(Long userId, PersonalizedReq
}

// 이번주 랜덤 전시 추천
public List<HomeListResponse> getRandomSchedule(ScheduleRandomRequest request){
public List<HomeListResponse> getRandomSchedule(ScheduleRandomRequest request, Long userId){

RandomExhibitRequest filter = homeConverter.from(request);
List<HomeListResponse> results = exhibitRepository.findRandomExhibits(filter);

Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
Expand All @@ -116,12 +134,15 @@ public List<HomeListResponse> getRandomSchedule(ScheduleRandomRequest request){
}

// 장르별 전시 랜덤 추천
public List<HomeListResponse> getRandomGenre(GenreRandomRequest request){
public List<HomeListResponse> getRandomGenre(GenreRandomRequest request, Long userId){

RandomExhibitRequest filter = homeConverter.fromGenre(request);

List<HomeListResponse> results = exhibitRepository.findRandomExhibits(filter);

Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
Expand All @@ -133,12 +154,15 @@ public List<HomeListResponse> getRandomGenre(GenreRandomRequest request){
}

// 오늘날 전시 랜덤 추천
public List<HomeListResponse> getRandomToday(TodayRandomRequest request){
public List<HomeListResponse> getRandomToday(TodayRandomRequest request, Long userId){

RandomExhibitRequest filter = homeConverter.fromToday(request);

List<HomeListResponse> results = exhibitRepository.findRandomExhibits(filter);

Set<Long> favoriteIds = getFavoriteIds(userId);
setFavorites(results, favoriteIds);

adjustLocationFields(
results,
request.getIsDomestic(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ public ResponseEntity<CommonResponse<List<HomeListResponse>>> getRandomPersonali
)
@PostMapping("/schedule")
public ResponseEntity<CommonResponse<List<HomeListResponse>>> getRandomSchedule(
@Valid @RequestBody ScheduleRandomRequest request){
@Valid @RequestBody ScheduleRandomRequest request,
@AuthenticationPrincipal UserDetails userDetails){

List<HomeListResponse> exhibits= homeService.getRandomSchedule(request);
Long userId = Long.parseLong(userDetails.getUsername());
List<HomeListResponse> exhibits= homeService.getRandomSchedule(request, userId);

return ResponseEntity.ok(CommonResponse.onSuccess(exhibits));
}
Expand Down Expand Up @@ -112,9 +114,11 @@ public ResponseEntity<CommonResponse<List<HomeListResponse>>> getRandomSchedule(
)
@PostMapping("/genre/random")
public ResponseEntity<CommonResponse<List<HomeListResponse>>> getRandomExhibits(
@Valid @RequestBody GenreRandomRequest request){
@Valid @RequestBody GenreRandomRequest request,
@AuthenticationPrincipal UserDetails userDetails){

List<HomeListResponse> exhibits = homeService.getRandomGenre(request);
Long userId = Long.parseLong(userDetails.getUsername());
List<HomeListResponse> exhibits = homeService.getRandomGenre(request, userId);
return ResponseEntity.ok(CommonResponse.onSuccess(exhibits));
}

Expand All @@ -140,9 +144,11 @@ public ResponseEntity<CommonResponse<List<HomeListResponse>>> getRandomExhibits(
)
@PostMapping("recommend/today")
public ResponseEntity<CommonResponse<List<HomeListResponse>>> getTodayRecommendations(
@Valid @RequestBody TodayRandomRequest request){
@Valid @RequestBody TodayRandomRequest request,
@AuthenticationPrincipal UserDetails userDetails){

List<HomeListResponse> exhibits = homeService.getRandomToday(request);
Long userId = Long.parseLong(userDetails.getUsername());
List<HomeListResponse> exhibits = homeService.getRandomToday(request, userId);

return ResponseEntity.ok(CommonResponse.onSuccess(exhibits));
}
Expand Down
Loading