Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -24,4 +24,6 @@ public interface PostImageRepository extends JpaRepository<PostImage, Long> {
order by pi.id asc
""")
List<String> findImageUrlsByPost(@Param("post") StudentCouncilPost post);

List<PostImage> findAllByPostIn(List<StudentCouncilPost> posts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ Page<StudentCouncilPost> findUpcomingMajorEvents(
LEFT JOIN w.school s
LEFT JOIN w.college c
LEFT JOIN w.major m
JOIN p.place pl
WHERE w.deletedAt IS NULL
AND p.category = :category
AND p.startDateTime <= :now
Expand Down Expand Up @@ -249,7 +250,7 @@ List<StudentCouncilPost> findByUserScopeWithCursor(
SELECT p
FROM StudentCouncilPost p
JOIN p.writer w
JOIN p.place pl
JOIN FETCH p.place pl
LEFT JOIN w.school s
LEFT JOIN w.college c
LEFT JOIN w.major m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@RequiredArgsConstructor
@Slf4j
public class StudentCouncilPostForUserController {
private final StudentCouncilPostForUserService postService;

@PostMapping("/{postId}/like")
@Operation(summary = "학생회 게시글 좋아요 토글")
Expand All @@ -56,8 +57,6 @@ public CommonResponse<Page<GetLikedPostResponse>> getLikedPosts(
return CommonResponse.success(StudentCouncilPostResponseCode.POST_LIST_READ_SUCCESS, responses);
}

private final StudentCouncilPostForUserService postService;

@GetMapping("/school")
@Operation(
summary = "학교 학생회 게시글 목록 조회",
Expand All @@ -79,10 +78,11 @@ public CommonResponse<Page<PostListItemResponse>> getSchoolPosts(
@RequestParam(required = false) PostCategory category,
@RequestParam(defaultValue = "1") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(required = false) Long excludePostId,
@RequestParam(required = false) Long excludePostId,
@CurrentUserId Long userId
) {
Page<PostListItemResponse> responseDto = postService.findSchoolPosts(category, page, size, userId, excludePostId);
Page<PostListItemResponse> responseDto = postService.findSchoolPosts(category, page, size, userId,
excludePostId);
return CommonResponse.success(StudentCouncilPostResponseCode.POST_LIST_READ_SUCCESS, responseDto);
}

Expand Down Expand Up @@ -111,7 +111,8 @@ public CommonResponse<Page<PostListItemResponse>> getCollegePosts(
@RequestParam(required = false) Long excludePostId,
@CurrentUserId Long userId
) {
Page<PostListItemResponse> responseDto = postService.findCollegePosts(category, page, size, userId, excludePostId);
Page<PostListItemResponse> responseDto = postService.findCollegePosts(category, page, size, userId,
excludePostId);

return CommonResponse.success(StudentCouncilPostResponseCode.POST_LIST_READ_SUCCESS, responseDto);
}
Expand Down Expand Up @@ -141,7 +142,8 @@ public CommonResponse<Page<PostListItemResponse>> getMajorPosts(
@RequestParam(required = false) Long excludePostId,
@CurrentUserId Long userId
) {
Page<PostListItemResponse> responseDto = postService.findMajorPosts(category, page, size, userId, excludePostId);
Page<PostListItemResponse> responseDto = postService.findMajorPosts(category, page, size, userId,
excludePostId);

return CommonResponse.success(StudentCouncilPostResponseCode.POST_LIST_READ_SUCCESS, responseDto);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.campus.campus.domain.partnership.application.dto.response;
package com.campus.campus.domain.place.application.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

import com.campus.campus.domain.council.domain.entity.CouncilType;
import com.campus.campus.domain.councilpost.domain.entity.StudentCouncilPost;
import com.campus.campus.domain.partnership.application.dto.response.PartnershipPinResponse;
import com.campus.campus.domain.place.application.dto.response.PartnershipPinResponse;
import com.campus.campus.domain.place.application.dto.response.LikeResponse;
import com.campus.campus.domain.place.application.dto.response.SavedPlaceInfo;
import com.campus.campus.domain.place.application.dto.response.geocoder.AddressResponse;
import com.campus.campus.domain.place.application.dto.response.naver.NaverSearchResponse;
import com.campus.campus.domain.place.application.dto.response.partnership.PartnershipResponse;
import com.campus.campus.domain.place.domain.entity.Coordinate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.campus.campus.domain.partnership.application.service;
package com.campus.campus.domain.place.application.service;

import java.time.LocalDateTime;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -15,10 +18,11 @@
import com.campus.campus.domain.councilpost.application.exception.PlaceInfoNotFoundException;
import com.campus.campus.domain.councilpost.application.exception.PostNotFoundException;
import com.campus.campus.domain.councilpost.domain.entity.PostCategory;
import com.campus.campus.domain.councilpost.domain.entity.PostImage;
import com.campus.campus.domain.councilpost.domain.entity.StudentCouncilPost;
import com.campus.campus.domain.councilpost.domain.repository.PostImageRepository;
import com.campus.campus.domain.councilpost.domain.repository.StudentCouncilPostRepository;
import com.campus.campus.domain.partnership.application.dto.response.PartnershipPinResponse;
import com.campus.campus.domain.place.application.dto.response.PartnershipPinResponse;
import com.campus.campus.domain.place.application.dto.response.partnership.PartnershipResponse;
import com.campus.campus.domain.place.application.mapper.PlaceMapper;
import com.campus.campus.domain.place.domain.entity.Place;
Expand All @@ -34,15 +38,15 @@
@Slf4j
@RequiredArgsConstructor
@Service
public class PartnershipService {
public class PartnershipPlaceService {

private final UserRepository userRepository;
private final LikedPlacesRepository likedPlacesRepository;
private final PostImageRepository postImageRepository;
private final PlaceMapper placeMapper;
private final StudentCouncilPostRepository studentCouncilPostRepository;

@Transactional
@Transactional(readOnly = true)
public List<PartnershipResponse> getPartnershipPlaces(Long userId, Long cursor, int size, double userLat,
double userLng) {
User user = userRepository.findById(userId)
Expand All @@ -58,45 +62,67 @@ public List<PartnershipResponse> getPartnershipPlaces(Long userId, Long cursor,

//유저가 속한 학생회들의 제휴글(major/college/school) 전부 조회
List<StudentCouncilPost> posts = studentCouncilPostRepository.findByUserScopeWithCursor(
majorId,
collegeId,
schoolId,
majorId, collegeId, schoolId,
PostCategory.PARTNERSHIP,
CouncilType.MAJOR_COUNCIL,
CouncilType.COLLEGE_COUNCIL,
CouncilType.SCHOOL_COUNCIL,
cursor,
LocalDateTime.now(),
pageable
CouncilType.MAJOR_COUNCIL, CouncilType.COLLEGE_COUNCIL, CouncilType.SCHOOL_COUNCIL,
cursor, LocalDateTime.now(), pageable
);

return posts.stream()
.filter(post -> post.getPlace() != null && post.getPlace().getCoordinate() != null)
List<AbstractMap.SimpleEntry<StudentCouncilPost, Double>> sortedEntries = posts.stream()
.map(post -> {
Place place = post.getPlace();

double distanceMeter = GeoUtil.distanceMeter(
userLat, userLng,
place.getCoordinate().latitude(),
place.getCoordinate().longitude()
);

// post + distance를 함께 묶음
return new AbstractMap.SimpleEntry<>(post, distanceMeter);
})
.sorted(Map.Entry.comparingByValue()) // 거리순 정렬
.sorted(Map.Entry.comparingByValue())
.limit(size)
.toList();

if (sortedEntries.isEmpty()) {
return List.of();
}

List<StudentCouncilPost> targetPosts = sortedEntries.stream()
.map(AbstractMap.SimpleEntry::getKey)
.toList();

Set<Long> placeIds = targetPosts.stream()
.map(post -> post.getPlace().getPlaceId())
.collect(Collectors.toSet());

Map<Long, List<String>> postImageMap = postImageRepository.findAllByPostIn(targetPosts)
.stream()
.collect(Collectors.groupingBy(
img -> img.getPost().getId(),
Collectors.mapping(PostImage::getImageUrl, Collectors.toList())
));

Set<Long> likedPlaceIds;
if (placeIds.isEmpty()) {
likedPlaceIds = Collections.emptySet();
} else {
likedPlaceIds = likedPlacesRepository.findLikedPlaceIds(userId, placeIds);
}

return sortedEntries.stream()
.map(entry -> {
StudentCouncilPost post = entry.getKey();
double distanceMeter = entry.getValue();
double rounded = Math.round(distanceMeter * 100.0) / 100.0;

List<String> images = postImageMap.getOrDefault(post.getId(), List.of());
boolean isLiked = likedPlaceIds.contains(post.getPlace().getPlaceId());

return placeMapper.toPartnershipResponse(
user,
post,
post.getPlace(),
isLiked(post.getPlace(), user),
getImgUrls(post),
isLiked,
images,
rounded
);
})
Expand Down Expand Up @@ -144,9 +170,7 @@ public PartnershipResponse getPartnershipDetail(Long postId, Long userId, double
.orElseThrow(UserNotFoundException::new);

double distanceMeter = GeoUtil.distanceMeter(
userLat, userLng,
place.getCoordinate().latitude(),
place.getCoordinate().longitude()
userLat, userLng, place.getCoordinate().latitude(), place.getCoordinate().longitude()
);
double rounded = Math.round(distanceMeter * 100.0) / 100.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ public Place findOrCreatePlace(PostRequest request) {
SavedPlaceInfo place = request.place();
String placeKey = place.placeKey();

//이미 Place 존재하는지 확인 후 없으면 객체 생성 후 저장
return placeRepository.findByPlaceKey(placeKey)
.orElseGet(() -> placeRepository.save(placeMapper.createPlace(place)));
.orElseGet(() -> {
Place newPlace = placeRepository.save(placeMapper.createPlace(place));

if (place.imgUrls() != null && !place.imgUrls().isEmpty()) {
migrateImagesToOci(newPlace.getPlaceKey(), place.imgUrls());
}

return newPlace;
});
}

//장소 저장
Expand Down
Loading