From 5986a2fbf4c1b103dc920c879745cc030102b480 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Fri, 15 Sep 2023 00:33:37 +0900 Subject: [PATCH 1/6] =?UTF-8?q?WIP:=20=E3=81=99=E3=81=B9=E3=81=A6=E3=81=AE?= =?UTF-8?q?=E5=9B=9E=E7=AD=94=E3=82=92=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B?= =?UTF-8?q?=E3=82=A8=E3=83=B3=E3=83=88=E3=83=AA=E3=83=9D=E3=82=A4=E3=83=B3?= =?UTF-8?q?=E3=83=88=E3=82=92=E7=94=9F=E3=82=84=E3=81=9D=E3=81=86=E3=81=A8?= =?UTF-8?q?=E3=81=97=E3=81=A6=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/Cargo.lock | 1 + .../domain/src/repository/form_repository.rs | 1 + server/entrypoint/src/main.rs | 6 +- server/errors/src/infra.rs | 2 + server/infra/resource/Cargo.toml | 1 + .../infra/resource/src/database/components.rs | 3 +- server/infra/resource/src/database/form.rs | 44 ++++++++++++++- server/infra/resource/src/dto.rs | 56 +++++++++++++++++++ .../src/repository/form_repository_impl.rs | 11 ++++ server/presentation/src/form_handler.rs | 14 +++++ server/usecase/src/form.rs | 4 ++ 11 files changed, 140 insertions(+), 3 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index 1594dae3..b4d02ee3 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -2530,6 +2530,7 @@ dependencies = [ "sea-orm", "serde", "tracing", + "uuid 1.4.1", ] [[package]] diff --git a/server/domain/src/repository/form_repository.rs b/server/domain/src/repository/form_repository.rs index 04d6c6ce..a8db2cc7 100644 --- a/server/domain/src/repository/form_repository.rs +++ b/server/domain/src/repository/form_repository.rs @@ -21,5 +21,6 @@ pub trait FormRepository: Send + Sync + 'static { form_update_targets: FormUpdateTargets, ) -> Result<(), Error>; async fn post_answer(&self, answers: PostedAnswers) -> Result<(), Error>; + async fn get_all_answers(&self) -> Result, Error>; async fn create_questions(&self, questions: FormQuestionUpdateSchema) -> Result<(), Error>; } diff --git a/server/entrypoint/src/main.rs b/server/entrypoint/src/main.rs index 8fef81f1..8ef85688 100644 --- a/server/entrypoint/src/main.rs +++ b/server/entrypoint/src/main.rs @@ -8,6 +8,7 @@ use axum::{ }; use common::config::{ENV, HTTP}; use hyper::header::AUTHORIZATION; +use presentation::form_handler::get_all_answers; use presentation::{ auth::auth, form_handler::{ @@ -71,7 +72,10 @@ async fn main() -> anyhow::Result<()> { .patch(update_form_handler), ) .with_state(shared_repository.to_owned()) - .route("/forms/answers", post(post_answer_handler)) + .route( + "/forms/answers", + post(post_answer_handler).get(get_all_answers), + ) .with_state(shared_repository.to_owned()) .route("/forms/questions", post(create_question_handler)) .with_state(shared_repository.to_owned()) diff --git a/server/errors/src/infra.rs b/server/errors/src/infra.rs index 9bb536b9..d6f5625d 100644 --- a/server/errors/src/infra.rs +++ b/server/errors/src/infra.rs @@ -7,6 +7,8 @@ pub enum InfraError { #[from] source: sea_orm::error::DbErr, }, + #[error("Uuid Parse Error: {}", .cause)] + UuidParse { cause: String }, #[error("Form Not Found: id = {}", .id)] FormNotFound { id: i32 }, #[error("Outgoing Error: {}", .cause)] diff --git a/server/infra/resource/Cargo.toml b/server/infra/resource/Cargo.toml index 12c1a6d3..d2e51aa9 100644 --- a/server/infra/resource/Cargo.toml +++ b/server/infra/resource/Cargo.toml @@ -22,3 +22,4 @@ serde = { workspace = true } tracing = { workspace = true } num-traits = { workspace = true } regex = { workspace = true } +uuid = { version = "1.4.1", features = ["v4"] } diff --git a/server/infra/resource/src/database/components.rs b/server/infra/resource/src/database/components.rs index db92540c..443e4b97 100644 --- a/server/infra/resource/src/database/components.rs +++ b/server/infra/resource/src/database/components.rs @@ -6,7 +6,7 @@ use domain::form::models::{ use errors::infra::InfraError; use mockall::automock; -use crate::dto::FormDto; +use crate::dto::{FormDto, PostedAnswersDto}; #[async_trait] pub trait DatabaseComponents: Send + Sync { @@ -34,6 +34,7 @@ pub trait FormDatabase: Send + Sync { form_update_targets: FormUpdateTargets, ) -> Result<(), InfraError>; async fn post_answer(&self, answer: PostedAnswers) -> Result<(), InfraError>; + async fn get_all_answers(&self) -> Result, InfraError>; async fn create_questions(&self, questions: FormQuestionUpdateSchema) -> Result<(), InfraError>; } diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 562453a0..61ed67e9 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -1,9 +1,10 @@ use async_trait::async_trait; use chrono::Utc; use domain::form::models::{ - DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, + Answer, DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, OffsetAndLimit, PostedAnswers, }; +use entities::prelude::Answers; use entities::{ answers, default_answer_titles, form_choices, form_meta_data, form_questions, form_webhooks, prelude::{ @@ -17,13 +18,16 @@ use futures::{stream, stream::StreamExt}; use itertools::Itertools; use num_traits::cast::FromPrimitive; use regex::Regex; +use sea_orm::prelude::Uuid; use sea_orm::{ sea_query::{Expr, SimpleExpr}, ActiveEnum, ActiveModelTrait, ActiveValue, ActiveValue::Set, ColumnTrait, EntityTrait, ModelTrait, QueryFilter, QueryOrder, QuerySelect, }; +use std::fmt::Error; +use crate::dto::{AnswerDto, PostedAnswersDto}; use crate::{ database::{components::FormDatabase, connection::ConnectionPool}, dto::{FormDto, QuestionDto}, @@ -431,6 +435,44 @@ impl FormDatabase for ConnectionPool { Ok(()) } + async fn get_all_answers(&self) -> Result, InfraError> { + stream::iter( + Answers::find() + .order_by_desc(answers::Column::TimeStamp) + .all(&self.pool) + .await?, + ) + .then(|answer| async { + let answers = RealAnswers::find() + .filter(Expr::col(real_answers::Column::AnswerId).eq(answer.id)) + .all(&self.pool) + .await? + .iter() + .map(|answer| AnswerDto { + question_id: answer.question_id, + answer: answer.to_owned().answer, + }) + .collect_vec(); + + Ok(PostedAnswersDto { + uuid: answer + .user + .chunks_exact(16) + .map(Uuid::from_slice) + .collect::>() + .unwrap(), + timestamp: answer.time_stamp, + form_id: 1, // TODO: answersテーブルにはform_idが存在しないので、追加して取得できるようにしたい + title: Some(answer.title), + answers, + }) + }) + .collect::>>() + .await + .into_iter() + .collect::, _>>() + } + async fn create_questions( &self, form_question_update_schema: FormQuestionUpdateSchema, diff --git a/server/infra/resource/src/dto.rs b/server/infra/resource/src/dto.rs index f441303e..ca942ba6 100644 --- a/server/infra/resource/src/dto.rs +++ b/server/infra/resource/src/dto.rs @@ -1,5 +1,7 @@ use chrono::{DateTime, Utc}; use domain::form::models::{FormSettings, ResponsePeriod}; +use itertools::Itertools; +use uuid::Uuid; pub struct QuestionDto { pub id: i32, @@ -79,3 +81,57 @@ impl TryFrom for domain::form::models::Form { .build()) } } + +pub struct AnswerDto { + pub question_id: i32, + pub answer: String, +} + +impl TryFrom for domain::form::models::Answer { + type Error = errors::domain::DomainError; + + fn try_from( + AnswerDto { + question_id, + answer, + }: AnswerDto, + ) -> Result { + Ok(domain::form::models::Answer { + question_id: question_id.into(), + answer, + }) + } +} + +pub struct PostedAnswersDto { + pub uuid: Uuid, + pub timestamp: DateTime, + pub form_id: i32, + pub title: Option, + pub answers: Vec, +} + +impl TryFrom for domain::form::models::PostedAnswers { + type Error = errors::domain::DomainError; + + fn try_from( + PostedAnswersDto { + uuid, + timestamp, + form_id, + title, + answers, + }: PostedAnswersDto, + ) -> Result { + Ok(domain::form::models::PostedAnswers { + uuid, + timestamp, + form_id: form_id.into(), + title: title.into(), + answers: answers + .into_iter() + .map(|answer| answer.try_into()) + .collect::, _>>()?, + }) + } +} diff --git a/server/infra/resource/src/repository/form_repository_impl.rs b/server/infra/resource/src/repository/form_repository_impl.rs index 9af10c10..8f5aa1cb 100644 --- a/server/infra/resource/src/repository/form_repository_impl.rs +++ b/server/infra/resource/src/repository/form_repository_impl.rs @@ -7,6 +7,7 @@ use domain::{ repository::form_repository::FormRepository, }; use errors::Error; +use futures::{stream, stream::StreamExt}; use outgoing::form_outgoing; use crate::{ @@ -77,6 +78,16 @@ impl FormRepository for Repository .map_err(Into::into) } + #[tracing::instrument(skip(self))] + async fn get_all_answers(&self) -> Result, Error> { + stream::iter(self.client.form().get_all_answers().await?) + .then(|posted_answers_dto| async { Ok(posted_answers_dto.try_into()?) }) + .collect::>>() + .await + .into_iter() + .collect::, _>>() + } + async fn create_questions(&self, questions: FormQuestionUpdateSchema) -> Result<(), Error> { self.client .form() diff --git a/server/presentation/src/form_handler.rs b/server/presentation/src/form_handler.rs index 17fce1fc..55e18b45 100644 --- a/server/presentation/src/form_handler.rs +++ b/server/presentation/src/form_handler.rs @@ -13,6 +13,7 @@ use domain::{ use errors::{infra::InfraError, Error}; use resource::repository::RealInfrastructureRepository; use serde_json::json; +use std::os::unix::raw::mode_t; use usecase::form::FormUseCase; pub async fn create_form_handler( @@ -110,6 +111,19 @@ pub async fn update_form_handler( } } +pub async fn get_all_answers( + State(repository): State, +) -> impl IntoResponse { + let form_use_case = FormUseCase { + repository: repository.form_repository(), + }; + + match form_use_case.get_all_answers().await { + Ok(answers) => (StatusCode::OK, Json(answers)).into_response(), + Err(err) => handle_error(err).into_response(), + } +} + pub async fn post_answer_handler( State(repository): State, Json(answers): Json, diff --git a/server/usecase/src/form.rs b/server/usecase/src/form.rs index 78169c04..bd03d29b 100644 --- a/server/usecase/src/form.rs +++ b/server/usecase/src/form.rs @@ -44,6 +44,10 @@ impl FormUseCase<'_, R> { self.repository.post_answer(answers).await } + pub async fn get_all_answers(&self) -> Result, Error> { + self.repository.get_all_answers().await + } + pub async fn create_questions(&self, questions: FormQuestionUpdateSchema) -> Result<(), Error> { self.repository.create_questions(questions).await } From 37433fdda9ab957be05f50078109065bf9f6171f Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 16 Sep 2023 21:42:06 +0900 Subject: [PATCH 2/6] =?UTF-8?q?=E3=83=95=E3=82=A9=E3=83=BC=E3=83=A0?= =?UTF-8?q?=E3=81=AE=E5=9B=9E=E7=AD=94=E3=82=92=E3=81=99=E3=81=B9=E3=81=A6?= =?UTF-8?q?=E5=8F=96=E5=BE=97=E3=81=99=E3=82=8B=E3=82=A8=E3=83=B3=E3=83=88?= =?UTF-8?q?=E3=83=AA=E3=83=9D=E3=82=A4=E3=83=B3=E3=83=88=E3=81=AE=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/Cargo.lock | 1 + server/Cargo.toml | 2 +- server/entrypoint/src/main.rs | 3 +-- server/errors/Cargo.toml | 1 + server/errors/src/infra.rs | 7 +++++-- server/infra/resource/Cargo.toml | 2 +- server/infra/resource/src/database/form.rs | 21 +++++++-------------- server/infra/resource/src/dto.rs | 1 - server/presentation/src/form_handler.rs | 1 - 9 files changed, 17 insertions(+), 22 deletions(-) diff --git a/server/Cargo.lock b/server/Cargo.lock index b4d02ee3..eb9591e3 100644 --- a/server/Cargo.lock +++ b/server/Cargo.lock @@ -993,6 +993,7 @@ dependencies = [ "sea-orm", "strum", "thiserror", + "uuid 1.4.1", ] [[package]] diff --git a/server/Cargo.toml b/server/Cargo.toml index 32743bb0..f810691a 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -36,7 +36,7 @@ serde_json = "1.0.107" itertools = "0.11.0" chrono = { version = "0.4.31" } futures = "0.3.28" -uuid = "1.4.1" +uuid = { version = "1.4.1", features = ["v4"] } deriving_via = "1.5.0" reqwest = { version = "0.11.20", default-features = false, features = ["rustls"] } num-traits = "0.2.16" diff --git a/server/entrypoint/src/main.rs b/server/entrypoint/src/main.rs index 8ef85688..730dde24 100644 --- a/server/entrypoint/src/main.rs +++ b/server/entrypoint/src/main.rs @@ -8,12 +8,11 @@ use axum::{ }; use common::config::{ENV, HTTP}; use hyper::header::AUTHORIZATION; -use presentation::form_handler::get_all_answers; use presentation::{ auth::auth, form_handler::{ create_form_handler, create_question_handler, delete_form_handler, form_list_handler, - get_form_handler, post_answer_handler, update_form_handler, + get_all_answers, get_form_handler, post_answer_handler, update_form_handler, }, health_check_handler::health_check, }; diff --git a/server/errors/Cargo.toml b/server/errors/Cargo.toml index 4e3b9097..b0ba8a25 100644 --- a/server/errors/Cargo.toml +++ b/server/errors/Cargo.toml @@ -9,3 +9,4 @@ edition = "2021" sea-orm = { workspace = true } strum = { workspace = true } thiserror = "1.0.48" +uuid = { workspace = true } diff --git a/server/errors/src/infra.rs b/server/errors/src/infra.rs index d6f5625d..a6219e4b 100644 --- a/server/errors/src/infra.rs +++ b/server/errors/src/infra.rs @@ -7,8 +7,11 @@ pub enum InfraError { #[from] source: sea_orm::error::DbErr, }, - #[error("Uuid Parse Error: {}", .cause)] - UuidParse { cause: String }, + #[error("Uuid Parse Error: {}", .source)] + UuidParse { + #[from] + source: uuid::Error, + }, #[error("Form Not Found: id = {}", .id)] FormNotFound { id: i32 }, #[error("Outgoing Error: {}", .cause)] diff --git a/server/infra/resource/Cargo.toml b/server/infra/resource/Cargo.toml index d2e51aa9..547366fb 100644 --- a/server/infra/resource/Cargo.toml +++ b/server/infra/resource/Cargo.toml @@ -22,4 +22,4 @@ serde = { workspace = true } tracing = { workspace = true } num-traits = { workspace = true } regex = { workspace = true } -uuid = { version = "1.4.1", features = ["v4"] } +uuid = { workspace = true } diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 61ed67e9..8060e411 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -1,14 +1,14 @@ use async_trait::async_trait; use chrono::Utc; use domain::form::models::{ - Answer, DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, + DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, OffsetAndLimit, PostedAnswers, }; -use entities::prelude::Answers; use entities::{ answers, default_answer_titles, form_choices, form_meta_data, form_questions, form_webhooks, prelude::{ - DefaultAnswerTitles, FormChoices, FormMetaData, FormQuestions, FormWebhooks, RealAnswers, + Answers, DefaultAnswerTitles, FormChoices, FormMetaData, FormQuestions, FormWebhooks, + RealAnswers, }, real_answers, response_period, sea_orm_active_enums::QuestionType, @@ -18,19 +18,17 @@ use futures::{stream, stream::StreamExt}; use itertools::Itertools; use num_traits::cast::FromPrimitive; use regex::Regex; -use sea_orm::prelude::Uuid; use sea_orm::{ + prelude::Uuid, sea_query::{Expr, SimpleExpr}, ActiveEnum, ActiveModelTrait, ActiveValue, ActiveValue::Set, ColumnTrait, EntityTrait, ModelTrait, QueryFilter, QueryOrder, QuerySelect, }; -use std::fmt::Error; -use crate::dto::{AnswerDto, PostedAnswersDto}; use crate::{ database::{components::FormDatabase, connection::ConnectionPool}, - dto::{FormDto, QuestionDto}, + dto::{AnswerDto, FormDto, PostedAnswersDto, QuestionDto}, }; #[async_trait] @@ -442,7 +440,7 @@ impl FormDatabase for ConnectionPool { .all(&self.pool) .await?, ) - .then(|answer| async { + .then(|answer| async move { let answers = RealAnswers::find() .filter(Expr::col(real_answers::Column::AnswerId).eq(answer.id)) .all(&self.pool) @@ -455,12 +453,7 @@ impl FormDatabase for ConnectionPool { .collect_vec(); Ok(PostedAnswersDto { - uuid: answer - .user - .chunks_exact(16) - .map(Uuid::from_slice) - .collect::>() - .unwrap(), + uuid: Uuid::from_slice(answer.user.as_slice())?, timestamp: answer.time_stamp, form_id: 1, // TODO: answersテーブルにはform_idが存在しないので、追加して取得できるようにしたい title: Some(answer.title), diff --git a/server/infra/resource/src/dto.rs b/server/infra/resource/src/dto.rs index ca942ba6..3c89ffcd 100644 --- a/server/infra/resource/src/dto.rs +++ b/server/infra/resource/src/dto.rs @@ -1,6 +1,5 @@ use chrono::{DateTime, Utc}; use domain::form::models::{FormSettings, ResponsePeriod}; -use itertools::Itertools; use uuid::Uuid; pub struct QuestionDto { diff --git a/server/presentation/src/form_handler.rs b/server/presentation/src/form_handler.rs index 55e18b45..f058db93 100644 --- a/server/presentation/src/form_handler.rs +++ b/server/presentation/src/form_handler.rs @@ -13,7 +13,6 @@ use domain::{ use errors::{infra::InfraError, Error}; use resource::repository::RealInfrastructureRepository; use serde_json::json; -use std::os::unix::raw::mode_t; use usecase::form::FormUseCase; pub async fn create_form_handler( From b5cd346f119b16a78b33e42777b2efbcbb763d25 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:14:20 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20answers=E3=83=86=E3=83=BC=E3=83=96?= =?UTF-8?q?=E3=83=AB=E3=81=ABform=5Fid=E3=82=AB=E3=83=A9=E3=83=A0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/m20230811_062425_create_answer_tables.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server/migration/src/m20230811_062425_create_answer_tables.rs b/server/migration/src/m20230811_062425_create_answer_tables.rs index 05eca048..49428caf 100644 --- a/server/migration/src/m20230811_062425_create_answer_tables.rs +++ b/server/migration/src/m20230811_062425_create_answer_tables.rs @@ -1,3 +1,4 @@ +use crate::m20220101_000001_create_table::FormMetaDataTable; use sea_orm_migration::prelude::*; use crate::m20221211_211233_form_questions::FormQuestionsTable; @@ -20,6 +21,13 @@ impl MigrationTrait for Migration { .auto_increment() .primary_key(), ) + .col(ColumnDef::new(AnswersTable::FormId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("fk-form-id-from-answers") + .from(AnswersTable::Answers, AnswersTable::FormId) + .to(FormMetaDataTable::FormMetaData, FormMetaDataTable::Id), + ) .col(ColumnDef::new(AnswersTable::User).uuid().not_null()) .col( ColumnDef::new(AnswersTable::Title) @@ -98,6 +106,7 @@ impl MigrationTrait for Migration { enum AnswersTable { Answers, Id, + FormId, User, Title, TimeStamp, From 88d361b4edea975ea5426f90a0ccc36e7b4968f9 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:19:48 +0900 Subject: [PATCH 4/6] =?UTF-8?q?chore:=20=E3=82=A8=E3=83=B3=E3=83=86?= =?UTF-8?q?=E3=82=A3=E3=83=86=E3=82=A3=E3=82=92=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/infra/entities/src/answers.rs | 15 +++++++++++++++ server/infra/entities/src/form_meta_data.rs | 8 ++++++++ server/infra/resource/src/database/form.rs | 3 ++- .../src/m20230811_062425_create_answer_tables.rs | 6 ++++-- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/server/infra/entities/src/answers.rs b/server/infra/entities/src/answers.rs index 9c279c1e..f5f12c37 100644 --- a/server/infra/entities/src/answers.rs +++ b/server/infra/entities/src/answers.rs @@ -7,6 +7,7 @@ use sea_orm::entity::prelude::*; pub struct Model { #[sea_orm(primary_key)] pub id: i32, + pub form_id: i32, #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(16)))")] pub user: Vec, pub title: String, @@ -15,10 +16,24 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { + #[sea_orm( + belongs_to = "super::form_meta_data::Entity", + from = "Column::FormId", + to = "super::form_meta_data::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + FormMetaData, #[sea_orm(has_many = "super::real_answers::Entity")] RealAnswers, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::FormMetaData.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::RealAnswers.def() diff --git a/server/infra/entities/src/form_meta_data.rs b/server/infra/entities/src/form_meta_data.rs index 90bc5524..c9d7157f 100644 --- a/server/infra/entities/src/form_meta_data.rs +++ b/server/infra/entities/src/form_meta_data.rs @@ -15,6 +15,8 @@ pub struct Model { #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation { + #[sea_orm(has_many = "super::answers::Entity")] + Answers, #[sea_orm(has_many = "super::default_answer_titles::Entity")] DefaultAnswerTitles, #[sea_orm(has_many = "super::form_questions::Entity")] @@ -25,6 +27,12 @@ pub enum Relation { ResponsePeriod, } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Answers.def() + } +} + impl Related for Entity { fn to() -> RelationDef { Relation::DefaultAnswerTitles.def() diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 8060e411..86834d22 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -407,6 +407,7 @@ impl FormDatabase for ConnectionPool { let id = answers::ActiveModel { id: Default::default(), + form_id: Set(answer.form_id.to_owned()), user: Set(answer.uuid.to_owned().as_ref().to_vec()), title: Set(embed_title), time_stamp: Set(Utc::now()), @@ -455,7 +456,7 @@ impl FormDatabase for ConnectionPool { Ok(PostedAnswersDto { uuid: Uuid::from_slice(answer.user.as_slice())?, timestamp: answer.time_stamp, - form_id: 1, // TODO: answersテーブルにはform_idが存在しないので、追加して取得できるようにしたい + form_id: answer.form_id, title: Some(answer.title), answers, }) diff --git a/server/migration/src/m20230811_062425_create_answer_tables.rs b/server/migration/src/m20230811_062425_create_answer_tables.rs index 49428caf..e74c548c 100644 --- a/server/migration/src/m20230811_062425_create_answer_tables.rs +++ b/server/migration/src/m20230811_062425_create_answer_tables.rs @@ -1,7 +1,9 @@ -use crate::m20220101_000001_create_table::FormMetaDataTable; use sea_orm_migration::prelude::*; -use crate::m20221211_211233_form_questions::FormQuestionsTable; +use crate::{ + m20220101_000001_create_table::FormMetaDataTable, + m20221211_211233_form_questions::FormQuestionsTable, +}; #[derive(DeriveMigrationName)] pub struct Migration; From a3cfdf8755bd10b418a5202cde8faed313a4fed0 Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:48:13 +0900 Subject: [PATCH 5/6] =?UTF-8?q?refactor:=20answer=E3=82=92move=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=81=A7AnswerDto=E3=82=92=E6=A7=8B=E7=AF=89?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Mitama --- server/infra/resource/src/database/form.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 86834d22..e62ff776 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -446,10 +446,14 @@ impl FormDatabase for ConnectionPool { .filter(Expr::col(real_answers::Column::AnswerId).eq(answer.id)) .all(&self.pool) .await? - .iter() - .map(|answer| AnswerDto { - question_id: answer.question_id, - answer: answer.to_owned().answer, + .into_iter() + .map(|entities::real_answers::Model { + question_id, + answer, + .. + }| AnswerDto { + question_id, + answer, }) .collect_vec(); From 643a3a3fc2b9d0f47205741fa0b79941103d478d Mon Sep 17 00:00:00 2001 From: rito528 <39003544+rito528@users.noreply.github.com> Date: Sat, 16 Sep 2023 22:49:35 +0900 Subject: [PATCH 6/6] style: apply pretty --- server/infra/resource/src/database/form.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index e62ff776..fbbf44fb 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -447,14 +447,16 @@ impl FormDatabase for ConnectionPool { .all(&self.pool) .await? .into_iter() - .map(|entities::real_answers::Model { - question_id, - answer, - .. - }| AnswerDto { - question_id, - answer, - }) + .map( + |entities::real_answers::Model { + question_id, + answer, + .. + }| AnswerDto { + question_id, + answer, + }, + ) .collect_vec(); Ok(PostedAnswersDto {