fix update api
This commit is contained in:
parent
20dae82345
commit
6d5333b047
17
src/api.rs
17
src/api.rs
@ -50,14 +50,12 @@ pub async fn read_questions(
|
||||
let questions_option = store.read().await.fetch_many_questions(start, size).await;
|
||||
match questions_option {
|
||||
Some(questions) => {
|
||||
println!("Num question {}", questions.len());
|
||||
let mut response_vec_dto: Vec<QuestionDTO> = vec![];
|
||||
for question in questions {
|
||||
//Not ideal - hitting the database serially for the tags of each invidual question. Can be optimized with more complex sql
|
||||
let tags_option = store.read().await.get_tags_for_question(question.id).await;
|
||||
match tags_option {
|
||||
Some(tags) => {
|
||||
println!("tags {:?}", tags);
|
||||
let question_dto: QuestionDTO = QuestionDTO::new(question, tags);
|
||||
response_vec_dto.push(question_dto)
|
||||
}
|
||||
@ -127,12 +125,15 @@ pub async fn update_question(
|
||||
.await
|
||||
.unwrap();
|
||||
// 4: Unassociated all current tags with this question
|
||||
let _remove_tags = store
|
||||
.write()
|
||||
.await
|
||||
.unassociate_tags(updated_question.id, current_tags)
|
||||
.await
|
||||
.unwrap();
|
||||
if !current_tags.is_empty() {
|
||||
let _remove_tags = store
|
||||
.write()
|
||||
.await
|
||||
.unassociate_tags(updated_question.id, current_tags)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// 5: Associated all of the incoming tags (now newly created if needed) with the question
|
||||
let _updated_tags = store
|
||||
.write()
|
||||
|
@ -156,7 +156,7 @@ impl Store {
|
||||
INSERT INTO question_tag (question_id, tag_id)
|
||||
SELECT $1::smallint, UNNEST($2::smallint[])
|
||||
ON CONFLICT DO NOTHING;
|
||||
";
|
||||
";
|
||||
let result = sqlx::query(query)
|
||||
.bind(question_id.to_string())
|
||||
.bind(tag_ids_string)
|
||||
@ -164,7 +164,6 @@ impl Store {
|
||||
.await;
|
||||
match result {
|
||||
Ok(pg_rows) => {
|
||||
println!("Num rows {}", pg_rows.len());
|
||||
let question_tags: Vec<QuestionTag> = pg_rows
|
||||
.iter()
|
||||
.map(|pg_row| {
|
||||
@ -188,17 +187,17 @@ impl Store {
|
||||
question_id: u8,
|
||||
tags: Vec<Tag>,
|
||||
) -> Result<bool, String> {
|
||||
let question_id_tag_id_tuple_string = tags
|
||||
let list = tags
|
||||
.iter()
|
||||
.map(|tag| format!("({},{})", question_id, tag.id))
|
||||
.collect::<Vec<String>>()
|
||||
.join(",");
|
||||
let query = "DELETE FROM question_tag WHERE (question_id, tag_id) IN ($1)";
|
||||
match sqlx::query(query)
|
||||
.bind(question_id_tag_id_tuple_string)
|
||||
.execute(&self.connection)
|
||||
.await
|
||||
{
|
||||
|
||||
let query = format!(
|
||||
"DELETE FROM question_tag WHERE (question_id, tag_id) IN ({})",
|
||||
list
|
||||
); // Not bulletproof to injection (still ok), but best we can do with sqlx aside from sequential hits
|
||||
match sqlx::query(&query).execute(&self.connection).await {
|
||||
Ok(_) => Ok(true),
|
||||
Err(e) => Err(e.to_string()),
|
||||
}
|
||||
@ -336,7 +335,6 @@ impl Store {
|
||||
|
||||
match rows_result {
|
||||
Ok(pg_rows) => {
|
||||
println!("num rows {}", pg_rows.len());
|
||||
let mut result: Vec<Question> = vec![];
|
||||
for pg_row in pg_rows {
|
||||
result.push(Question::new(
|
||||
@ -375,9 +373,15 @@ impl Store {
|
||||
title: String,
|
||||
content: String,
|
||||
) -> Result<Question, String> {
|
||||
let result = sqlx::query("UPDATE questions SET title = $1 AND SET content = $2 WHERE id = $3 RETURNING id, title, content")
|
||||
.bind(title).bind(content).bind(id.to_string())
|
||||
.fetch_one(&self.connection).await;
|
||||
let query = "UPDATE questions
|
||||
SET title = $1, content = $2
|
||||
WHERE id = $3 RETURNING id, title, content";
|
||||
let result = sqlx::query(query)
|
||||
.bind(title)
|
||||
.bind(content)
|
||||
.bind(i32::from(id))
|
||||
.fetch_one(&self.connection)
|
||||
.await;
|
||||
match result {
|
||||
Ok(pg_row) => Ok(Question::new(
|
||||
Store::id_to_u8(&pg_row, "id"),
|
||||
|
Reference in New Issue
Block a user