Skip to content

Commit

Permalink
refactor: Resolver 関数を廃止
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Dec 14, 2024
1 parent 21d6414 commit 668493b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 49 deletions.
19 changes: 1 addition & 18 deletions server/domain/src/form/models.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
#[cfg(test)]
use common::test_utils::{arbitrary_date_time, arbitrary_opt_date_time, arbitrary_with_size};
use derive_getters::Getters;
use deriving_via::DerivingVia;
use errors::{domain::DomainError, Error};
use errors::domain::DomainError;
#[cfg(test)]
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};
use typed_builder::TypedBuilder;
use types::Resolver;

use crate::{
repository::form_repository::FormRepository,
types::authorization_guard::AuthorizationGuardDefinitions,
user::models::{Role::Administrator, User},
};

pub type FormId = types::IntegerId<Form>;

#[async_trait]
impl<Repo: FormRepository + Sized + Sync> Resolver<Form, Error, Repo> for FormId {
async fn resolve(&self, repo: &Repo) -> Result<Option<Form>, Error> {
repo.get(self.to_owned()).await
}
}

#[derive(Deserialize, Debug)]
pub struct OffsetAndLimit {
#[serde(default)]
Expand Down Expand Up @@ -218,13 +208,6 @@ impl DefaultAnswerTitle {

pub type AnswerId = types::IntegerId<FormAnswer>;

#[async_trait]
impl<Repo: FormRepository + Sized + Sync> Resolver<FormAnswer, Error, Repo> for AnswerId {
async fn resolve(&self, repo: &Repo) -> Result<Option<FormAnswer>, Error> {
repo.get_answers(self.to_owned()).await
}
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
pub struct FormAnswer {
pub id: AnswerId,
Expand Down
3 changes: 1 addition & 2 deletions server/infra/resource/src/repository/form_repository_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use domain::{
use errors::{domain::DomainError, infra::InfraError::AnswerNotFount, Error};
use futures::{stream, stream::StreamExt};
use outgoing::form_outgoing;
use types::Resolver;

use crate::{
database::components::{DatabaseComponents, FormDatabase},
Expand Down Expand Up @@ -303,7 +302,7 @@ impl<Client: DatabaseComponents + 'static> FormRepository for Repository<Client>
}

async fn post_comment(&self, answer_id: AnswerId, comment: &Comment) -> Result<(), Error> {
let posted_answers = answer_id.resolve(self).await?.ok_or(AnswerNotFount {
let posted_answers = self.get_answers(answer_id).await?.ok_or(AnswerNotFount {
id: answer_id.into_inner(),
})?;

Expand Down
6 changes: 0 additions & 6 deletions server/types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use async_trait::async_trait;
#[cfg(feature = "arbitrary")]
use common::test_utils::arbitrary_uuid_v7;
use deriving_via::DerivingVia;
Expand Down Expand Up @@ -36,8 +35,3 @@ impl<T> Id<T> {
Self(Uuid::now_v7(), std::marker::PhantomData)
}
}

#[async_trait]
pub trait Resolver<T, Error, Repo> {
async fn resolve(&self, repo: &Repo) -> Result<Option<T>, Error>;
}
45 changes: 22 additions & 23 deletions server/usecase/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use futures::{
future::{join_all, OptionFuture},
stream, try_join, StreamExt,
};
use types::Resolver;

use crate::dto::AnswerDto;

Expand Down Expand Up @@ -146,23 +145,22 @@ impl<R1: FormRepository, R2: NotificationRepository> FormUseCase<'_, R1, R2> {
title: DefaultAnswerTitle,
answers: Vec<FormAnswerContent>,
) -> Result<(), Error> {
let is_within_period = form_id
.resolve(self.form_repository)
.await?
.and_then(|form| {
let response_period = form.settings.response_period;

response_period
.start_at
.zip(response_period.end_at)
.map(|(start_at, end_at)| {
let now = Utc::now();
now >= start_at && now <= end_at
})
})
// Note: Noneの場合はフォームが存在していないかそもそも回答期間が無いフォーム
.unwrap_or(true);

let is_within_period =
self.form_repository
.get(form_id)
.await?
.and_then(|form| {
let response_period = form.settings.response_period;

response_period.start_at.zip(response_period.end_at).map(
|(start_at, end_at)| {
let now = Utc::now();
now >= start_at && now <= end_at
},
)
})
// Note: Noneの場合はフォームが存在していないかそもそも回答期間が無いフォーム
.unwrap_or(true);
if is_within_period {
self.form_repository
.post_answer(user, form_id, title, answers)
Expand Down Expand Up @@ -276,14 +274,15 @@ impl<R1: FormRepository, R2: NotificationRepository> FormUseCase<'_, R1, R2> {
let can_post_comment = match comment.commented_by.role {
Administrator => true,
StandardUser => {
let answer = answer_id
.resolve(self.form_repository)
let answer = self
.form_repository
.get_answers(answer_id)
.await?
.ok_or(AnswerNotFound)?;

let form = answer
.form_id
.resolve(self.form_repository)
let form = self
.form_repository
.get(answer.form_id)
.await?
.ok_or(FormNotFound)?;

Expand Down

0 comments on commit 668493b

Please sign in to comment.