Skip to content

Commit

Permalink
feat: 横断検索エンドポイントでコメントも返すように
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Sep 25, 2024
1 parent 41ced3f commit 53ab2b7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion server/domain/src/repository/search_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use errors::Error;
use mockall::automock;

use crate::{
form::models::{Answer, Form, Label},
form::models::{Answer, Comment, Form, Label},
user::models::User,
};

Expand All @@ -15,4 +15,5 @@ pub trait SearchRepository: Send + Sync + 'static {
async fn search_labels_for_forms(&self, query: String) -> Result<Vec<Label>, Error>;
async fn search_labels_for_answers(&self, query: String) -> Result<Vec<Label>, Error>;
async fn search_answers(&self, query: String) -> Result<Vec<Answer>, Error>;
async fn search_comments(&self, query: String) -> Result<Vec<Comment>, Error>;
}
3 changes: 2 additions & 1 deletion server/domain/src/search/models.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::{
form::models::{Answer, Form, Label},
form::models::{Answer, Comment, Form, Label},
user::models::User,
};

Expand All @@ -12,6 +12,7 @@ pub struct CrossSearchResult {
pub answers: Vec<Answer>,
pub label_for_forms: Vec<Label>,
pub label_for_answers: Vec<Label>,
pub comments: Vec<Comment>,
}

#[derive(Deserialize, Debug, PartialEq)]
Expand Down
1 change: 1 addition & 0 deletions server/infra/resource/src/database/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,5 @@ pub trait SearchDatabase: Send + Sync {
async fn search_labels_for_forms(&self, query: String) -> Result<Vec<Label>, InfraError>;
async fn search_labels_for_answers(&self, query: String) -> Result<Vec<Label>, InfraError>;
async fn search_answers(&self, query: String) -> Result<Vec<Answer>, InfraError>;
async fn search_comments(&self, query: String) -> Result<Vec<Comment>, InfraError>;
}
17 changes: 16 additions & 1 deletion server/infra/resource/src/database/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;
use domain::{
form::models::{Answer, Form, Label},
form::models::{Answer, Comment, Form, Label},
user::models::User,
};
use errors::infra::InfraError;
Expand Down Expand Up @@ -85,4 +85,19 @@ impl SearchDatabase for ConnectionPool {
.map(|hit| hit.result)
.collect_vec())
}

async fn search_comments(&self, query: String) -> Result<Vec<Comment>, InfraError> {
Ok(self
.meilisearch_client
.index("form_answer_comments")
.search()
.with_query(query.as_str())
.with_attributes_to_highlight(Selectors::All)
.execute::<Comment>()
.await?
.hits
.into_iter()
.map(|hit| hit.result)
.collect_vec())
}
}
10 changes: 9 additions & 1 deletion server/infra/resource/src/repository/search_repository_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_trait::async_trait;
use domain::{
form::models::{Answer, Form, Label},
form::models::{Answer, Comment, Form, Label},
repository::search_repository::SearchRepository,
user::models::User,
};
Expand Down Expand Up @@ -52,4 +52,12 @@ impl<Client: DatabaseComponents + 'static> SearchRepository for Repository<Clien
.await
.map_err(Into::into)
}

async fn search_comments(&self, query: String) -> Result<Vec<Comment>, Error> {
self.client
.search()
.search_comments(query)
.await
.map_err(Into::into)
}
}
6 changes: 4 additions & 2 deletions server/usecase/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ pub struct SearchUseCase<'a, SearchRepo: SearchRepository> {

impl<R: SearchRepository> SearchUseCase<'_, R> {
pub async fn cross_search(&self, query: String) -> Result<CrossSearchResult, Error> {
let (forms, users, label_for_forms, label_for_answers, answers) = try_join!(
let (forms, users, label_for_forms, label_for_answers, answers, comments) = try_join!(
self.repository.search_forms(query.to_owned()),
self.repository.search_users(query.to_owned()),
self.repository.search_labels_for_forms(query.to_owned()),
self.repository.search_labels_for_answers(query.to_owned()),
self.repository.search_answers(query.to_owned())
self.repository.search_answers(query.to_owned()),
self.repository.search_comments(query.to_owned())
)?;

Ok(CrossSearchResult {
Expand All @@ -22,6 +23,7 @@ impl<R: SearchRepository> SearchUseCase<'_, R> {
label_for_forms,
label_for_answers,
answers,
comments,
})
}
}

0 comments on commit 53ab2b7

Please sign in to comment.