From fac4053cf4c25f2c394b5a409cfc4f5a8212a9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mia=20Pilchov=C3=A1?= Date: Mon, 24 Feb 2025 14:44:35 +0100 Subject: [PATCH] add cat model & list endpoint --- Cargo.lock | 1 + Cargo.toml | 1 + src/endpoints/cats/list.rs | 16 +++++++++++++++ src/endpoints/cats/mod.rs | 7 +++++++ src/endpoints/mod.rs | 2 ++ src/models/cat.rs | 40 ++++++++++++++++++++++++++++++++++++++ src/models/mod.rs | 1 + src/utils/mod.rs | 1 + src/utils/strings.rs | 16 +++++++++++++++ 9 files changed, 85 insertions(+) create mode 100644 src/endpoints/cats/list.rs create mode 100644 src/endpoints/cats/mod.rs create mode 100644 src/models/cat.rs create mode 100644 src/utils/strings.rs diff --git a/Cargo.lock b/Cargo.lock index c2b8926..7ebfbd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -532,6 +532,7 @@ dependencies = [ "dotenvy", "hmac", "jwt", + "regex", "sea-orm", "serde", "sha2", diff --git a/Cargo.toml b/Cargo.toml index d5f6eb9..4293462 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ jwt = "0.16" hmac = "0.12" sha2 = "0.10" derive_more = { version = "2.0", features = ["full"] } +regex = "1.11" diff --git a/src/endpoints/cats/list.rs b/src/endpoints/cats/list.rs new file mode 100644 index 0000000..df8d8a9 --- /dev/null +++ b/src/endpoints/cats/list.rs @@ -0,0 +1,16 @@ +use sea_orm::EntityTrait; + +use crate::{ + endpoints::prelude::*, entities::prelude::Cat, models::cat::CatModel, state::AppState, + utils::errors::GenericError, +}; + +#[get("/")] +pub async fn handler(state: Data) -> Result { + let users = Cat::find() + .all(&state.db) + .await + .map_err(|e| GenericError::new(e.to_string().as_str()))?; + + Ok(Json(CatModel::from_many(users))) +} diff --git a/src/endpoints/cats/mod.rs b/src/endpoints/cats/mod.rs new file mode 100644 index 0000000..51b1604 --- /dev/null +++ b/src/endpoints/cats/mod.rs @@ -0,0 +1,7 @@ +use actix_web::dev::HttpServiceFactory; + +pub mod list; + +pub fn scope() -> impl HttpServiceFactory + 'static { + actix_web::web::scope("/cat").service(list::handler) +} diff --git a/src/endpoints/mod.rs b/src/endpoints/mod.rs index 8d7cde9..9be05df 100644 --- a/src/endpoints/mod.rs +++ b/src/endpoints/mod.rs @@ -1,12 +1,14 @@ use actix_web::web::ServiceConfig; pub mod auth; +pub mod cats; pub mod misc; mod prelude; pub mod users; pub fn register(cfg: &mut ServiceConfig) { cfg.service(auth::scope()); + cfg.service(cats::scope()); cfg.service(misc::scope()); cfg.service(users::scope()); } diff --git a/src/models/cat.rs b/src/models/cat.rs new file mode 100644 index 0000000..4902737 --- /dev/null +++ b/src/models/cat.rs @@ -0,0 +1,40 @@ +use serde::Serialize; + +use crate::utils::strings::create_page_title; + +#[derive(Serialize)] +pub struct CatModel { + pub id: i32, + pub image_url: String, + pub thumbnail_url: Option, + // cat_tags + pub content: Option, + pub page_title: Option, + pub date: String, + // camera_tag, + // tags +} + +impl From for CatModel { + fn from(value: crate::entities::cat::Model) -> Self { + Self { + id: value.id, + image_url: format!("/cats/{}", value.image), + thumbnail_url: value + .thumbnail + .map(|thumbnail| format!("/cats/{}", thumbnail)), + content: value.content.clone(), + page_title: value + .content + .clone() + .map(|content| create_page_title(&content)), + date: value.date.and_utc().to_rfc3339(), + } + } +} + +impl CatModel { + pub fn from_many(values: Vec) -> Vec { + values.into_iter().map(Into::into).collect() + } +} diff --git a/src/models/mod.rs b/src/models/mod.rs index 22d12a3..f74a290 100644 --- a/src/models/mod.rs +++ b/src/models/mod.rs @@ -1 +1,2 @@ +pub mod cat; pub mod user; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index d33e1ae..4eb79b9 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,3 +1,4 @@ pub mod errors; pub mod hasher; pub mod jwt; +pub mod strings; diff --git a/src/utils/strings.rs b/src/utils/strings.rs new file mode 100644 index 0000000..bd39454 --- /dev/null +++ b/src/utils/strings.rs @@ -0,0 +1,16 @@ +use regex::Regex; + +pub fn strip_html(content: &str) -> String { + let regex = Regex::new(r"<[^>]*>").unwrap(); + String::from(regex.replace_all(content, "")) +} + +pub fn create_page_title(content: &str) -> String { + let mut title = strip_html(content).chars().take(30).collect(); + + if content.len() > 30 { + title = format!("{}...", title); + } + + title +}