Skip to content

Commit bf79dd9

Browse files
author
Bastian Gruber
committed
Updated
1 parent 3a267df commit bf79dd9

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

ch_04/final/src/main.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use parking_lot::RwLock;
2-
use std::{sync::Arc, collections::HashMap};
32
use serde::{Deserialize, Serialize};
3+
use std::{collections::HashMap, sync::Arc};
44
use warp::{
5-
http::Method,
6-
Filter,
75
filters::{body::BodyDeserializeError, cors::CorsForbidden},
6+
http::Method,
87
http::StatusCode,
98
reject::Reject,
10-
Rejection, Reply,
9+
Filter, Rejection, Reply,
1110
};
1211

1312
#[derive(Deserialize, Serialize, Debug, Clone)]
@@ -20,12 +19,14 @@ struct Question {
2019
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
2120
struct QuestionId(String);
2221

22+
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
23+
struct AnswerId(String);
2324

2425
#[derive(Serialize, Deserialize, Debug, Clone)]
2526
struct Answer {
26-
id: String,
27+
id: AnswerId,
2728
content: String,
28-
question_id: String,
29+
question_id: QuestionId,
2930
}
3031

3132
#[derive(Debug)]
@@ -34,11 +35,10 @@ struct Pagination {
3435
end: usize,
3536
}
3637

37-
3838
#[derive(Clone)]
3939
struct Store {
4040
questions: Arc<RwLock<HashMap<QuestionId, Question>>>,
41-
answers: Arc<RwLock<HashMap<String, Answer>>>,
41+
answers: Arc<RwLock<HashMap<AnswerId, Answer>>>,
4242
}
4343

4444
impl Store {
@@ -75,7 +75,6 @@ impl std::fmt::Display for Error {
7575
impl Reject for Error {}
7676

7777
async fn return_error(r: Rejection) -> Result<impl Reply, Rejection> {
78-
println!("{:?}", r);
7978
if let Some(error) = r.find::<Error>() {
8079
Ok(warp::reply::with_status(
8180
error.to_string(),
@@ -99,7 +98,6 @@ async fn return_error(r: Rejection) -> Result<impl Reply, Rejection> {
9998
}
10099
}
101100

102-
103101
fn extract_pagination(params: HashMap<String, String>) -> Result<Pagination, Error> {
104102
if params.contains_key("start") && params.contains_key("end") {
105103
return Ok(Pagination {
@@ -141,7 +139,7 @@ async fn add_question(
141139
store
142140
.questions
143141
.write()
144-
.insert(question.clone().id, question);
142+
.insert(question.id.clone(), question);
145143

146144
Ok(warp::reply::with_status("Question added", StatusCode::OK))
147145
}
@@ -159,10 +157,7 @@ async fn update_question(
159157
Ok(warp::reply::with_status("Question updated", StatusCode::OK))
160158
}
161159

162-
async fn delete_question(
163-
id: String,
164-
store: Store,
165-
) -> Result<impl warp::Reply, warp::Rejection> {
160+
async fn delete_question(id: String, store: Store) -> Result<impl warp::Reply, warp::Rejection> {
166161
match store.questions.write().remove(&QuestionId(id)) {
167162
Some(_) => return Ok(warp::reply::with_status("Question deleted", StatusCode::OK)),
168163
None => return Err(warp::reject::custom(Error::QuestionNotFound)),
@@ -174,12 +169,12 @@ async fn add_answer(
174169
params: HashMap<String, String>,
175170
) -> Result<impl warp::Reply, warp::Rejection> {
176171
let answer = Answer {
177-
id: "CI001".to_string(),
172+
id: AnswerId("1".to_string()),
178173
content: params.get("content").unwrap().to_string(),
179-
question_id: params.get("questionId").unwrap().to_string(),
174+
question_id: QuestionId(params.get("questionId").unwrap().to_string()),
180175
};
181176

182-
store.answers.write().insert(answer.clone().id, answer);
177+
store.answers.write().insert(answer.id.clone(), answer);
183178

184179
Ok(warp::reply::with_status("Answer added", StatusCode::OK))
185180
}

0 commit comments

Comments
 (0)