From 27f2feb7a4e33f3ae0d8cabe15897ef56ad3a5f5 Mon Sep 17 00:00:00 2001 From: Andres Velasco Date: Tue, 29 Apr 2025 23:48:07 -0500 Subject: [PATCH] feat: added endpoint for publish winner book --- src/book.rs | 13 ++++++++++ src/consts.rs | 1 + src/main.rs | 1 + src/router/book_club.rs | 56 +++++++++++++++++++++++++++++++++++++++++ src/router/mod.rs | 1 + src/router/setup.rs | 3 ++- 6 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 src/book.rs create mode 100644 src/router/book_club.rs diff --git a/src/book.rs b/src/book.rs new file mode 100644 index 0000000..065c490 --- /dev/null +++ b/src/book.rs @@ -0,0 +1,13 @@ +use serenity::all::{CreateEmbed, CreateEmbedFooter}; + +use crate::{errors::CustomError, router::book_club::BookSubmission}; + +pub async fn create_book_embeb(book: BookSubmission) -> Result { + let embed = CreateEmbed::new() + .title(book.title) + .description(format!("{}\n\nvotes: `{}`\npowered by: <@{}>",book.description,book.votes, book.creator_id)) + .footer(CreateEmbedFooter::new(book.authors.join("-").to_string())) + .image(book.cover_url); + + Ok(embed) +} diff --git a/src/consts.rs b/src/consts.rs index 5a9ecfd..8fac758 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -2,6 +2,7 @@ pub const GITHUB_ORG_ID: &str = "171086856"; pub const GITHUB_ORG_NAME: &str = "Chads-Programming"; pub const COMMUNICATIONS_CHANNEL_ID: u64 = 1260104740746825768; pub const GENERAL_CHANNEL_ID: u64 = 1242262189985763471; +pub const NOTIFICATIONS_CHANNEL_ID: u64 = 1244371195206570066; pub const COURSES_CHANNEL_ID: u64 = 1263988904919433247; pub const WELCOME_CHANNEL_ID: u64 = 1244512390091898931; pub const CAT_IMAGE: &str = "./assets/cat_cat.png"; diff --git a/src/main.rs b/src/main.rs index b1c2909..4d83464 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod book; mod bot; mod commands; mod consts; diff --git a/src/router/book_club.rs b/src/router/book_club.rs new file mode 100644 index 0000000..0c0709d --- /dev/null +++ b/src/router/book_club.rs @@ -0,0 +1,56 @@ +use axum::{extract::State, response::IntoResponse, routing::post, Json, Router}; +use reqwest::StatusCode; +use serde::Deserialize; +use tracing::error; + +use crate::{book, consts, utils}; + +use super::setup::RouterState; + +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct BookSubmission { + pub title: String, + pub authors: Vec, + pub description: String, + pub cover_url: String, + pub votes: i32, + pub creator_id: String, +} + +async fn publish_book_winner( + State(ctx): State, + Json(body): Json, +) -> impl IntoResponse { + let embeb_book_result = book::create_book_embeb(body).await; + + if embeb_book_result.is_err() { + return (StatusCode::BAD_GATEWAY, "Error on embed book").into_response(); + } + + let response = utils::send_embeds_to_channel( + &ctx.0, + consts::NOTIFICATIONS_CHANNEL_ID, + vec![embeb_book_result.unwrap()], + Some("Libro elegido por el club".to_string()), + ) + .await; + + if let Err(err) = response { + error!("Error on send message: {err}"); + + return ( + StatusCode::BAD_GATEWAY, + "Error on \"publish book\" reminder", + ) + .into_response(); + } + + tracing::info!("Message was sending to channel [COMUNICADOS]"); + + (StatusCode::OK, "Published \"winner book\" reminder").into_response() +} + +pub fn build_router() -> Router { + Router::new().route("/publish-winner-book", post(publish_book_winner)) +} diff --git a/src/router/mod.rs b/src/router/mod.rs index a700243..54a27f6 100644 --- a/src/router/mod.rs +++ b/src/router/mod.rs @@ -1,3 +1,4 @@ +pub mod book_club; pub mod health_check; pub mod reminders; pub mod scraping; diff --git a/src/router/setup.rs b/src/router/setup.rs index 30a8bd1..774ce1a 100644 --- a/src/router/setup.rs +++ b/src/router/setup.rs @@ -10,7 +10,7 @@ use axum::{middleware, Router}; use crate::wallet::services::WalletService; -use super::{health_check, reminders, scraping, wallet}; +use super::{book_club, health_check, reminders, scraping, wallet}; #[derive(Clone, Debug)] pub struct RouterSecrets { @@ -48,6 +48,7 @@ pub fn build_router(secrets: RouterSecrets, state: RouterState) -> Router { .nest("/scraping", scraping::build_router()) .nest("/reminder", reminders::build_router()) .nest("/wallet", wallet::build_router()) + .nest("/book-club", book_club::build_router()) .layer(middleware::from_fn_with_state(secrets, api_key_strategy)) .route("/hello-chad", get(health_check::hello_chad)) .with_state(state)