Skip to content

Commit

Permalink
Merge pull request #559 from GiganticMinecraft/feat/searchComments
Browse files Browse the repository at this point in the history
横断検索でコメントの検索結果も返すように
  • Loading branch information
rito528 authored Sep 25, 2024
2 parents 87ae049 + 4d78443 commit cfd77f9
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 33 deletions.
3 changes: 2 additions & 1 deletion docker/meilisync/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ sync:
full: true
- table: users
full: true

- table: form_answer_comments
full: true
1 change: 1 addition & 0 deletions server/domain/src/form/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ pub type CommentId = types::Id<Comment>;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Comment {
pub answer_id: AnswerId,
pub comment_id: CommentId,
pub content: String,
pub timestamp: DateTime<Utc>,
Expand Down
12 changes: 7 additions & 5 deletions server/domain/src/repository/search_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ use mockall::automock;

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

#[automock]
#[async_trait]
pub trait SearchRepository: Send + Sync + 'static {
async fn search_users(&self, query: String) -> Result<Vec<User>, Error>;
async fn search_forms(&self, query: String) -> Result<Vec<Form>, Error>;
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_users(&self, query: &str) -> Result<Vec<User>, Error>;
async fn search_forms(&self, query: &str) -> Result<Vec<Form>, Error>;
async fn search_labels_for_forms(&self, query: &str) -> Result<Vec<Label>, Error>;
async fn search_labels_for_answers(&self, query: &str) -> Result<Vec<Label>, Error>;
async fn search_answers(&self, query: &str) -> Result<Vec<Answer>, Error>;
async fn search_comments(&self, query: &str) -> Result<Vec<Comment>, Error>;
}
12 changes: 11 additions & 1 deletion server/domain/src/search/models.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use serde::{Deserialize, Serialize};
use uuid::Uuid;

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

#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct Comment {
pub answer_id: AnswerId,
pub id: CommentId,
pub content: String,
pub commented_by: Uuid,
}

