-
Notifications
You must be signed in to change notification settings - Fork 1
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
✨ FEAT. ElevenLabs 음성 생성 API 기능
- Loading branch information
Showing
12 changed files
with
253 additions
and
12 deletions.
There are no files selected for viewing
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
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
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
20 changes: 20 additions & 0 deletions
20
src/main/java/fairytale/tbd/domain/voice/converter/VoiceConverter.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,20 @@ | ||
package fairytale.tbd.domain.voice.converter; | ||
|
||
import fairytale.tbd.domain.voice.entity.Voice; | ||
import fairytale.tbd.domain.voice.web.dto.VoiceResponseDTO; | ||
|
||
public class VoiceConverter { | ||
|
||
public static VoiceResponseDTO.AddVoiceResultDTO toAddVoiceResult(Voice voice){ | ||
return VoiceResponseDTO.AddVoiceResultDTO.builder() | ||
.voiceId(voice.getId()) | ||
.createdAt(voice.getCreatedAt()) | ||
.build(); | ||
} | ||
|
||
public static Voice toVoice(String keyId){ | ||
return Voice.builder() | ||
.keyId(keyId) | ||
.build(); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/fairytale/tbd/domain/voice/entity/Voice.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,45 @@ | ||
package fairytale.tbd.domain.voice.entity; | ||
|
||
import fairytale.tbd.domain.user.entity.User; | ||
import fairytale.tbd.global.entity.BaseEntity; | ||
import jakarta.persistence.CascadeType; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.OneToOne; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Voice extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "voice_id") | ||
private Long id; | ||
|
||
@Column(name = "voice_key_id", nullable = false) | ||
private String keyId; | ||
|
||
|
||
@OneToOne | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
|
||
// 연관 관계 편의 메소드 | ||
public void setUser(User user){ | ||
this.user = user; | ||
|
||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/fairytale/tbd/domain/voice/repository/VoiceRepository.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,12 @@ | ||
package fairytale.tbd.domain.voice.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import fairytale.tbd.domain.voice.entity.Voice; | ||
|
||
@Repository | ||
public interface VoiceRepository extends JpaRepository<Voice, Long> { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/fairytale/tbd/domain/voice/service/VoiceCommandService.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,8 @@ | ||
package fairytale.tbd.domain.voice.service; | ||
|
||
import fairytale.tbd.domain.voice.entity.Voice; | ||
import fairytale.tbd.domain.voice.web.dto.VoiceRequestDTO; | ||
|
||
public interface VoiceCommandService { | ||
Voice uploadVoice(VoiceRequestDTO.AddVoiceDTO request); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/fairytale/tbd/domain/voice/service/VoiceCommandServiceImpl.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,55 @@ | ||
package fairytale.tbd.domain.voice.service; | ||
|
||
import java.util.Optional; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import net.andrewcpu.elevenlabs.ElevenLabs; | ||
|
||
import fairytale.tbd.domain.user.entity.User; | ||
import fairytale.tbd.domain.user.repository.UserRepository; | ||
import fairytale.tbd.domain.voice.converter.VoiceConverter; | ||
import fairytale.tbd.domain.voice.entity.Voice; | ||
import fairytale.tbd.domain.voice.repository.VoiceRepository; | ||
import fairytale.tbd.domain.voice.web.dto.VoiceRequestDTO; | ||
import fairytale.tbd.global.elevenlabs.ElevenlabsManager; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class VoiceCommandServiceImpl implements VoiceCommandService{ | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(VoiceCommandServiceImpl.class); | ||
|
||
private final ElevenlabsManager elevenlabsManager; | ||
private final UserRepository userRepository; | ||
private final VoiceRepository voiceRepository; | ||
|
||
/** | ||
* ElevenLabs Voice 추가 | ||
* @param request MultiPartFile sample 사용자 녹음 파일 | ||
*/ | ||
|
||
@Transactional | ||
@Override | ||
public Voice uploadVoice(VoiceRequestDTO.AddVoiceDTO request) { | ||
|
||
Optional<User> userOptional = userRepository.findById(6L); | ||
User user = userOptional.get(); | ||
|
||
// TODO username session에서 가져오기 | ||
String keyId = elevenlabsManager.addVoice("test", request.getSample()); | ||
|
||
Voice voice = VoiceConverter.toVoice(keyId); | ||
user.setVoice(voice); | ||
voiceRepository.save(voice); | ||
|
||
return voice; | ||
} | ||
|
||
|
||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/fairytale/tbd/domain/voice/web/controller/VoiceRestController.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,37 @@ | ||
package fairytale.tbd.domain.voice.web.controller; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import fairytale.tbd.domain.voice.converter.VoiceConverter; | ||
import fairytale.tbd.domain.voice.entity.Voice; | ||
import fairytale.tbd.domain.voice.service.VoiceCommandService; | ||
import fairytale.tbd.domain.voice.web.dto.VoiceRequestDTO; | ||
import fairytale.tbd.domain.voice.web.dto.VoiceResponseDTO; | ||
import fairytale.tbd.global.response.ApiResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/voice") | ||
public class VoiceRestController { | ||
|
||
private static final Logger LOGGER = LogManager.getLogger(VoiceRestController.class); | ||
private final VoiceCommandService voiceCommandService; | ||
|
||
@PostMapping("") | ||
public ApiResponse<VoiceResponseDTO.AddVoiceResultDTO> addVoice(@Valid @ModelAttribute VoiceRequestDTO.AddVoiceDTO request){ | ||
// TODO 이미 존재하는 Voice 인지 검증 | ||
LOGGER.info("request = {}", request); | ||
Voice voice = voiceCommandService.uploadVoice(request); | ||
return ApiResponse.onSuccess(VoiceConverter.toAddVoiceResult(voice)); | ||
} | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/fairytale/tbd/domain/voice/web/dto/VoiceRequestDTO.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,20 @@ | ||
package fairytale.tbd.domain.voice.web.dto; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
public class VoiceRequestDTO { | ||
@Getter | ||
@Setter | ||
public static class AddVoiceDTO{ | ||
//파일 형식 검증 | ||
@NotNull | ||
MultipartFile sample; | ||
|
||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/fairytale/tbd/domain/voice/web/dto/VoiceResponseDTO.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,19 @@ | ||
package fairytale.tbd.domain.voice.web.dto; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class VoiceResponseDTO { | ||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class AddVoiceResultDTO{ | ||
private Long voiceId; | ||
private LocalDateTime createdAt; | ||
} | ||
} |
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