diff --git a/src/controllers/samples/controller.rs b/src/controllers/samples/controller.rs index 46d2b80..0614037 100644 --- a/src/controllers/samples/controller.rs +++ b/src/controllers/samples/controller.rs @@ -20,7 +20,7 @@ pub async fn request_put_url(req: HttpRequest, body: web::Json 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" }))); } diff --git a/src/controllers/voices/controller.rs b/src/controllers/voices/controller.rs index 61e3c5d..6a629b4 100644 --- a/src/controllers/voices/controller.rs +++ b/src/controllers/voices/controller.rs @@ -24,7 +24,7 @@ pub async fn get_voice_by_id(req: HttpRequest, voice_id: web::Path) -> A pub async fn delete_voice(req: HttpRequest, voice_id: web::Path) -> 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" })) ); diff --git a/src/models/voice.rs b/src/models/voice.rs index 48a867a..52ecd24 100644 --- a/src/models/voice.rs +++ b/src/models/voice.rs @@ -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, @@ -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(), } } } @@ -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, pub eleven_labs_id: Option, pub created_at: DateTime, @@ -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(), @@ -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 { + Ok(Self::count(Some(doc! { "status": VoiceStatus::Active.to_string() })).await?) + } }