#[derive(Serialize, Debug, PartialEq)]
pub struct CrossSearchResult {
pub forms: Vec<Form>,
pub users: Vec<User>,
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
14 changes: 9 additions & 5 deletions server/infra/resource/src/database/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ pub trait UserDatabase: Send + Sync {
#[automock]
#[async_trait]
pub trait SearchDatabase: Send + Sync {
async fn search_users(&self, query: String) -> Result<Vec<User>, InfraError>;
async fn search_forms(&self, query: String) -> Result<Vec<Form>, InfraError>;
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_users(&self, query: &str) -> Result<Vec<User>, InfraError>;
async fn search_forms(&self, query: &str) -> Result<Vec<Form>, InfraError>;
async fn search_labels_for_forms(&self, query: &str) -> Result<Vec<Label>, InfraError>;
async fn search_labels_for_answers(&self, query: &str) -> Result<Vec<Label>, InfraError>;
async fn search_answers(&self, query: &str) -> Result<Vec<Answer>, InfraError>;
async fn search_comments(
&self,
query: &str,
) -> Result<Vec<domain::search::models::Comment>, InfraError>;
}
3 changes: 3 additions & 0 deletions server/infra/resource/src/database/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ impl FormDatabase for ConnectionPool {
iter()
.map(|rs| {
Ok::<CommentDto, InfraError>(CommentDto {
answer_id: rs.try_get("", "answer_id")?,
comment_id: rs.try_get("", "comment_id")?,
content: rs.try_get("", "content")?,
timestamp: rs.try_get("", "timestamp")?,
Expand Down Expand Up @@ -712,6 +713,7 @@ impl FormDatabase for ConnectionPool {
})
.map(|rs| {
Ok::<CommentDto, InfraError>(CommentDto {
answer_id: rs.try_get("", "answer_id")?,
comment_id: rs.try_get("", "comment_id")?,
content: rs.try_get("", "content")?,
timestamp: rs.try_get("", "timestamp")?,
Expand Down Expand Up @@ -812,6 +814,7 @@ impl FormDatabase for ConnectionPool {
})
.map(|rs| {
Ok::<CommentDto, InfraError>(CommentDto {
answer_id: rs.try_get("", "answer_id")?,
comment_id: rs.try_get("", "comment_id")?,
content: rs.try_get("", "content")?,
timestamp: rs.try_get("", "timestamp")?,
Expand Down
36 changes: 26 additions & 10 deletions server/infra/resource/src/database/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;
use domain::{
form::models::{Answer, Form, Label},
search::models::Comment,
user::models::User,
};
use errors::infra::InfraError;
Expand All @@ -11,12 +12,12 @@ use crate::database::{components::SearchDatabase, connection::ConnectionPool};

#[async_trait]
impl SearchDatabase for ConnectionPool {
async fn search_users(&self, query: String) -> Result<Vec<User>, InfraError> {
async fn search_users(&self, query: &str) -> Result<Vec<User>, InfraError> {
Ok(self
.meilisearch_client
.index("users")
.search()
.with_query(&query)
.with_query(query)
.with_attributes_to_highlight(Selectors::All)
.execute::<User>()
.await?
Expand All @@ -26,12 +27,12 @@ impl SearchDatabase for ConnectionPool {
.collect_vec())
}

async fn search_forms(&self, query: String) -> Result<Vec<Form>, InfraError> {
async fn search_forms(&self, query: &str) -> Result<Vec<Form>, InfraError> {
Ok(self
.meilisearch_client
.index("form_meta_data")
.search()
.with_query(query.as_str())
.with_query(query)
.with_attributes_to_highlight(Selectors::All)
.execute::<Form>()
.await?
Expand All @@ -41,12 +42,12 @@ impl SearchDatabase for ConnectionPool {
.collect_vec())
}

async fn search_labels_for_forms(&self, query: String) -> Result<Vec<Label>, InfraError> {
async fn search_labels_for_forms(&self, query: &str) -> Result<Vec<Label>, InfraError> {
Ok(self
.meilisearch_client
.index("label_for_forms")
.search()
.with_query(query.as_str())
.with_query(query)
.with_attributes_to_highlight(Selectors::All)
.execute::<Label>()
.await?
Expand All @@ -56,12 +57,12 @@ impl SearchDatabase for ConnectionPool {
.collect_vec())
}

async fn search_labels_for_answers(&self, query: String) -> Result<Vec<Label>, InfraError> {
async fn search_labels_for_answers(&self, query: &str) -> Result<Vec<Label>, InfraError> {
Ok(self
.meilisearch_client
.index("label_for_form_answers")
.search()
.with_query(query.as_str())
.with_query(query)
.with_attributes_to_highlight(Selectors::All)
.execute::<Label>()
.await?
Expand All @@ -71,12 +72,12 @@ impl SearchDatabase for ConnectionPool {
.collect_vec())
}

async fn search_answers(&self, query: String) -> Result<Vec<Answer>, InfraError> {
async fn search_answers(&self, query: &str) -> Result<Vec<Answer>, InfraError> {
Ok(self
.meilisearch_client
.index("real_answers")
.search()
.with_query(query.as_str())
.with_query(query)
.with_attributes_to_highlight(Selectors::All)
.execute::<Answer>()
.await?
Expand All @@ -85,4 +86,19 @@ impl SearchDatabase for ConnectionPool {
.map(|hit| hit.result)
.collect_vec())
}

async fn search_comments(&self, query: &str) -> Result<Vec<Comment>, InfraError> {
Ok(self
.meilisearch_client
.index("form_answer_comments")
.search()
.with_query(query)
.with_attributes_to_highlight(Selectors::All)
.execute::<Comment>()
.await?
.hits
.into_iter()
.map(|hit| hit.result)
.collect_vec())
}
}
3 changes: 3 additions & 0 deletions server/infra/resource/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl TryFrom<UserDto> for User {
}

pub struct CommentDto {
pub answer_id: i32,
pub comment_id: i32,
pub content: String,
pub timestamp: DateTime<Utc>,
Expand All @@ -185,13 +186,15 @@ impl TryFrom<CommentDto> for domain::form::models::Comment {

fn try_from(
CommentDto {
answer_id,
comment_id,
content,
timestamp,
commented_by,
}: CommentDto,
) -> Result<Self, Self::Error> {
Ok(domain::form::models::Comment {
answer_id: answer_id.into(),
comment_id: comment_id.into(),
content,
timestamp,
Expand Down
19 changes: 14 additions & 5 deletions server/infra/resource/src/repository/search_repository_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use async_trait::async_trait;
use domain::{
form::models::{Answer, Form, Label},
repository::search_repository::SearchRepository,
search::models::Comment,
user::models::User,
};
use errors::Error;
Expand All @@ -13,43 +14,51 @@ use crate::{

#[async_trait]
impl<Client: DatabaseComponents + 'static> SearchRepository for Repository<Client> {
async fn search_users(&self, query: String) -> Result<Vec<User>, Error> {
async fn search_users(&self, query: &str) -> Result<Vec<User>, Error> {
self.client
.search()
.search_users(query)
.await
.map_err(Into::into)
}

async fn search_forms(&self, query: String) -> Result<Vec<Form>, Error> {
async fn search_forms(&self, query: &str) -> Result<Vec<Form>, Error> {
self.client
.search()
.search_forms(query)
.await
.map_err(Into::into)
}

async fn search_labels_for_forms(&self, query: String) -> Result<Vec<Label>, Error> {
async fn search_labels_for_forms(&self, query: &str) -> Result<Vec<Label>, Error> {
self.client
.search()
.search_labels_for_forms(query)
.await
.map_err(Into::into)
}

async fn search_labels_for_answers(&self, query: String) -> Result<Vec<Label>, Error> {
async fn search_labels_for_answers(&self, query: &str) -> Result<Vec<Label>, Error> {
self.client
.search()
.search_labels_for_answers(query)
.await
.map_err(Into::into)
}

async fn search_answers(&self, query: String) -> Result<Vec<Answer>, Error> {
async fn search_answers(&self, query: &str) -> Result<Vec<Answer>, Error> {
self.client
.search()
.search_answers(query)
.await
.map_err(Into::into)
}

async fn search_comments(&self, query: &str) -> Result<Vec<Comment>, Error> {
self.client
.search()
.search_comments(query)
.await
.map_err(Into::into)
}
}
1 change: 1 addition & 0 deletions server/presentation/src/form_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ pub async fn post_form_comment(
let comment = Comment {
// NOTE: コメントはデータベースで insert した後に id が振られるのでデフォルト値を入れておく
comment_id: Default::default(),
answer_id: comment_schema.answer_id,
content: comment_schema.content,
timestamp: chrono::Utc::now(),
commented_by: user,
Expand Down
14 changes: 8 additions & 6 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!(
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())
let (forms, users, label_for_forms, label_for_answers, answers, comments) = try_join!(
self.repository.search_forms(&query),
self.repository.search_users(&query),
self.repository.search_labels_for_forms(&query),
self.repository.search_labels_for_answers(&query),
self.repository.search_answers(&query),
self.repository.search_comments(&query)
)?;

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 cfd77f9

Please sign in to comment.