|
|
@ -7,6 +7,7 @@ use diesel::{delete, insert_into};
|
|
|
|
use std::any;
|
|
|
|
use std::any;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::fmt::Debug;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
use tokio_diesel::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Database {
|
|
|
|
pub struct Database {
|
|
|
@ -22,22 +23,22 @@ impl Database {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns a guild setting from the database
|
|
|
|
/// Returns a guild setting from the database
|
|
|
|
pub fn get_guild_setting<T: 'static>(
|
|
|
|
pub async fn get_guild_setting<T: 'static>(
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
guild_id: u64,
|
|
|
|
guild_id: u64,
|
|
|
|
key: &str,
|
|
|
|
key: String,
|
|
|
|
) -> DatabaseResult<Option<T>>
|
|
|
|
) -> DatabaseResult<Option<T>>
|
|
|
|
where
|
|
|
|
where
|
|
|
|
T: FromStr,
|
|
|
|
T: FromStr,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
use guild_settings::dsl;
|
|
|
|
use guild_settings::dsl;
|
|
|
|
log::debug!("Retrieving setting '{}' for guild {}", key, guild_id);
|
|
|
|
log::debug!("Retrieving setting '{}' for guild {}", key, guild_id);
|
|
|
|
let connection = self.pool.get()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let entries: Vec<GuildSetting> = dsl::guild_settings
|
|
|
|
let entries: Vec<GuildSetting> = dsl::guild_settings
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::key.eq(key))
|
|
|
|
.filter(dsl::key.eq(key))
|
|
|
|
.load::<GuildSetting>(&connection)?;
|
|
|
|
.load_async::<GuildSetting>(&self.pool)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
log::trace!("Result is {:?}", entries);
|
|
|
|
log::trace!("Result is {:?}", entries);
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(first) = entries.first() {
|
|
|
|
if let Some(first) = entries.first() {
|
|
|
@ -57,16 +58,20 @@ impl Database {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Upserting a guild setting
|
|
|
|
/// Upserting a guild setting
|
|
|
|
pub fn set_guild_setting<T>(&self, guild_id: u64, key: &str, value: T) -> DatabaseResult<()>
|
|
|
|
pub async fn set_guild_setting<T>(
|
|
|
|
|
|
|
|
&self,
|
|
|
|
|
|
|
|
guild_id: u64,
|
|
|
|
|
|
|
|
key: String,
|
|
|
|
|
|
|
|
value: T,
|
|
|
|
|
|
|
|
) -> DatabaseResult<()>
|
|
|
|
where
|
|
|
|
where
|
|
|
|
T: ToString + Debug,
|
|
|
|
T: ToString + Debug,
|
|
|
|
{
|
|
|
|
{
|
|
|
|
use guild_settings::dsl;
|
|
|
|
use guild_settings::dsl;
|
|
|
|
log::debug!("Setting '{}' to '{:?}' for guild {}", key, value, guild_id);
|
|
|
|
log::debug!("Setting '{}' to '{:?}' for guild {}", key, value, guild_id);
|
|
|
|
let connection = self.pool.get()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
insert_into(dsl::guild_settings)
|
|
|
|
insert_into(dsl::guild_settings)
|
|
|
|
.values(&GuildSettingInsert {
|
|
|
|
.values(GuildSettingInsert {
|
|
|
|
guild_id: guild_id as i64,
|
|
|
|
guild_id: guild_id as i64,
|
|
|
|
key: key.to_string(),
|
|
|
|
key: key.to_string(),
|
|
|
|
value: value.to_string(),
|
|
|
|
value: value.to_string(),
|
|
|
@ -74,69 +79,76 @@ impl Database {
|
|
|
|
.on_conflict((dsl::guild_id, dsl::key))
|
|
|
|
.on_conflict((dsl::guild_id, dsl::key))
|
|
|
|
.do_update()
|
|
|
|
.do_update()
|
|
|
|
.set(dsl::value.eq(value.to_string()))
|
|
|
|
.set(dsl::value.eq(value.to_string()))
|
|
|
|
.execute(&connection)?;
|
|
|
|
.execute_async(&self.pool)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Deletes a guild setting
|
|
|
|
/// Deletes a guild setting
|
|
|
|
pub fn delete_guild_setting(&self, guild_id: u64, key: &str) -> DatabaseResult<()> {
|
|
|
|
pub async fn delete_guild_setting(&self, guild_id: u64, key: String) -> DatabaseResult<()> {
|
|
|
|
use guild_settings::dsl;
|
|
|
|
use guild_settings::dsl;
|
|
|
|
log::debug!("Deleting '{}' for guild {}", key, guild_id);
|
|
|
|
|
|
|
|
let connection = self.pool.get()?;
|
|
|
|
|
|
|
|
delete(dsl::guild_settings)
|
|
|
|
delete(dsl::guild_settings)
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::key.eq(key))
|
|
|
|
.filter(dsl::key.eq(key))
|
|
|
|
.execute(&connection)?;
|
|
|
|
.execute_async(&self.pool)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns a list of all guild playlists
|
|
|
|
/// Returns a list of all guild playlists
|
|
|
|
pub fn get_guild_playlists(&self, guild_id: u64) -> DatabaseResult<Vec<GuildPlaylist>> {
|
|
|
|
pub async fn get_guild_playlists(&self, guild_id: u64) -> DatabaseResult<Vec<GuildPlaylist>> {
|
|
|
|
use guild_playlists::dsl;
|
|
|
|
use guild_playlists::dsl;
|
|
|
|
log::debug!("Retrieving guild playlists for guild {}", guild_id);
|
|
|
|
log::debug!("Retrieving guild playlists for guild {}", guild_id);
|
|
|
|
let connection = self.pool.get()?;
|
|
|
|
|
|
|
|
let playlists: Vec<GuildPlaylist> = dsl::guild_playlists
|
|
|
|
let playlists: Vec<GuildPlaylist> = dsl::guild_playlists
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.load::<GuildPlaylist>(&connection)?;
|
|
|
|
.load_async::<GuildPlaylist>(&self.pool)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(playlists)
|
|
|
|
Ok(playlists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Returns a guild playlist by name
|
|
|
|
/// Returns a guild playlist by name
|
|
|
|
pub fn get_guild_playlist(
|
|
|
|
pub async fn get_guild_playlist(
|
|
|
|
&self,
|
|
|
|
&self,
|
|
|
|
guild_id: u64,
|
|
|
|
guild_id: u64,
|
|
|
|
name: &str,
|
|
|
|
name: String,
|
|
|
|
) -> DatabaseResult<Option<GuildPlaylist>> {
|
|
|
|
) -> DatabaseResult<Option<GuildPlaylist>> {
|
|
|
|
use guild_playlists::dsl;
|
|
|
|
use guild_playlists::dsl;
|
|
|
|
log::debug!("Retriving guild playlist '{}' for guild {}", name, guild_id);
|
|
|
|
log::debug!("Retriving guild playlist '{}' for guild {}", name, guild_id);
|
|
|
|
let connection = self.pool.get()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let playlists: Vec<GuildPlaylist> = dsl::guild_playlists
|
|
|
|
let playlists: Vec<GuildPlaylist> = dsl::guild_playlists
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::guild_id.eq(guild_id as i64))
|
|
|
|
.filter(dsl::name.eq(name))
|
|
|
|
.filter(dsl::name.eq(name))
|
|
|
|
.load::<GuildPlaylist>(&connection)?;
|
|
|
|
.load_async::<GuildPlaylist>(&self.pool)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(playlists.into_iter().next())
|
|
|
|
Ok(playlists.into_iter().next())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Adds a new playlist to the database overwriting the old one
|
|
|
|
/// Adds a new playlist to the database overwriting the old one
|
|
|
|
pub fn add_guild_playlist(&self, guild_id: u64, name: &str, url: &str) -> DatabaseResult<()> {
|
|
|
|
pub async fn add_guild_playlist(
|
|
|
|
|
|
|
|
&self,
|
|
|
|
|
|
|
|
guild_id: u64,
|
|
|
|
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
url: String,
|
|
|
|
|
|
|
|
) -> DatabaseResult<()> {
|
|
|
|
use guild_playlists::dsl;
|
|
|
|
use guild_playlists::dsl;
|
|
|
|
log::debug!("Inserting guild playlist '{}' for guild {}", name, guild_id);
|
|
|
|
log::debug!("Inserting guild playlist '{}' for guild {}", name, guild_id);
|
|
|
|
let connection = self.pool.get()?;
|
|
|
|
|
|
|
|
insert_into(dsl::guild_playlists)
|
|
|
|
insert_into(dsl::guild_playlists)
|
|
|
|
.values(GuildPlaylistInsert {
|
|
|
|
.values(GuildPlaylistInsert {
|
|
|
|
guild_id: guild_id as i64,
|
|
|
|
guild_id: guild_id as i64,
|
|
|
|
name: name.to_string(),
|
|
|
|
name: name.clone(),
|
|
|
|
url: url.to_string(),
|
|
|
|
url: url.clone(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.on_conflict((dsl::guild_id, dsl::name))
|
|
|
|
.on_conflict((dsl::guild_id, dsl::name))
|
|
|
|
.do_update()
|
|
|
|
.do_update()
|
|
|
|
.set(dsl::url.eq(url.to_string()))
|
|
|
|
.set(dsl::url.eq(url))
|
|
|
|
.execute(&connection)?;
|
|
|
|
.execute_async(&self.pool)
|
|
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|