diff --git a/src/botv2/domain/concour/mod.rs b/src/botv2/domain/concour/mod.rs index d7fbbe9..4e0c63a 100644 --- a/src/botv2/domain/concour/mod.rs +++ b/src/botv2/domain/concour/mod.rs @@ -2,3 +2,4 @@ pub mod check_if_allowed; pub mod create_concour; pub mod get_channel_concour; pub mod list_concour; +pub mod update_concour; diff --git a/src/botv2/domain/concour/update_concour.rs b/src/botv2/domain/concour/update_concour.rs new file mode 100644 index 0000000..1c08157 --- /dev/null +++ b/src/botv2/domain/concour/update_concour.rs @@ -0,0 +1,61 @@ +use serde::{Deserialize, Serialize}; +use tracing::{info, instrument}; + +use crate::db::concour::Concour; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum UpdateConcourError { + DoesntExist, + FindError(String), + UnknownError(String), +} + +#[instrument(level = "info")] +pub async fn update_concour( + server_id: u64, + channel_id: u64, + title: Option, + description: Option, + role_recopense: Option, + banner: Option, +) -> Result, UpdateConcourError> { + let concour = match Concour::find_by_server_id_channel_id(&server_id, &channel_id).await { + Ok(list_concour) => list_concour, + Err(err) => { + tracing::error!(error = err.to_string(), "Error finding concour"); + return Err(UpdateConcourError::UnknownError( + "Error finding concour".to_string(), + )); + } + }; + + if concour.is_none() { + info!("Concour doesn't exist"); + return Err(UpdateConcourError::DoesntExist); + } + + let mut concour = concour.unwrap(); + if let Some(title) = title { + concour.title = title; + } + if let Some(description) = description { + concour.description = description; + } + if let Some(role_recopense) = role_recopense { + concour.role_recompense = role_recopense; + } + if let Some(banner) = banner { + concour.banner = Some(banner); + } + + match concour.update().await { + Ok(_) => {} + Err(err) => { + tracing::error!(error = err.to_string(), "Error updating concour"); + return Err(UpdateConcourError::UnknownError( + "Error updating concour".to_string(), + )); + } + } + Ok(Some(concour)) +} diff --git a/src/db/concour.rs b/src/db/concour.rs index a1af212..042bf7c 100644 --- a/src/db/concour.rs +++ b/src/db/concour.rs @@ -24,13 +24,12 @@ impl fmt::Display for ConcourStatus { #[derive(Debug, Serialize, Deserialize, Clone, ToSchema)] pub struct Concour { pub server_id: u64, - pub enable: bool, pub channel_id: u64, pub title: String, pub description: String, pub start_date: chrono::DateTime, pub periode: time::Duration, - pub role_récompense: u64, + pub role_recompense: u64, pub keywords: Vec, pub banner: Option, pub index_keyword: u64, @@ -42,13 +41,12 @@ impl Default for Concour { fn default() -> Self { Self { server_id: 0, - enable: true, channel_id: 0, title: String::new(), description: String::new(), start_date: chrono::Utc::now(), periode: time::Duration::days(0), - role_récompense: 0, + role_recompense: 0, keywords: Vec::new(), banner: None, index_keyword: 0, @@ -62,13 +60,12 @@ impl Concour { pub fn new(server_id: u64, channel_id: u64) -> Result { Ok(Self { server_id, - enable: true, channel_id, title: String::new(), description: String::new(), start_date: chrono::Utc::now(), periode: time::Duration::days(1), - role_récompense: 0, + role_recompense: 0, keywords: Vec::new(), banner: None, index_keyword: 0, @@ -87,8 +84,8 @@ impl Concour { } pub async fn update(&self) -> Result<(), surrealdb::Error> { let sql = format!( - "UPDATE {} SET enable = {}, title = '{}', description = '{}', start_date = '{}', periode = '{}', role_récompense = {}, keywords = '{:?}', banner = '{:?}', index_keyword = {}, status = '{}', winner = '{:?}' WHERE server_id = {} and channel_id = {}", - CONCOUR, self.enable, self.title, self.description, self.start_date, self.periode, self.role_récompense, self.keywords, self.banner, self.index_keyword, self.status, self.winner, self.server_id, self.channel_id + "UPDATE {} SET title = '{}', description = '{}', start_date = '{}', periode = '{}', role_récompense = {}, keywords = '{:?}', banner = '{:?}', index_keyword = {}, status = '{}', winner = '{:?}' WHERE server_id = {} and channel_id = {}", + CONCOUR, self.title, self.description, self.start_date, self.periode, self.role_recompense, self.keywords, self.banner, self.index_keyword, self.status, self.winner, self.server_id, self.channel_id ); match DB.query(sql).await { Ok(res) => {