Skip to content

Commit

Permalink
feat: search api
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglun committed Nov 25, 2023
1 parent d9a6abe commit ba2459b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
35 changes: 35 additions & 0 deletions src-tauri/src/core/common.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
pub cursor: Option<i64>,
}

impl Common {
pub fn global_search(search: GlobalSearchQuery) -> Vec<models::Article> {
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::<models::Article>(&mut connection)
.unwrap();

println!("result {:?}", result);
result
}
}
1 change: 1 addition & 0 deletions src-tauri/src/core/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod common;
pub mod tray;
pub mod scraper;
pub mod menu;
Expand Down
5 changes: 0 additions & 5 deletions src-tauri/src/server/handlers/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) -> Result<impl Responder> {
let res = feed::article::Article::get_article_with_uuid(uuid.to_string());
Expand Down
29 changes: 24 additions & 5 deletions src-tauri/src/server/handlers/common.rs
Original file line number Diff line number Diff line change
@@ -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<i64>,
pub cursor: Option<i64>,
}

#[get("/api/search")]
pub async fn handle_search() -> Result<impl Responder> {}
pub async fn handle_search(search: web::Query<SearchRequest>) -> Result<impl Responder> {
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<impl Responder> {}
pub async fn handle_get_favorite() -> Result<impl Responder> {
Ok("")
}

#[post("/api/favorite")]
pub async fn handle_update_favorite() -> Result<impl Responder> {}
pub async fn handle_update_favorite() -> Result<impl Responder> {
Ok("")
}

pub fn config(cfg: &mut web::ServiceConfig) {
cfg
.service(handle_search)
.service(handle_favorite)
.service(handle_get_favorite)
.service(handle_update_favorite);
}
1 change: 1 addition & 0 deletions src-tauri/src/server/handlers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod common;
pub mod article;
pub mod feed;
1 change: 1 addition & 0 deletions src-tauri/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit ba2459b

Please sign in to comment.