feat: handler the update concour

This commit is contained in:
Max batleforc 2024-06-21 10:24:27 +02:00
parent f7fefb1ab6
commit 2ae3e96f36
No known key found for this signature in database
GPG Key ID: 25D243AB4B6AC9E7
3 changed files with 67 additions and 8 deletions

View File

@ -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;

View File

@ -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<String>,
description: Option<String>,
role_recopense: Option<u64>,
banner: Option<String>,
) -> Result<Option<Concour>, 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))
}

View File

@ -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<chrono::Utc>,
pub periode: time::Duration,
pub role_récompense: u64,
pub role_recompense: u64,
pub keywords: Vec<String>,
pub banner: Option<String>,
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<Self, surrealdb::Error> {
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) => {