1
- use parking_lot:: RwLock ;
2
1
use serde:: { Deserialize , Serialize } ;
3
2
use std:: { collections:: HashMap , sync:: Arc } ;
4
3
use warp:: {
@@ -9,6 +8,8 @@ use warp::{
9
8
Filter , Rejection , Reply ,
10
9
} ;
11
10
11
+ use tokio:: sync:: RwLock ;
12
+
12
13
#[ derive( Deserialize , Serialize , Debug , Clone ) ]
13
14
struct Question {
14
15
id : QuestionId ,
@@ -123,11 +124,11 @@ async fn get_questions(
123
124
) -> Result < impl warp:: Reply , warp:: Rejection > {
124
125
if !params. is_empty ( ) {
125
126
let pagination = extract_pagination ( params) ?;
126
- let res: Vec < Question > = store. questions . read ( ) . values ( ) . cloned ( ) . collect ( ) ;
127
+ let res: Vec < Question > = store. questions . read ( ) . await . values ( ) . cloned ( ) . collect ( ) ;
127
128
let res = & res[ pagination. start ..pagination. end ] ;
128
129
Ok ( warp:: reply:: json ( & res) )
129
130
} else {
130
- let res: Vec < Question > = store. questions . read ( ) . values ( ) . cloned ( ) . collect ( ) ;
131
+ let res: Vec < Question > = store. questions . read ( ) . await . values ( ) . cloned ( ) . collect ( ) ;
131
132
Ok ( warp:: reply:: json ( & res) )
132
133
}
133
134
}
@@ -139,6 +140,7 @@ async fn add_question(
139
140
store
140
141
. questions
141
142
. write ( )
143
+ . await
142
144
. insert ( question. id . clone ( ) , question) ;
143
145
144
146
Ok ( warp:: reply:: with_status ( "Question added" , StatusCode :: OK ) )
@@ -149,7 +151,7 @@ async fn update_question(
149
151
store : Store ,
150
152
question : Question ,
151
153
) -> Result < impl warp:: Reply , warp:: Rejection > {
152
- match store. questions . write ( ) . get_mut ( & QuestionId ( id) ) {
154
+ match store. questions . write ( ) . await . get_mut ( & QuestionId ( id) ) {
153
155
Some ( q) => * q = question,
154
156
None => return Err ( warp:: reject:: custom ( Error :: QuestionNotFound ) ) ,
155
157
}
@@ -158,7 +160,7 @@ async fn update_question(
158
160
}
159
161
160
162
async fn delete_question ( id : String , store : Store ) -> Result < impl warp:: Reply , warp:: Rejection > {
161
- match store. questions . write ( ) . remove ( & QuestionId ( id) ) {
163
+ match store. questions . write ( ) . await . remove ( & QuestionId ( id) ) {
162
164
Some ( _) => return Ok ( warp:: reply:: with_status ( "Question deleted" , StatusCode :: OK ) ) ,
163
165
None => return Err ( warp:: reject:: custom ( Error :: QuestionNotFound ) ) ,
164
166
}
@@ -174,7 +176,11 @@ async fn add_answer(
174
176
question_id : QuestionId ( params. get ( "questionId" ) . unwrap ( ) . to_string ( ) ) ,
175
177
} ;
176
178
177
- store. answers . write ( ) . insert ( answer. id . clone ( ) , answer) ;
179
+ store
180
+ . answers
181
+ . write ( )
182
+ . await
183
+ . insert ( answer. id . clone ( ) , answer) ;
178
184
179
185
Ok ( warp:: reply:: with_status ( "Answer added" , StatusCode :: OK ) )
180
186
}
0 commit comments