From bec8b6bc6663e30577448440c9e510fe773e3830 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Tue, 17 Dec 2024 23:30:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=E3=83=95=E3=82=A9=E3=83=BC?= =?UTF-8?q?=E3=83=A0=E3=83=AA=E3=82=B9=E3=83=88=E7=94=A8=E3=83=8F=E3=83=B3?= =?UTF-8?q?=E3=83=89=E3=83=A9=E3=82=92=E4=B8=80=E3=81=A4=E3=81=AB=E7=B5=B1?= =?UTF-8?q?=E4=B8=80=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/domain/src/form/models.rs | 4 +- server/entrypoint/src/main.rs | 8 +- server/presentation/src/auth.rs | 1 - server/presentation/src/form_handler.rs | 31 +- .../src/schemas/form/form_response_schemas.rs | 312 ++++++++++-------- server/usecase/src/form.rs | 9 +- 6 files changed, 200 insertions(+), 165 deletions(-) diff --git a/server/domain/src/form/models.rs b/server/domain/src/form/models.rs index c0371e1a..159401f8 100644 --- a/server/domain/src/form/models.rs +++ b/server/domain/src/form/models.rs @@ -142,7 +142,9 @@ impl ResponsePeriod { } #[cfg_attr(test, derive(Arbitrary))] -#[derive(Serialize, Deserialize, Debug, EnumString, Display, Default, PartialOrd, PartialEq)] +#[derive( + Serialize, Deserialize, Debug, EnumString, Display, Copy, Clone, Default, PartialOrd, PartialEq, +)] pub enum Visibility { PUBLIC, #[default] diff --git a/server/entrypoint/src/main.rs b/server/entrypoint/src/main.rs index 2244b3c1..00c88bc5 100644 --- a/server/entrypoint/src/main.rs +++ b/server/entrypoint/src/main.rs @@ -21,9 +21,9 @@ use presentation::{ edit_label_for_answers, edit_label_for_forms, form_list_handler, get_all_answers, get_answer_by_form_id_handler, get_answer_handler, get_form_handler, get_labels_for_answers, get_labels_for_forms, get_messages_handler, get_questions_handler, - post_answer_handler, post_form_comment, post_message_handler, public_form_list_handler, - put_question_handler, replace_answer_labels, replace_form_labels, update_answer_handler, - update_form_handler, update_message_handler, + post_answer_handler, post_form_comment, post_message_handler, put_question_handler, + replace_answer_labels, replace_form_labels, update_answer_handler, update_form_handler, + update_message_handler, }, health_check_handler::health_check, notification_handler::{fetch_by_request_user, update_read_state}, @@ -77,8 +77,6 @@ async fn main() -> anyhow::Result<()> { let router = Router::new() .route("/forms", post(create_form_handler).get(form_list_handler)) .with_state(shared_repository.to_owned()) - .route("/forms/public", get(public_form_list_handler)) - .with_state(shared_repository.to_owned()) .route( "/forms/:id", get(get_form_handler) diff --git a/server/presentation/src/auth.rs b/server/presentation/src/auth.rs index bf75aefd..1b389950 100644 --- a/server/presentation/src/auth.rs +++ b/server/presentation/src/auth.rs @@ -57,7 +57,6 @@ pub async fn auth( }; let static_endpoints_allowed_for_standard_users = [ - (&Method::GET, "/forms/public"), (&Method::POST, "/forms/answers"), (&Method::POST, "/forms/answers/comment"), (&Method::GET, "/users"), diff --git a/server/presentation/src/form_handler.rs b/server/presentation/src/form_handler.rs index e4ef4f8b..3a9231f8 100644 --- a/server/presentation/src/form_handler.rs +++ b/server/presentation/src/form_handler.rs @@ -25,7 +25,7 @@ use crate::{ FormQuestionUpdateSchema, FormUpdateSchema, LabelSchema, MessageUpdateSchema, OffsetAndLimit, PostedMessageSchema, ReplaceAnswerLabelSchema, }, - form_response_schemas::{FormAnswer, MessageContentSchema, SenderSchema}, + form_response_schemas::{FormAnswer, FormListSchema, MessageContentSchema, SenderSchema}, }, }; @@ -56,26 +56,6 @@ pub async fn create_form_handler( } } -pub async fn public_form_list_handler( - State(repository): State, - Query(offset_and_limit): Query, -) -> impl IntoResponse { - let form_use_case = FormUseCase { - form_repository: repository.form_repository(), - notification_repository: repository.notification_repository(), - }; - - // FIXME: public_form_list を実装する - todo!(); - // match form_use_case - // .public_form_list(offset_and_limit.offset, offset_and_limit.limit) - // .await - // { - // Ok(forms) => (StatusCode::OK, Json(forms)).into_response(), - // Err(err) => handle_error(err).into_response(), - // } -} - pub async fn form_list_handler( Extension(user): Extension, State(repository): State, @@ -90,7 +70,14 @@ pub async fn form_list_handler( .form_list(&user, offset_and_limit.offset, offset_and_limit.limit) .await { - Ok(forms) => (StatusCode::OK, Json(forms)).into_response(), + Ok(forms) => { + let form_list_schema = forms + .into_iter() + .map(Into::::into) + .collect_vec(); + + (StatusCode::OK, Json(form_list_schema)).into_response() + } Err(err) => handle_error(err).into_response(), } } diff --git a/server/presentation/src/schemas/form/form_response_schemas.rs b/server/presentation/src/schemas/form/form_response_schemas.rs index 6d0659cd..61766137 100644 --- a/server/presentation/src/schemas/form/form_response_schemas.rs +++ b/server/presentation/src/schemas/form/form_response_schemas.rs @@ -1,132 +1,180 @@ -use chrono::{DateTime, Utc}; -use itertools::Itertools; -use serde::Serialize; -use uuid::Uuid; - -#[derive(Serialize, Debug)] -pub(crate) enum Role { - #[serde(rename = "STANDARD_USER")] - StandardUser, - #[serde(rename = "ADMINISTRATOR")] - Administrator, -} - -impl From for Role { - fn from(val: domain::user::models::Role) -> Self { - match val { - domain::user::models::Role::StandardUser => Role::StandardUser, - domain::user::models::Role::Administrator => Role::Administrator, - } - } -} - -#[derive(Serialize, Debug)] -pub(crate) struct User { - uuid: String, - name: String, - role: Role, -} - -impl From for User { - fn from(val: domain::user::models::User) -> Self { - User { - uuid: val.id.to_string(), - name: val.name, - role: val.role.into(), - } - } -} - -#[derive(Serialize, Debug)] -pub(crate) struct AnswerContent { - question_id: i32, - answer: String, -} - -impl From for AnswerContent { - fn from(val: domain::form::models::FormAnswerContent) -> Self { - AnswerContent { - question_id: val.question_id.into(), - answer: val.answer, - } - } -} - -#[derive(Serialize, Debug)] -pub(crate) struct AnswerComment { - content: String, - timestamp: DateTime, - commented_by: User, -} - -impl From for AnswerComment { - fn from(val: domain::form::models::Comment) -> Self { - AnswerComment { - content: val.content, - timestamp: val.timestamp, - commented_by: val.commented_by.into(), - } - } -} - -#[derive(Serialize, Debug)] -pub(crate) struct AnswerLabels { - id: i32, - name: String, -} - -impl From for AnswerLabels { - fn from(val: domain::form::models::AnswerLabel) -> Self { - AnswerLabels { - id: val.id.into(), - name: val.name, - } - } -} - -#[derive(Serialize, Debug)] -pub(crate) struct FormAnswer { - id: i32, - user: User, - form_id: Uuid, - timestamp: DateTime, - title: Option, - answers: Vec, - comments: Vec, - labels: Vec, -} - -impl FormAnswer { - pub fn new( - answer: domain::form::models::FormAnswer, - answer_contents: Vec, - comments: Vec, - labels: Vec, - ) -> Self { - FormAnswer { - id: answer.id.into(), - user: answer.user.into(), - form_id: answer.form_id.into(), - timestamp: answer.timestamp, - title: answer.title, - answers: answer_contents.into_iter().map(Into::into).collect_vec(), - comments: comments.into_iter().map(Into::into).collect_vec(), - labels: labels.into_iter().map(Into::into).collect_vec(), - } - } -} - -#[derive(Serialize, Debug)] -pub struct MessageContentSchema { - pub id: Uuid, - pub body: String, - pub sender: SenderSchema, - pub timestamp: DateTime, -} - -#[derive(Serialize, Debug)] -pub struct SenderSchema { - pub uuid: String, - pub name: String, - pub role: String, -} +use chrono::{DateTime, Utc}; +use domain::form::models::FormId; +use itertools::Itertools; +use serde::Serialize; +use uuid::Uuid; + +#[derive(Serialize, Debug)] +pub(crate) struct ResponsePeriodSchema { + start_at: Option>, + end_at: Option>, +} + +#[derive(Serialize, Debug)] +pub(crate) enum AnswerVisibility { + #[serde(rename = "PUBLIC")] + Public, + #[serde(rename = "PRIVATE")] + Private, +} + +impl From for AnswerVisibility { + fn from(val: domain::form::models::Visibility) -> Self { + match val { + domain::form::models::Visibility::PUBLIC => AnswerVisibility::Public, + domain::form::models::Visibility::PRIVATE => AnswerVisibility::Private, + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) struct FormListSchema { + id: FormId, + title: String, + description: Option, + response_period: ResponsePeriodSchema, + answer_visibility: AnswerVisibility, +} + +impl From for FormListSchema { + fn from(form: domain::form::models::Form) -> Self { + FormListSchema { + id: form.id().to_owned(), + title: form.title().to_owned().into_inner(), + description: form.description().to_owned().into_inner(), + response_period: ResponsePeriodSchema { + start_at: form.settings().response_period().start_at().to_owned(), + end_at: form.settings().response_period().end_at().to_owned(), + }, + answer_visibility: form.settings().answer_visibility().to_owned().into(), + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) enum Role { + #[serde(rename = "STANDARD_USER")] + StandardUser, + #[serde(rename = "ADMINISTRATOR")] + Administrator, +} + +impl From for Role { + fn from(val: domain::user::models::Role) -> Self { + match val { + domain::user::models::Role::StandardUser => Role::StandardUser, + domain::user::models::Role::Administrator => Role::Administrator, + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) struct User { + uuid: String, + name: String, + role: Role, +} + +impl From for User { + fn from(val: domain::user::models::User) -> Self { + User { + uuid: val.id.to_string(), + name: val.name, + role: val.role.into(), + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) struct AnswerContent { + question_id: i32, + answer: String, +} + +impl From for AnswerContent { + fn from(val: domain::form::models::FormAnswerContent) -> Self { + AnswerContent { + question_id: val.question_id.into(), + answer: val.answer, + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) struct AnswerComment { + content: String, + timestamp: DateTime, + commented_by: User, +} + +impl From for AnswerComment { + fn from(val: domain::form::models::Comment) -> Self { + AnswerComment { + content: val.content, + timestamp: val.timestamp, + commented_by: val.commented_by.into(), + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) struct AnswerLabels { + id: i32, + name: String, +} + +impl From for AnswerLabels { + fn from(val: domain::form::models::AnswerLabel) -> Self { + AnswerLabels { + id: val.id.into(), + name: val.name, + } + } +} + +#[derive(Serialize, Debug)] +pub(crate) struct FormAnswer { + id: i32, + user: User, + form_id: Uuid, + timestamp: DateTime, + title: Option, + answers: Vec, + comments: Vec, + labels: Vec, +} + +impl FormAnswer { + pub fn new( + answer: domain::form::models::FormAnswer, + answer_contents: Vec, + comments: Vec, + labels: Vec, + ) -> Self { + FormAnswer { + id: answer.id.into(), + user: answer.user.into(), + form_id: answer.form_id.into(), + timestamp: answer.timestamp, + title: answer.title, + answers: answer_contents.into_iter().map(Into::into).collect_vec(), + comments: comments.into_iter().map(Into::into).collect_vec(), + labels: labels.into_iter().map(Into::into).collect_vec(), + } + } +} + +#[derive(Serialize, Debug)] +pub struct MessageContentSchema { + pub id: Uuid, + pub body: String, + pub sender: SenderSchema, + pub timestamp: DateTime, +} + +#[derive(Serialize, Debug)] +pub struct SenderSchema { + pub uuid: String, + pub name: String, + pub role: String, +} diff --git a/server/usecase/src/form.rs b/server/usecase/src/form.rs index 2ffa2b5b..6f7394e2 100644 --- a/server/usecase/src/form.rs +++ b/server/usecase/src/form.rs @@ -48,19 +48,20 @@ impl FormUseCase<'_, R1, R2> { Ok(form.id().to_owned()) } + /// `actor` が参照可能なフォームのリストを取得する pub async fn form_list( &self, actor: &User, offset: Option, limit: Option, ) -> Result, Error> { - self.form_repository + Ok(self + .form_repository .list(offset, limit) .await? .into_iter() - .map(|form| form.try_into_read(actor)) - .collect::, _>>() - .map_err(Into::into) + .flat_map(|form| form.try_into_read(actor)) + .collect::>()) } pub async fn get_form(&self, actor: &User, form_id: FormId) -> Result {