feat: handler the update concour
This commit is contained in:
parent
f7fefb1ab6
commit
2ae3e96f36
@ -2,3 +2,4 @@ pub mod check_if_allowed;
|
|||||||
pub mod create_concour;
|
pub mod create_concour;
|
||||||
pub mod get_channel_concour;
|
pub mod get_channel_concour;
|
||||||
pub mod list_concour;
|
pub mod list_concour;
|
||||||
|
pub mod update_concour;
|
||||||
|
61
src/botv2/domain/concour/update_concour.rs
Normal file
61
src/botv2/domain/concour/update_concour.rs
Normal 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))
|
||||||
|
}
|
@ -24,13 +24,12 @@ impl fmt::Display for ConcourStatus {
|
|||||||
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
|
#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
|
||||||
pub struct Concour {
|
pub struct Concour {
|
||||||
pub server_id: u64,
|
pub server_id: u64,
|
||||||
pub enable: bool,
|
|
||||||
pub channel_id: u64,
|
pub channel_id: u64,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub description: String,
|
pub description: String,
|
||||||
pub start_date: chrono::DateTime<chrono::Utc>,
|
pub start_date: chrono::DateTime<chrono::Utc>,
|
||||||
pub periode: time::Duration,
|
pub periode: time::Duration,
|
||||||
pub role_récompense: u64,
|
pub role_recompense: u64,
|
||||||
pub keywords: Vec<String>,
|
pub keywords: Vec<String>,
|
||||||
pub banner: Option<String>,
|
pub banner: Option<String>,
|
||||||
pub index_keyword: u64,
|
pub index_keyword: u64,
|
||||||
@ -42,13 +41,12 @@ impl Default for Concour {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
server_id: 0,
|
server_id: 0,
|
||||||
enable: true,
|
|
||||||
channel_id: 0,
|
channel_id: 0,
|
||||||
title: String::new(),
|
title: String::new(),
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
start_date: chrono::Utc::now(),
|
start_date: chrono::Utc::now(),
|
||||||
periode: time::Duration::days(0),
|
periode: time::Duration::days(0),
|
||||||
role_récompense: 0,
|
role_recompense: 0,
|
||||||
keywords: Vec::new(),
|
keywords: Vec::new(),
|
||||||
banner: None,
|
banner: None,
|
||||||
index_keyword: 0,
|
index_keyword: 0,
|
||||||
@ -62,13 +60,12 @@ impl Concour {
|
|||||||
pub fn new(server_id: u64, channel_id: u64) -> Result<Self, surrealdb::Error> {
|
pub fn new(server_id: u64, channel_id: u64) -> Result<Self, surrealdb::Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
server_id,
|
server_id,
|
||||||
enable: true,
|
|
||||||
channel_id,
|
channel_id,
|
||||||
title: String::new(),
|
title: String::new(),
|
||||||
description: String::new(),
|
description: String::new(),
|
||||||
start_date: chrono::Utc::now(),
|
start_date: chrono::Utc::now(),
|
||||||
periode: time::Duration::days(1),
|
periode: time::Duration::days(1),
|
||||||
role_récompense: 0,
|
role_recompense: 0,
|
||||||
keywords: Vec::new(),
|
keywords: Vec::new(),
|
||||||
banner: None,
|
banner: None,
|
||||||
index_keyword: 0,
|
index_keyword: 0,
|
||||||
@ -87,8 +84,8 @@ impl Concour {
|
|||||||
}
|
}
|
||||||
pub async fn update(&self) -> Result<(), surrealdb::Error> {
|
pub async fn update(&self) -> Result<(), surrealdb::Error> {
|
||||||
let sql = format!(
|
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 = {}",
|
"UPDATE {} SET 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
|
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 {
|
match DB.query(sql).await {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user