diff --git a/src-tauri/src/core/common.rs b/src-tauri/src/core/common.rs new file mode 100644 index 000000000..c4684ea0a --- /dev/null +++ b/src-tauri/src/core/common.rs @@ -0,0 +1,35 @@ +use diesel::prelude::*; + +use crate::{ + db::establish_connection, + feed::article::ArticleQueryResult, + models::{self, Article}, + schema, +}; + +pub struct Common {} +pub struct GlobalSearchQuery { + pub query: String, + pub limit: Option, + pub cursor: Option, +} + +impl Common { + pub fn global_search(search: GlobalSearchQuery) -> Vec { + let mut connection = establish_connection(); + let query = search.query; + let limit = search.limit.unwrap_or(12); + let cursor = search.cursor.unwrap_or(1); + + let result = schema::articles::dsl::articles + .filter(schema::articles::dsl::title.like(format!("%{}%", query))) + .filter(schema::articles::dsl::content.like(format!("%{}%",query))) + .limit(limit) + .offset((cursor - 1) * limit) + .load::(&mut connection) + .unwrap(); + + println!("result {:?}", result); + result + } +} diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index f4b6fb14d..53f2ad949 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -1,3 +1,4 @@ +pub mod common; pub mod tray; pub mod scraper; pub mod menu; diff --git a/src-tauri/src/server/handlers/article.rs b/src-tauri/src/server/handlers/article.rs index b181b61b8..a374b4bd5 100644 --- a/src-tauri/src/server/handlers/article.rs +++ b/src-tauri/src/server/handlers/article.rs @@ -4,11 +4,6 @@ use serde::{Deserialize, Serialize}; use crate::core; use crate::feed; -#[derive(Serialize)] -struct MyObj { - name: String, -} - #[get("/api/articles/{uuid}")] pub async fn handle_get_article_detail(uuid: web::Path) -> Result { let res = feed::article::Article::get_article_with_uuid(uuid.to_string()); diff --git a/src-tauri/src/server/handlers/common.rs b/src-tauri/src/server/handlers/common.rs index 388541b49..a5218685e 100644 --- a/src-tauri/src/server/handlers/common.rs +++ b/src-tauri/src/server/handlers/common.rs @@ -1,20 +1,39 @@ use actix_web::{get, post, web, Responder, Result}; use serde::{Deserialize, Serialize}; -use crate::core; +use crate::core::common; + +#[derive(Debug, Deserialize)] +pub struct SearchRequest { + pub query: String, + pub limit: Option, + pub cursor: Option, +} #[get("/api/search")] -pub async fn handle_search() -> Result {} +pub async fn handle_search(search: web::Query) -> Result { + let result = common::Common::global_search(common::GlobalSearchQuery { + query: search.query.to_string(), + limit: search.limit, + cursor: search.cursor + }); + + Ok(web::Json(result)) +} #[get("/api/favorite")] -pub async fn handle_get_favorite() -> Result {} +pub async fn handle_get_favorite() -> Result { + Ok("") +} #[post("/api/favorite")] -pub async fn handle_update_favorite() -> Result {} +pub async fn handle_update_favorite() -> Result { + Ok("") +} pub fn config(cfg: &mut web::ServiceConfig) { cfg .service(handle_search) - .service(handle_favorite) + .service(handle_get_favorite) .service(handle_update_favorite); } diff --git a/src-tauri/src/server/handlers/mod.rs b/src-tauri/src/server/handlers/mod.rs index f9aed2e8a..322872b43 100644 --- a/src-tauri/src/server/handlers/mod.rs +++ b/src-tauri/src/server/handlers/mod.rs @@ -1,2 +1,3 @@ +pub mod common; pub mod article; pub mod feed; diff --git a/src-tauri/src/server/mod.rs b/src-tauri/src/server/mod.rs index 9595d6706..ea6ca5aa1 100644 --- a/src-tauri/src/server/mod.rs +++ b/src-tauri/src/server/mod.rs @@ -27,6 +27,7 @@ pub async fn init(app: AppHandle) -> std::io::Result<()> { .wrap(cors) .app_data(tauri_app.clone()) .wrap(middleware::Logger::default()) + .configure(handlers::common::config) .configure(handlers::article::config) .configure(handlers::feed::config) })