Skip to content

Commit

Permalink
Merge pull request #33 from GoogleEyes-ewha/feature/#29
Browse files Browse the repository at this point in the history
[Feature] 상품 음성파일 구현
  • Loading branch information
Haewonny authored Feb 11, 2024
2 parents eb663ca + b17f3e0 commit 216050b
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.gdsc.hearo.domain.item.controller;

import com.gdsc.hearo.domain.item.dto.ItemTtsFileResponseDto;
import com.gdsc.hearo.domain.item.dto.ItemTtsRequestDto;
import com.gdsc.hearo.domain.item.dto.ItemTtsResponseDto;
import com.gdsc.hearo.domain.item.service.ItemTtsService;
import com.gdsc.hearo.domain.review.dto.ReviewTTSDto;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.BaseResponse;
import com.gdsc.hearo.global.common.BaseResponseStatus;
import com.gdsc.hearo.global.common.VoiceType;
import com.gdsc.hearo.global.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("item")
public class ItemTtsController {

private final ItemTtsService itemTtsService;

@Autowired
public ItemTtsController(ItemTtsService itemTtsService){
this.itemTtsService = itemTtsService;
}

//[post]상품 음성 파일 저장
@PostMapping("/tts/{itemId}")
public BaseResponse<?> postItemTTSFile(@AuthenticationPrincipal CustomUserDetails user, @PathVariable(name="itemId") Long itemId, @RequestBody ItemTtsRequestDto request) {
try {
if (user != null) { // 로그인 한 경우
// 서비스를 호출하여 음성 파일 저장
ItemTtsResponseDto result = itemTtsService.saveItemTTS(user, itemId, request);

return new BaseResponse<>(BaseResponseStatus.SUCCESS, result);

} else { // 로그인하지 않은 경우
ItemTtsResponseDto result = itemTtsService.saveItemTTS(null, itemId, request);

return new BaseResponse<>(BaseResponseStatus.SUCCESS, "음성 파일이 저장되었습니다.");
}
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

// 상품 음성 파일 조회
@GetMapping("/tts/{itemId}")
public BaseResponse<ItemTtsFileResponseDto> getItemTts(@AuthenticationPrincipal CustomUserDetails user,
@PathVariable(name = "itemId") Long itemId) {

try{
if (user != null) { // 로그인 한 경우
ItemTtsFileResponseDto result = itemTtsService.getItemTts(user, itemId);
return new BaseResponse<>(BaseResponseStatus.SUCCESS, result);
}
else{
ItemTtsFileResponseDto result = itemTtsService.getItemTts(null,itemId);
return new BaseResponse<>(BaseResponseStatus.SUCCESS, result);
}
}catch(Exception e){
e.printStackTrace();
return null; // 예외처리 이상함..
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.gdsc.hearo.domain.item.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ItemTtsFileResponseDto {

private String itemUrl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gdsc.hearo.domain.item.dto;

import com.gdsc.hearo.global.common.VoiceType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ItemTtsRequestDto {

private String itemUrl;
private VoiceType voiceType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gdsc.hearo.domain.item.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ItemTtsResponseDto {

private String result;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gdsc.hearo.domain.item.repository;

import com.gdsc.hearo.domain.item.entity.ItemTts;
import com.gdsc.hearo.global.common.VoiceType;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ItemTtsRepository extends JpaRepository<ItemTts, Long> {

ItemTts findTopByItemItemIdAndVoiceTypeOrderByCreatedAtDesc(
Long itemId, VoiceType voiceType);
}
113 changes: 113 additions & 0 deletions src/main/java/com/gdsc/hearo/domain/item/service/ItemTtsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.gdsc.hearo.domain.item.service;

import com.gdsc.hearo.domain.item.dto.ItemTtsFileResponseDto;
import com.gdsc.hearo.domain.item.dto.ItemTtsRequestDto;
import com.gdsc.hearo.domain.item.dto.ItemTtsResponseDto;
import com.gdsc.hearo.domain.item.entity.Item;
import com.gdsc.hearo.domain.item.entity.ItemTts;
import com.gdsc.hearo.domain.item.repository.ItemRepository;
import com.gdsc.hearo.domain.item.repository.ItemTtsRepository;
import com.gdsc.hearo.domain.member.entity.MemberSetting;
import com.gdsc.hearo.domain.member.repository.MemberSettingRepository;
import com.gdsc.hearo.domain.review.repository.ReviewTtsRepository;
import com.gdsc.hearo.global.common.BaseException;
import com.gdsc.hearo.global.common.VoiceType;
import com.gdsc.hearo.global.security.CustomUserDetails;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ItemTtsService {

private final ItemTtsRepository itemTtsRepository;
private final MemberSettingRepository memberSettingRepository;
private final ReviewTtsRepository reviewTtsRepository;
private final ItemRepository itemRepository;

//상품 음성파일 저장
public ItemTtsResponseDto saveItemTTS(@Nullable CustomUserDetails user, Long itemId, ItemTtsRequestDto itemTtsRequestDto) throws BaseException {
if (user != null) { // 로그인한 경우
Item item = itemRepository.findById(itemId).orElse(null);

Long settingId = user.getMember().getMemberSetting().getSettingId();
MemberSetting memberSetting = memberSettingRepository.findById(settingId).orElse(null);
VoiceType voiceType = memberSetting.getVoiceType();

ItemTts itemTts = ItemTts.builder()
.item(item)
.ttsFile(itemTtsRequestDto.getItemUrl())
.voiceType(voiceType)
.build();

ItemTts savedItemTts = itemTtsRepository.save(itemTts);

return ItemTtsResponseDto.builder()
.result("상품 음성파일이 저장되었습니다.")
.build();

} else { // 로그인하지 않은 경우
Item item = itemRepository.findById(itemId).orElse(null);

ItemTts itemTts = ItemTts.builder()
.item(item)
.ttsFile(itemTtsRequestDto.getItemUrl())
.voiceType(VoiceType.MALE_VOICE)
.build();

ItemTts savedItemTts = itemTtsRepository.save(itemTts);

return ItemTtsResponseDto.builder()
.result("상품 음성파일이 저장되었습니다.")
.build();

}
}

public ItemTtsFileResponseDto getItemTts(@Nullable CustomUserDetails user,Long itemId) {

if (user != null) { // 로그인한 경우

Long settingId = user.getMember().getMemberSetting().getSettingId();
MemberSetting memberSetting = memberSettingRepository.findById(settingId).orElse(null);
VoiceType voiceType= memberSetting.getVoiceType();

ItemTts itemTts = itemTtsRepository.findTopByItemItemIdAndVoiceTypeOrderByCreatedAtDesc(
itemId,voiceType);

if(itemTts != null){
return ItemTtsFileResponseDto.builder()
.itemUrl(itemTts.getTtsFile())
.build();
}
else{
return ItemTtsFileResponseDto.builder()
.itemUrl("")
.build();
}

} else {
VoiceType voiceType= VoiceType.MALE_VOICE;

ItemTts itemTts = itemTtsRepository.findTopByItemItemIdAndVoiceTypeOrderByCreatedAtDesc(
itemId,voiceType);

if(itemTts != null){
return ItemTtsFileResponseDto.builder()
.itemUrl(itemTts.getTtsFile())
.build();
}
else{
return ItemTtsFileResponseDto.builder()
.itemUrl("")
.build();
}
}
}


}

0 comments on commit 216050b

Please sign in to comment.