Skip to content

Commit

Permalink
added user_id foreign key in the search_history
Browse files Browse the repository at this point in the history
  • Loading branch information
rathijitpapon committed Apr 5, 2024
1 parent 66574d0 commit 4e494bf
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions server/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ tracing-subscriber = { version = "0.3.18", features = [
"registry",
"env-filter",
] }
uuid = { version = "1.8.0", features = ["serde"] }
uuid = { version = "1.8.0", features = ["serde", "v4"] }
log = "0.4.21"
redis = { version = "0.25.2", features = ["tokio-comp", "json"] }
1 change: 1 addition & 0 deletions server/migrations/20240403115016_search_history.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
create table search_history (
search_history_id uuid primary key default uuid_generate_v1mc(),
user_id uuid not null references users(user_id),
query text not null,
result text not null,
sources text[] not null,
Expand Down
2 changes: 1 addition & 1 deletion server/src/search/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct SearchResponse {
#[derive(FromRow, Serialize, Deserialize, Clone, Debug)]
pub struct SearchHistory {
pub search_history_id: uuid::Uuid,
// pub user_id: uuid::Uuid,
pub user_id: uuid::Uuid,
pub query: String,
pub result: String,
pub sources: Vec<String>,
Expand Down
17 changes: 14 additions & 3 deletions server/src/search/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ async fn get_search_handler(
State(cache): State<RedisClient>,
Query(search_query): Query<SearchQueryRequest>,
) -> crate::Result<impl IntoResponse> {
let user_id = uuid::Uuid::parse_str("78c4c766-f310-11ee-a6ee-5f4062fc15f2").unwrap();

let mut connection = cache
.get_multiplexed_async_connection()
.await
.map_err(|e| AppError::from(e))?;

let search_response = services::search(&mut connection, &search_query).await?;
services::insert_search_history(&pool, &mut connection, &search_query, &search_response)
.await?;
services::insert_search_history(
&pool,
&mut connection,
&user_id,
&search_query,
&search_response,
)
.await?;

Ok((StatusCode::OK, Json(search_response)))
}
Expand All @@ -33,9 +41,12 @@ async fn get_search_history_handler(
State(pool): State<PgPool>,
Query(search_history_request): Query<SearchHistoryRequest>,
) -> crate::Result<impl IntoResponse> {
let user_id = uuid::Uuid::parse_str("78c4c766-f310-11ee-a6ee-5f4062fc15f2").unwrap();

let search_history = sqlx::query_as!(
SearchHistory,
"select * from search_history order by created_at desc limit $1 offset $2",
"select * from search_history where user_id = $1 order by created_at desc limit $2 offset $3",
user_id,
search_history_request.limit.unwrap_or(10) as i64,
search_history_request.offset.unwrap_or(0) as i64
)
Expand Down
9 changes: 6 additions & 3 deletions server/src/search/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use redis::aio::MultiplexedConnection;
use redis::AsyncCommands;
use reqwest::Client as ReqwestClient;
use sqlx::PgPool;
use uuid::Uuid;

#[tracing::instrument(level = "debug", ret, err)]
pub async fn search(
Expand Down Expand Up @@ -58,6 +59,7 @@ pub async fn search(
pub async fn insert_search_history(
pool: &PgPool,
cache: &mut MultiplexedConnection,
user_id: &Uuid,
search_query: &SearchQueryRequest,
search_response: &SearchResponse,
) -> crate::Result<SearchHistory> {
Expand All @@ -72,9 +74,10 @@ pub async fn insert_search_history(

let search_history = sqlx::query_as!(
SearchHistory,
"insert into search_history (query, result, sources) values ($1, $2, $3) returning *",
&search_query.query,
&search_response.result,
"insert into search_history (user_id, query, result, sources) values ($1, $2, $3, $4) returning *",
user_id,
search_query.query,
search_response.result,
&search_response.sources
)
.fetch_one(pool)
Expand Down

0 comments on commit 4e494bf

Please sign in to comment.