Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/book.rs
Original file line number Diff line number Diff line change
@@ -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<CreateEmbed, CustomError> {
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)
}
1 change: 1 addition & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod book;
mod bot;
mod commands;
mod consts;
Expand Down
56 changes: 56 additions & 0 deletions src/router/book_club.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
pub description: String,
pub cover_url: String,
pub votes: i32,
pub creator_id: String,
}

async fn publish_book_winner(
State(ctx): State<RouterState>,
Json(body): Json<BookSubmission>,
) -> 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<RouterState> {
Router::new().route("/publish-winner-book", post(publish_book_winner))
}
1 change: 1 addition & 0 deletions src/router/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod book_club;
pub mod health_check;
pub mod reminders;
pub mod scraping;
Expand Down
3 changes: 2 additions & 1 deletion src/router/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Loading