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

[feat] 동화 도메인 관련 api구현 #7

Merged
merged 3 commits into from
Feb 17, 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
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ dependencies {
//oauth2
implementation 'org.springframework.boot:spring-boot-starter-oauth2-client'

// Youtube API
implementation 'com.google.api-client:google-api-client:1.33.0'
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.23.0'
implementation 'com.google.apis:google-api-services-youtube:v3-rev20230816-2.0.0'
implementation 'com.google.http-client:google-http-client-jackson2:1.39.2'

//redis
//implementation 'org.springframework.boot:spring-boot-starter-data-redis'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.example.comma.domain.fairytale.controller;

import com.example.comma.domain.fairytale.dto.FairytaleDetailResponseDto;
import com.example.comma.domain.fairytale.dto.FairytaleResponseDto;
import com.example.comma.domain.fairytale.service.FairytaleService;
import com.example.comma.global.common.SuccessResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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 java.util.List;

@RequiredArgsConstructor
@RequestMapping("/api/fairytale")
@RestController
public class FairytaleController {
private final FairytaleService fairytaleService;

@GetMapping
public ResponseEntity<SuccessResponse<?>> getFairytale() {
List<FairytaleResponseDto> fairytale = fairytaleService.getFairytale();
return SuccessResponse.ok(fairytale);
}

@GetMapping("/detail/{fairytaleId}")
public ResponseEntity<SuccessResponse<?>> getFairytaleDetail(@PathVariable(name = "fairytaleId") Long fairytaleId) {
FairytaleDetailResponseDto fairytale = fairytaleService.getFairytaleDetail(fairytaleId);
return SuccessResponse.ok(fairytale);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.comma.domain.fairytale.dto;

public record FairytaleDetailResponseDto(
String description,
String recommendImageUrl
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.comma.domain.fairytale.dto;

public record FairytaleResponseDto(
Long id,
String title,
String imgaeUrl,
String channelName,
String year,
String time,
String link,
Boolean subtitleTag,
Boolean signTag
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class Fairytale {

private String imgaeUrl;

private String recommendImageUrl;

private String channelName;

private Year year;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.comma.domain.fairytale.repository;

import com.example.comma.domain.fairytale.entity.Fairytale;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface FairytaleRepository extends JpaRepository<Fairytale, Long> {
List<Fairytale> findByIdNot(Long fairytaleId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.example.comma.domain.fairytale.service;

import com.example.comma.domain.fairytale.dto.FairytaleDetailResponseDto;
import com.example.comma.domain.fairytale.dto.FairytaleResponseDto;
import com.example.comma.domain.fairytale.entity.Fairytale;
import com.example.comma.domain.fairytale.repository.FairytaleRepository;
import com.example.comma.global.error.exception.EntityNotFoundException;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.example.comma.global.error.ErrorCode.FAIRYTALE_NOT_FOUND;

@RequiredArgsConstructor
@Service
@Transactional
public class FairytaleService {

@Value("${youtube.api.key}")
private String apiKey;

private final FairytaleRepository fairytaleRepository;


public String searchVideo(String title, String channelName) throws IOException {
JsonFactory jsonFactory = new JacksonFactory();

YouTube youtube = new YouTube.Builder(
new com.google.api.client.http.javanet.NetHttpTransport(),
jsonFactory,
request -> {})
.build();

String query = title + " " + channelName;

YouTube.Search.List search = youtube.search().list(Collections.singletonList("id,snippet"));

search.setKey(apiKey);
search.setQ(query);

SearchListResponse searchResponse = search.execute();

List<SearchResult> searchResultList = searchResponse.getItems();

if (searchResultList != null && searchResultList.size() > 0) {

SearchResult searchResult = searchResultList.get(0);

String videoId = searchResult.getId().getVideoId();
String videoTitle = searchResult.getSnippet().getTitle();

return "https://www.youtube.com/watch?v=" + videoId;
}

return "검색 결과가 없습니다";
}

public List<FairytaleResponseDto> getFairytale() {
List<Fairytale> fairytaleList = fairytaleRepository.findAll();

return fairytaleList.stream()
.map(fairytale -> {
try {
String videolink = searchVideo(fairytale.getTitle(), fairytale.getChannelName());


return new FairytaleResponseDto(
fairytale.getId(),
fairytale.getTitle(),
fairytale.getImgaeUrl(),
fairytale.getChannelName(),
fairytale.getYear().toString(),
fairytale.getTime().toString(),
(videolink != null) ? videolink : fairytale.getLink(),
fairytale.getSubtitleTag(),
fairytale.getSignTag()
);
} catch (IOException e) {
e.printStackTrace();
return null;
}
})
.filter(dto -> dto != null)
.collect(Collectors.toList());
}

public FairytaleDetailResponseDto getFairytaleDetail(Long fairytaleId) {
Fairytale fairytale = fairytaleRepository.findById(fairytaleId)
.orElseThrow(() -> new EntityNotFoundException(FAIRYTALE_NOT_FOUND)
);

String description = fairytale.getDescription();

List<Fairytale> randomFairytaleList = fairytaleRepository.findByIdNot(fairytaleId);
Collections.shuffle(randomFairytaleList);
Fairytale recommendFairytale = randomFairytaleList.get(0);

return new FairytaleDetailResponseDto(description, recommendFairytale.getRecommendImageUrl());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public enum ErrorCode {
CARD_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 카드를 찾을 수 없습니다."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 유저를 찾을 수 없습니다."),
USER_CARD_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 유저가 등록한 카드를 찾을 수 없습니다."),
FAIRYTALE_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 동화를 찾을 수 없습니다."),
/**
* 405 Method Not Allowed
*/
Expand Down
Loading