You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.5 KiB
Rust
58 lines
1.5 KiB
Rust
4 years ago
|
use diesel::insert_into;
|
||
|
use diesel::prelude::*;
|
||
|
use tokio_diesel::*;
|
||
|
|
||
|
use crate::error::DatabaseResult;
|
||
|
use crate::models::*;
|
||
|
use crate::schema::*;
|
||
|
use crate::Database;
|
||
|
|
||
|
impl Database {
|
||
|
/// Returns a list of all gifs in the database
|
||
3 years ago
|
pub async fn get_all_media(&self) -> DatabaseResult<Vec<Media>> {
|
||
|
use media::dsl;
|
||
4 years ago
|
log::debug!("Loading all gifs from the database");
|
||
|
|
||
3 years ago
|
let gifs: Vec<Media> = dsl::media.load_async::<Media>(&self.pool).await?;
|
||
4 years ago
|
Ok(gifs)
|
||
|
}
|
||
|
|
||
|
/// Returns a list of gifs by assigned category
|
||
3 years ago
|
pub async fn get_media_by_category(&self, category: &str) -> DatabaseResult<Vec<Media>> {
|
||
|
use media::dsl;
|
||
4 years ago
|
log::debug!("Searching for gifs in category '{}'", category);
|
||
|
|
||
3 years ago
|
let gifs: Vec<Media> = dsl::media
|
||
4 years ago
|
.filter(dsl::category.eq(category))
|
||
3 years ago
|
.load_async::<Media>(&self.pool)
|
||
4 years ago
|
.await?;
|
||
|
Ok(gifs)
|
||
|
}
|
||
|
|
||
|
/// Adds a gif to the database
|
||
3 years ago
|
pub async fn add_media(
|
||
4 years ago
|
&self,
|
||
|
url: &str,
|
||
|
category: Option<String>,
|
||
|
name: Option<String>,
|
||
|
) -> DatabaseResult<()> {
|
||
3 years ago
|
use media::dsl;
|
||
4 years ago
|
log::debug!(
|
||
|
"Inserting gif with url '{}' and name {:?} and category {:?}",
|
||
|
url,
|
||
|
name,
|
||
|
category
|
||
|
);
|
||
3 years ago
|
insert_into(dsl::media)
|
||
|
.values(MediaInsert {
|
||
4 years ago
|
url: url.to_string(),
|
||
|
name,
|
||
|
category,
|
||
|
})
|
||
|
.execute_async(&self.pool)
|
||
|
.await?;
|
||
|
|
||
|
Ok(())
|
||
|
}
|
||
|
}
|