-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Feature] 상품 음성파일 구현
- Loading branch information
Showing
6 changed files
with
241 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/com/gdsc/hearo/domain/item/controller/ItemTtsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; // 예외처리 이상함.. | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/gdsc/hearo/domain/item/dto/ItemTtsFileResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/gdsc/hearo/domain/item/dto/ItemTtsRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/gdsc/hearo/domain/item/dto/ItemTtsResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/gdsc/hearo/domain/item/repository/ItemTtsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
113
src/main/java/com/gdsc/hearo/domain/item/service/ItemTtsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
|
||
|
||
} |