Skip to content

Commit

Permalink
Merge pull request #16 from kakao-tech-campus-2nd-step3/feat/#5-video…
Browse files Browse the repository at this point in the history
…Alias

[Feat] #5 videoAlias 정보를 랜덤하게 매핑해주는 기능을 구현했어요!
  • Loading branch information
sanghee0820 authored Sep 27, 2024
2 parents 504bf00 + c3dcb5f commit 0ef6809
Show file tree
Hide file tree
Showing 22 changed files with 457 additions and 166 deletions.
26 changes: 26 additions & 0 deletions src/main/java/team7/inplace/influencer/entity/Influencer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package team7.inplace.influencer.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@RequiredArgsConstructor // 테스팅을 위한 부분 추가, 협의 하에 다른 방식 채택 가능
public class Influencer {
/*
* 더미 데이터 입니다 !!!
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
@NonNull
private String name;
@Column
@NonNull
private String job;
@Column
@NonNull
private String imgUrl;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package team7.inplace.video.repository;
package team7.inplace.influencer.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import team7.inplace.video.entity.Influencer;
import team7.inplace.influencer.entity.Influencer;

import java.util.List;

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/team7/inplace/place/dto/PlaceForVideo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package team7.inplace.place.dto;

public record PlaceForVideo(
Long placeId,
String placeName
) {
public static PlaceForVideo of(Long placeId, String placeName) {
return new PlaceForVideo(
placeId,
placeName
);
}
}
4 changes: 0 additions & 4 deletions src/main/java/team7/inplace/video/DTO/PlaceForVideo.java

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/java/team7/inplace/video/DTO/VideoData.java

This file was deleted.

7 changes: 0 additions & 7 deletions src/main/java/team7/inplace/video/DTO/VideoResponse.java

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/java/team7/inplace/video/application/AliasUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package team7.inplace.video.application;

import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import team7.inplace.place.domain.Category;

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class AliasUtil {
private final Template template;
public static String makeAlias(String influencerName, Category category){
String alias = mapTemplateToCategory(category);
return influencerName + " " + alias;
}

// 카테고리에 따라 템플릿을 매핑하는 메서드
private static String mapTemplateToCategory(Category category) {
return switch (category) {
case CAFE -> Template.CAFE.getRandomTemplate();
case WESTERN -> Template.WESTERN.getRandomTemplate();
case JAPANESE -> Template.JAPANESE.getRandomTemplate();
case KOREAN -> Template.KOREAN.getRandomTemplate();
};
}
}
52 changes: 52 additions & 0 deletions src/main/java/team7/inplace/video/application/Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package team7.inplace.video.application;

import lombok.Getter;
import team7.inplace.place.domain.Category;

import java.util.*;

@Getter
public enum Template {
// 카페 관련 템플릿
CAFE(new String[]{
"이(가) 방문한 카페에서 따뜻한 커피를 즐겨보세요!",
"이(가) 방문했던 카페에서 여유로운 시간을 보내보세요!",
"이(가) 추천하는 사진 명소 예쁜 카페에 방문해보세요!"
}),

// 양식 관련 템플릿
WESTERN(new String[]{
"이(가) 추천! 맛있는 서양 음식을 즐길 수 있는 곳!",
"이(가) 추천하는 고급스러운 서양 요리를 맛볼 수 있습니다.",
"이(가) 극찬! 서양식 디저트를 꼭 한 번 시도해보세요!"
}),

// 일식 관련 템플릿
JAPANESE(new String[]{
"피셜, 현지와 다름없는 정통 일본 요리 레스토랑.",
"이(가) 추천하는 맛있는 일본 초밥과 라멘을 즐겨보세요.",
"이(가) 극찬! 일본 요리의 정수를 느껴보세요."
}),

// 한식 관련 템플릿
KOREAN(new String[]{
"이(가) 전통 한식을 맛볼 수 있는 한식당입니다.",
"이(가) 추천하는 가게에서 정성스럽게 준비된 한식으로 든든한 한 끼를!",
"이(가) 극찬! 한식의 깊은 맛을 느껴보세요."
});

// 템플릿 배열을 반환하는 메서드
// 필드 선언
private final String[] templates;

// 생성자
Template(String[] templates) {
this.templates = templates;
}

// 특정 템플릿을 랜덤으로 반환하는 메서드
public String getRandomTemplate() {
int randomIndex = (int) (Math.random() * templates.length);
return templates[randomIndex];
}
}
54 changes: 54 additions & 0 deletions src/main/java/team7/inplace/video/application/VideoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package team7.inplace.video.application;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import team7.inplace.influencer.entity.Influencer;
import team7.inplace.influencer.repository.InfluencerRepository;
import team7.inplace.place.domain.Place;
import team7.inplace.place.dto.PlaceForVideo;
import team7.inplace.video.application.dto.VideoInfo;
import team7.inplace.video.domain.Video;
import team7.inplace.video.persistence.VideoRepository;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class VideoService {
private final VideoRepository videoRepository;
private final InfluencerRepository influencerRepository;

public List<VideoInfo> findByInfluencer(List<String> influencers) {
// 인플루언서 정보 처리
List<Long> influencerIds = influencerRepository.findByNameIn(influencers).stream()
.map(Influencer::getId)
.toList();

// 인플루언서 정보로 필터링한 비디오 정보 불러오기
List<Video> savedVideos = videoRepository.findVideosByInfluencerIdIn(influencerIds);

// DTO 형식에 맞게 대입
return videoToInfo(savedVideos);
}

private List<VideoInfo> videoToInfo(List<Video> savedVideos) {
List<VideoInfo> videoInfos = new ArrayList<>();
for (Video savedVideo : savedVideos) {
Place place = savedVideo.getPlace();
String alias = AliasUtil.makeAlias(
savedVideo.getInfluencer().getName(),
place.getCategory()
);
videoInfos.add(
new VideoInfo(
savedVideo.getId(),
alias,
savedVideo.getVideoUrl(),
PlaceForVideo.of(place.getPlaceId(), place.getName())
)
);
}
return videoInfos;
}
}
12 changes: 12 additions & 0 deletions src/main/java/team7/inplace/video/application/dto/VideoInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package team7.inplace.video.application.dto;

import team7.inplace.place.dto.PlaceForVideo;

// Video 도메인의 Controller와 Service 사이의 정보 전달을 담당하는 클래스 ( Service Return )
public record VideoInfo(
Long videoId,
String videoAlias,
String videoUrl,
PlaceForVideo place
) {
}
32 changes: 0 additions & 32 deletions src/main/java/team7/inplace/video/controller/VideoController.java

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/java/team7/inplace/video/domain/Video.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package team7.inplace.video.domain;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import team7.inplace.influencer.entity.Influencer;
import team7.inplace.place.domain.Place;

@Entity
@Getter
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
@RequiredArgsConstructor // 테스팅을 위한 부분 추가, 협의 하에 다른 방식 채택 가능
public class Video {
@Id
@GeneratedValue(strategy = GenerationType.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
private Place place;
}
26 changes: 0 additions & 26 deletions src/main/java/team7/inplace/video/entity/Influencer.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/team7/inplace/video/entity/Place.java

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/team7/inplace/video/entity/Video.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package team7.inplace.video.repository;
package team7.inplace.video.persistence;

import org.springframework.data.jpa.repository.JpaRepository;
import team7.inplace.video.entity.Video;
import team7.inplace.video.domain.Video;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package team7.inplace.video.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import team7.inplace.video.application.dto.VideoInfo;
import team7.inplace.video.presentation.dto.VideoResponse;
import team7.inplace.video.application.VideoService;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class VideoController {
private final VideoService videoService;

// 내 인플루언서가 방문한 그 곳 ( 토큰 0 )
@GetMapping("/video")
public ResponseEntity<List<VideoResponse>> readByInfluencer(
@RequestParam(name = "influencer", required = false) List<String> influencers
) {
List<VideoInfo> videoInfos = videoService.findByInfluencer(influencers);
List<VideoResponse> videoResponses = videoInfos.stream().map(VideoResponse::new).toList();
return new ResponseEntity<>(videoResponses, HttpStatus.OK);
}
}
Loading

0 comments on commit 0ef6809

Please sign in to comment.