add cat model & list endpoint
This commit is contained in:
parent
443bfa29eb
commit
fac4053cf4
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -532,6 +532,7 @@ dependencies = [
|
||||
"dotenvy",
|
||||
"hmac",
|
||||
"jwt",
|
||||
"regex",
|
||||
"sea-orm",
|
||||
"serde",
|
||||
"sha2",
|
||||
|
||||
@ -17,3 +17,4 @@ jwt = "0.16"
|
||||
hmac = "0.12"
|
||||
sha2 = "0.10"
|
||||
derive_more = { version = "2.0", features = ["full"] }
|
||||
regex = "1.11"
|
||||
|
||||
16
src/endpoints/cats/list.rs
Normal file
16
src/endpoints/cats/list.rs
Normal 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)))
|
||||
}
|
||||
7
src/endpoints/cats/mod.rs
Normal file
7
src/endpoints/cats/mod.rs
Normal 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)
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
|
||||
40
src/models/cat.rs
Normal file
40
src/models/cat.rs
Normal 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()
|
||||
}
|
||||
}
|
||||
@ -1 +1,2 @@
|
||||
pub mod cat;
|
||||
pub mod user;
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
pub mod errors;
|
||||
pub mod hasher;
|
||||
pub mod jwt;
|
||||
pub mod strings;
|
||||
|
||||
16
src/utils/strings.rs
Normal file
16
src/utils/strings.rs
Normal 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
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user