add cat model & list endpoint

This commit is contained in:
Mia Pilchová 2025-02-24 14:44:35 +01:00
parent 443bfa29eb
commit fac4053cf4
9 changed files with 85 additions and 0 deletions

1
Cargo.lock generated
View File

@ -532,6 +532,7 @@ dependencies = [
"dotenvy", "dotenvy",
"hmac", "hmac",
"jwt", "jwt",
"regex",
"sea-orm", "sea-orm",
"serde", "serde",
"sha2", "sha2",

View File

@ -17,3 +17,4 @@ jwt = "0.16"
hmac = "0.12" hmac = "0.12"
sha2 = "0.10" sha2 = "0.10"
derive_more = { version = "2.0", features = ["full"] } derive_more = { version = "2.0", features = ["full"] }
regex = "1.11"

View File

@ -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<AppState>) -> Result<impl Responder, GenericError> {
let users = Cat::find()
.all(&state.db)
.await
.map_err(|e| GenericError::new(e.to_string().as_str()))?;
Ok(Json(CatModel::from_many(users)))
}

View File

@ -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)
}

View File

@ -1,12 +1,14 @@
use actix_web::web::ServiceConfig; use actix_web::web::ServiceConfig;
pub mod auth; pub mod auth;
pub mod cats;
pub mod misc; pub mod misc;
mod prelude; mod prelude;
pub mod users; pub mod users;
pub fn register(cfg: &mut ServiceConfig) { pub fn register(cfg: &mut ServiceConfig) {
cfg.service(auth::scope()); cfg.service(auth::scope());
cfg.service(cats::scope());
cfg.service(misc::scope()); cfg.service(misc::scope());
cfg.service(users::scope()); cfg.service(users::scope());
} }

40
src/models/cat.rs Normal file
View File

@ -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<String>,
// cat_tags
pub content: Option<String>,
pub page_title: Option<String>,
pub date: String,
// camera_tag,
// tags
}
impl From<crate::entities::cat::Model> 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<crate::entities::cat::Model>) -> Vec<Self> {
values.into_iter().map(Into::into).collect()
}
}

View File

@ -1 +1,2 @@
pub mod cat;
pub mod user; pub mod user;

View File

@ -1,3 +1,4 @@
pub mod errors; pub mod errors;
pub mod hasher; pub mod hasher;
pub mod jwt; pub mod jwt;
pub mod strings;

16
src/utils/strings.rs Normal file
View File

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