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

[Refactor] #49 Integration 과정에서 발생하는 문제들을 해결해보아요 #53

Merged
merged 6 commits into from
Oct 11, 2024
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
@@ -0,0 +1,31 @@
package team7.inplace.global.exception.code;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;


@AllArgsConstructor
@Getter
public enum PlaceErrorCode implements ErrorCode{
NOT_FOUND(HttpStatus.NOT_FOUND, "P001", "Can't find such place info");

private final HttpStatus httpStatus;
private final String errorCode;
private final String message;

@Override
public HttpStatus httpStatus() {
return httpStatus;
}

@Override
public String code() {
return errorCode;
}

@Override
public String message() {
return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@AllArgsConstructor
@Getter
public enum VideoErrorCode implements ErrorCode{
NO_SUCH_VIDEO(HttpStatus.NOT_FOUND, "V001", "Can't find such video info");
NOT_FOUND(HttpStatus.NOT_FOUND, "V001", "Can't find such video info");

private final HttpStatus httpStatus;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void deleteInfluencer(Long id) {
influencerRepository.delete(influencer);
}

@Transactional
public void likeToInfluencer(InfluencerRequestParam param) {
String username = AuthorizationUtil.getUsername();
if (StringUtils.hasText(username)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Objects;
import org.springframework.data.domain.Pageable;
import team7.inplace.place.domain.Place;
import team7.inplace.video.presentation.dto.VideoSearchParams;

public class PlacesCommand {

Expand All @@ -19,6 +20,17 @@ public record PlacesCoordinateCommand(
String latitude,
Pageable pageable
) {
public static PlacesCoordinateCommand from(VideoSearchParams videoSearchParams, Pageable pageable) {
return new PlacesCoordinateCommand(
videoSearchParams.topLeftLongitude(),
videoSearchParams.topLeftLatitude(),
videoSearchParams.bottomRightLatitude(),
videoSearchParams.bottomRightLatitude(),
videoSearchParams.longitude(),
videoSearchParams.latitude(),
pageable
);
}
}

public record PlacesFilterParamsCommand(
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/team7/inplace/video/application/VideoFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,44 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import team7.inplace.global.annotation.Facade;
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.AuthorizationErrorCode;
import team7.inplace.place.application.PlaceService;
import team7.inplace.place.application.command.PlacesCommand;
import team7.inplace.security.util.AuthorizationUtil;
import team7.inplace.user.application.UserService;
import team7.inplace.video.application.command.VideoCommand;
import team7.inplace.video.application.dto.VideoInfo;

@Facade
@RequiredArgsConstructor
public class VideoFacade {
private final VideoService videoService;
private final PlaceService placeService;
private final UserService userService;

@Transactional
public void createVideos(List<VideoCommand.Create> videoCommands, List<PlacesCommand.Create> placeCommands) {

var placeIds = placeService.createPlaces(placeCommands);
videoService.createVideos(videoCommands, placeIds);
}

public Page<VideoInfo> getVideosByMyInfluencer(Pageable pageable){
// User 정보를 쿠키에서 추출
Long userId = AuthorizationUtil.getUserId();
// 토큰 정보에 대한 검증
if (ObjectUtils.isEmpty(userId)) {
throw InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY);
}
// 유저 정보를 이용하여 유저가 좋아요를 누른 인플루언서 id 리스트를 조회
List<Long> influencerIds = userService.getInfluencerIdsByUsername(userId);
// 인플루언서 id를 사용하여 영상을 조회
return videoService.getVideosByMyInfluencer(influencerIds, pageable);
}
}
38 changes: 17 additions & 21 deletions src/main/java/team7/inplace/video/application/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import team7.inplace.global.exception.InplaceException;
import team7.inplace.global.exception.code.AuthorizationErrorCode;
import team7.inplace.global.exception.code.PlaceErrorCode;
import team7.inplace.global.exception.code.VideoErrorCode;
import team7.inplace.influencer.persistence.InfluencerRepository;
import team7.inplace.place.application.dto.PlaceForVideo;
import team7.inplace.place.domain.Place;
import team7.inplace.place.persistence.PlaceRepository;
import team7.inplace.security.util.AuthorizationUtil;
import team7.inplace.user.application.UserService;
import team7.inplace.video.application.command.VideoCommand.Create;
import team7.inplace.video.application.dto.VideoInfo;
import team7.inplace.video.domain.Video;
Expand All @@ -28,22 +26,29 @@ public class VideoService {
private final VideoRepository videoRepository;
private final PlaceRepository placeRepository;
private final InfluencerRepository influencerRepository;
private final UserService userService;

public Page<VideoInfo> getVideosBySurround(VideoSearchParams videoSearchParams, Pageable pageable) {
Page<Place> places = placeRepository.getPlacesByDistance(
// Place 엔티티 조회
Page<Place> places = placeRepository.getPlacesByDistanceAndFilters(
videoSearchParams.topLeftLongitude(),
videoSearchParams.topLeftLatitude(),
videoSearchParams.bottomRightLongitude(),
videoSearchParams.bottomRightLatitude(),
videoSearchParams.longitude(),
videoSearchParams.latitude(),
new ArrayList<>(),
new ArrayList<>(),
pageable
);

// 조회된 엔티티가 비어있는지 아닌지 확인
if(places.isEmpty()){
throw InplaceException.of(PlaceErrorCode.NOT_FOUND);
}
// 장소를 기준으로 비디오 엔티티 조회 ( 장소 별로 가장 최근 비디오 하나 씩 )
List<Video> videos = new ArrayList<>();
for (Place place : places.getContent()) {
if (videos.size() == places.getSize()) {
break;
}
videos.add(videoRepository.findTopByPlaceOrderByIdDesc(place)
.orElseThrow(() -> InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY)));
.orElseThrow(() -> InplaceException.of(VideoErrorCode.NOT_FOUND)));
}
return new PageImpl<>(videos).map(this::videoToInfo);
}
Expand All @@ -56,16 +61,7 @@ public Page<VideoInfo> getAllVideosDesc(Pageable pageable) {
return videos.map(this::videoToInfo);
}

public Page<VideoInfo> getVideosByMyInfluencer(Pageable pageable) {
// User 정보를 쿠키에서 추출
Long userId = AuthorizationUtil.getUserId();
// 토큰 정보에 대한 검증
if (ObjectUtils.isEmpty(userId)) {
throw InplaceException.of(AuthorizationErrorCode.TOKEN_IS_EMPTY);
}
// 유저 정보를 이용하여 유저가 좋아요를 누른 인플루언서 id 리스트를 조회
List<Long> influencerIds = userService.getInfluencerIdsByUsername(userId);
// 인플루언서 id 리스트를 이용하여 해당 인플루언서의 비디오들을 조회
public Page<VideoInfo> getVideosByMyInfluencer(List<Long> influencerIds, Pageable pageable) {
Page<Video> videos = videoRepository.findVideosByInfluencerIdIn(influencerIds, pageable);
return videos.map(this::videoToInfo);
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/team7/inplace/video/domain/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,20 @@
@Entity
@Getter
@NoArgsConstructor(access = PROTECTED)
@RequiredArgsConstructor
public class Video {
@Id
@GeneratedValue(strategy = IDENTITY)
private Long id;

@Column(name = "video_url", nullable = false, columnDefinition = "TEXT")
@NonNull
private String videoUrl;

@ManyToOne
@JoinColumn(name = "influencer_id", nullable = false)
@NonNull
private Influencer influencer;

@ManyToOne
@JoinColumn(name = "place_id", nullable = false)
@NonNull
@JoinColumn(name = "place_id")
private Place place;

private Video(Influencer influencer, Place place, String videoUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import team7.inplace.video.application.VideoFacade;
import team7.inplace.video.application.VideoService;
import team7.inplace.video.presentation.dto.VideoResponse;
import team7.inplace.video.presentation.dto.VideoSearchParams;
Expand All @@ -20,6 +21,7 @@
@RequestMapping("/videos")
public class VideoController implements VideoControllerApiSpec {
private final VideoService videoService;
private final VideoFacade videoFacade;

@GetMapping()
public ResponseEntity<Page<VideoResponse>> readVideos(
Expand Down Expand Up @@ -54,7 +56,7 @@ public ResponseEntity<Page<VideoResponse>> readByCool(
public ResponseEntity<Page<VideoResponse>> readByInfluencer(
@PageableDefault(page = 0, size = 10) Pageable pageable
) {
Page<VideoResponse> videoResponses = videoService.getVideosByMyInfluencer(pageable)
Page<VideoResponse> videoResponses = videoFacade.getVideosByMyInfluencer(pageable)
.map(VideoResponse::from);
return new ResponseEntity<>(videoResponses, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public static VideoResponse from(VideoInfo videoInfo) {
return new VideoResponse(
videoInfo.videoId(),
videoInfo.videoAlias(),
videoInfo.videoUrl(),
"https://www.youtube.com/watch?v=" + videoInfo.videoUrl(),
videoInfo.place()
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package team7.inplace.video.presentation.dto;

public record VideoSearchParams(
String topLeftLongitude,
String topLeftLatitude,
String bottomRightLongitude,
String bottomRightLatitude,
String longitude,
String latitude
) {
Expand Down