Skip to content

Commit

Permalink
feat: todo部分を実装する
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Oct 11, 2023
1 parent f506d1a commit 1fe2392
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 61 deletions.
3 changes: 2 additions & 1 deletion server/infra/entities/src/form_questions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
use super::sea_orm_active_enums::QuestionType;
use sea_orm::entity::prelude::*;

use super::sea_orm_active_enums::QuestionType;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "form_questions")]
pub struct Model {
Expand Down
16 changes: 7 additions & 9 deletions server/infra/entities/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1
pub use super::answers::Entity as Answers;
pub use super::default_answer_titles::Entity as DefaultAnswerTitles;
pub use super::form_choices::Entity as FormChoices;
pub use super::form_meta_data::Entity as FormMetaData;
pub use super::form_questions::Entity as FormQuestions;
pub use super::form_webhooks::Entity as FormWebhooks;
pub use super::real_answers::Entity as RealAnswers;
pub use super::response_period::Entity as ResponsePeriod;
pub use super::users::Entity as Users;
pub use super::{
answers::Entity as Answers, default_answer_titles::Entity as DefaultAnswerTitles,
form_choices::Entity as FormChoices, form_meta_data::Entity as FormMetaData,
form_questions::Entity as FormQuestions, form_webhooks::Entity as FormWebhooks,
real_answers::Entity as RealAnswers, response_period::Entity as ResponsePeriod,
users::Entity as Users,
};
106 changes: 59 additions & 47 deletions server/infra/resource/src/database/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use domain::form::models::{
FormUpdateTargets, OffsetAndLimit, PostedAnswers,
};
use entities::{
answers, default_answer_titles, form_choices, form_meta_data, form_questions, form_webhooks,
prelude::{
Answers, DefaultAnswerTitles, FormChoices, FormMetaData, FormQuestions, FormWebhooks,
RealAnswers,
},
real_answers, response_period,
default_answer_titles, form_choices, form_meta_data, form_questions, form_webhooks,
prelude::{DefaultAnswerTitles, FormChoices, FormMetaData, FormQuestions, FormWebhooks},
response_period,
sea_orm_active_enums::QuestionType,
};
use errors::infra::{InfraError, InfraError::FormNotFound};
Expand All @@ -21,7 +18,7 @@ use sea_orm::{
sea_query::{Expr, SimpleExpr},
ActiveEnum, ActiveModelTrait, ActiveValue,
ActiveValue::Set,
ColumnTrait, ConnectionTrait, DatabaseBackend, EntityTrait, ModelTrait, QueryFilter,
ColumnTrait, ConnectionTrait, DatabaseBackend, DbErr, EntityTrait, ModelTrait, QueryFilter,
QueryOrder, QuerySelect, Statement,
};

Expand Down Expand Up @@ -406,11 +403,18 @@ impl FormDatabase for ConnectionPool {
},
);

let id = self.pool.execute(Statement::from_sql_and_values(
DatabaseBackend::MySql,
"INSERT INTO answers (form_id, user, title) VALUES (?, (SELECT id FROM users WHERE uuid = UUID_TO_BIN(?)), ?)",
[answer.form_id.to_owned().into(), answer.uuid.to_string().into(), embed_title.into()])
)
let id = self
.pool
.execute(Statement::from_sql_and_values(
DatabaseBackend::MySql,
"INSERT INTO answers (form_id, user, title) VALUES (?, (SELECT id FROM users \
WHERE uuid = UUID_TO_BIN(?)), ?)",
[
answer.form_id.to_owned().into(),
answer.uuid.to_string().into(),
embed_title.into(),
],
))
.await?
.last_insert_id();

Expand Down Expand Up @@ -445,43 +449,51 @@ impl FormDatabase for ConnectionPool {
}

async fn get_all_answers(&self) -> Result<Vec<PostedAnswersDto>, InfraError> {
stream::iter(
Answers::find()
.order_by_desc(answers::Column::TimeStamp)
.all(&self.pool)
.await?,
)
.then(|answer| async move {
let answers = RealAnswers::find()
.filter(Expr::col(real_answers::Column::AnswerId).eq(answer.id))
.all(&self.pool)
.await?
.into_iter()
.map(
|real_answers::Model {
question_id,
answer,
..
}| AnswerDto {
question_id,
answer,
},
)
.collect_vec();
let answers = self
.pool
.query_all(Statement::from_string(
DatabaseBackend::MySql,
"SELECT form_id, answers.id AS answer_id, title, uuid, time_stamp FROM answers
INNER JOIN users ON answers.user = users.id
ORDER BY answers.time_stamp",
))
.await?;

Ok(PostedAnswersDto {
// uuid: Uuid::from_slice(answer.user.as_slice())?,
uuid: todo!(),
timestamp: answer.time_stamp,
form_id: answer.form_id,
title: Some(answer.title),
answers,
let real_answers = self
.pool
.query_all(Statement::from_string(
DatabaseBackend::MySql,
"SELECT answer_id, question_id, answer FROM real_answers",
))
.await?;

answers
.iter()
.map(|rs| {
let answer_id: i32 = rs.try_get("", "answer_id")?;
let answers = real_answers
.iter()
.filter(|rs| {
rs.try_get::<i32>("", "answer_id")
.is_ok_and(|id| id == answer_id)
})
.map(|rs| {
Ok::<AnswerDto, DbErr>(AnswerDto {
question_id: rs.try_get("", "question_id")?,
answer: rs.try_get("", "answer")?,
})
})
.collect::<Result<Vec<_>, _>>()?;

Ok(PostedAnswersDto {
uuid: rs.try_get("", "uuid")?,
timestamp: rs.try_get("", "time_stamp")?,
form_id: rs.try_get("", "form_id")?,
title: rs.try_get("", "title")?,
answers,
})
})
})
.collect::<Vec<Result<PostedAnswersDto, _>>>()
.await
.into_iter()
.collect::<Result<Vec<PostedAnswersDto>, _>>()
.collect::<Result<Vec<_>, _>>()
}

async fn create_questions(
Expand Down
3 changes: 2 additions & 1 deletion server/migration/src/m20220101_000001_create_table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::m20231008_135425_create_user_table::UsersTable;
use sea_orm_migration::prelude::*;

use crate::m20231008_135425_create_user_table::UsersTable;

#[derive(DeriveMigrationName)]
pub struct Migration;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use sea_orm_migration::prelude::*;

use crate::m20231008_135425_create_user_table::UsersTable;
use crate::{
m20220101_000001_create_table::FormMetaDataTable,
m20221211_211233_form_questions::FormQuestionsTable,
m20231008_135425_create_user_table::UsersTable,
};

#[derive(DeriveMigrationName)]
Expand Down
3 changes: 1 addition & 2 deletions server/presentation/src/auth.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use axum::http::HeaderValue;
use axum::{
extract::TypedHeader,
headers::authorization::{Authorization, Bearer},
http::{Request, StatusCode},
http::{HeaderValue, Request, StatusCode},
middleware::Next,
response::Response,
};
Expand Down

0 comments on commit 1fe2392

Please sign in to comment.