Skip to content

Commit

Permalink
Voice Status Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
judegiordano committed Nov 21, 2023
1 parent 8e3c2f0 commit 27b1fa5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/controllers/samples/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub async fn request_put_url(req: HttpRequest, body: web::Json<UploadSampleBody>
let config = env::Config::new()?;
let s3 = Client::new(&config.samples_bucket_name).await;
let name = slug::slugify(body.voice_name.to_string());
let count = Voice::count(None).await?;
let count = Voice::active_voices_count().await?;
if count >= 10 {
return Ok(HttpResponse::BadRequest().json(json!({ "error": "10 voice limit reached" })));
}
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/voices/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn get_voice_by_id(req: HttpRequest, voice_id: web::Path<String>) -> A
pub async fn delete_voice(req: HttpRequest, voice_id: web::Path<String>) -> ApiResponse {
authenticate(req).await?;
let voice = Voice::read_by_id(&voice_id).await?;
if voice.status.to_string() != VoiceStatus::Active.to_string() {
if voice.status != VoiceStatus::Active {
return Ok(
HttpResponse::InternalServerError().json(json!({ "error": "voice is not active" }))
);
Expand Down
17 changes: 11 additions & 6 deletions src/models/voice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use mongoose::{
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, Clone)]
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
pub enum VoiceStatus {
Active,
Draft,
Expand All @@ -16,9 +16,9 @@ pub enum VoiceStatus {
impl VoiceStatus {
pub fn to_string(&self) -> String {
match self {
VoiceStatus::Active => "active".to_string(),
VoiceStatus::Draft => "draft".to_string(),
VoiceStatus::Deleted => "deleted".to_string(),
VoiceStatus::Active => "Active".to_string(),
VoiceStatus::Draft => "Draft".to_string(),
VoiceStatus::Deleted => "Deleted".to_string(),
}
}
}
Expand All @@ -28,7 +28,7 @@ pub struct Voice {
#[serde(rename = "_id")]
pub id: String,
pub name: String,
pub status: String,
pub status: VoiceStatus,
pub description: Option<String>,
pub eleven_labs_id: Option<String>,
pub created_at: DateTime,
Expand All @@ -40,7 +40,7 @@ impl Default for Voice {
Self {
id: Self::generate_nanoid(),
name: std::string::String::default(),
status: VoiceStatus::Draft.to_string(),
status: VoiceStatus::Draft,
description: None,
eleven_labs_id: None,
created_at: DateTime::now(),
Expand All @@ -61,7 +61,12 @@ impl Voice {
IndexModel::builder()
.keys(doc! { "eleven_labs_id": 1 })
.build(),
IndexModel::builder().keys(doc! { "status": 1 }).build(),
])
.await
}

pub async fn active_voices_count() -> anyhow::Result<u64> {
Ok(Self::count(Some(doc! { "status": VoiceStatus::Active.to_string() })).await?)
}
}

0 comments on commit 27b1fa5

Please sign in to comment.