diff options
author | alpaylan | 2021-04-14 00:55:24 +0300 |
---|---|---|
committer | alpaylan | 2021-04-14 00:55:24 +0300 |
commit | 75a19ffdf7b0455946a73de73733eae475b8bc5e (patch) | |
tree | 3f6167d4bfd9e3bfee65e7e61f4fbc9c34ff6869 /src/handlers.rs | |
parent | 54e81adac6a24291e2ee16cf5597f127122f62eb (diff) | |
download | gradecoin-75a19ffdf7b0455946a73de73733eae475b8bc5e.tar.gz gradecoin-75a19ffdf7b0455946a73de73733eae475b8bc5e.tar.bz2 gradecoin-75a19ffdf7b0455946a73de73733eae475b8bc5e.zip |
add balance check to transactions.
Diffstat (limited to 'src/handlers.rs')
-rw-r--r-- | src/handlers.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/handlers.rs b/src/handlers.rs index 362a341..be2a020 100644 --- a/src/handlers.rs +++ b/src/handlers.rs | |||
@@ -291,6 +291,40 @@ pub async fn authorized_propose_transaction( | |||
291 | 291 | ||
292 | // `user` is an authenticated student, can propose | 292 | // `user` is an authenticated student, can propose |
293 | 293 | ||
294 | // check if user can afford the transaction | ||
295 | if new_transaction.by == new_transaction.source { // Propose to transact with another user | ||
296 | if internal_user.balance < new_transaction.amount { | ||
297 | return Ok(warp::reply::with_status( | ||
298 | warp::reply::json(&GradeCoinResponse { | ||
299 | res: ResponseType::Error, | ||
300 | message: "User does not have enough balance in their account" | ||
301 | .to_owned(), | ||
302 | }), | ||
303 | StatusCode::BAD_REQUEST, | ||
304 | )); | ||
305 | } | ||
306 | } else if new_transaction.by == new_transaction.target { | ||
307 | if internal_user.balance < new_transaction.amount { | ||
308 | return Ok(warp::reply::with_status( | ||
309 | warp::reply::json(&GradeCoinResponse { | ||
310 | res: ResponseType::Error, | ||
311 | message: "Bank does not have enough balance" | ||
312 | .to_owned(), | ||
313 | }), | ||
314 | StatusCode::BAD_REQUEST, | ||
315 | )); | ||
316 | } | ||
317 | } else { | ||
318 | return Ok(warp::reply::with_status( | ||
319 | warp::reply::json(&GradeCoinResponse { | ||
320 | res: ResponseType::Error, | ||
321 | message: "Invalid by field for the proposed transaction" | ||
322 | .to_owned(), | ||
323 | }), | ||
324 | StatusCode::BAD_REQUEST, | ||
325 | )); | ||
326 | } | ||
327 | |||
294 | // This public key was already written to the database, we can panic if it's not valid at | 328 | // This public key was already written to the database, we can panic if it's not valid at |
295 | // *this* point | 329 | // *this* point |
296 | let proposer_public_key = &internal_user.public_key; | 330 | let proposer_public_key = &internal_user.public_key; |
@@ -331,6 +365,7 @@ pub async fn authorized_propose_transaction( | |||
331 | 365 | ||
332 | debug!("clear for transaction proposal"); | 366 | debug!("clear for transaction proposal"); |
333 | 367 | ||
368 | |||
334 | let mut transactions = db.pending_transactions.write(); | 369 | let mut transactions = db.pending_transactions.write(); |
335 | transactions.insert(new_transaction.source.to_owned(), new_transaction); | 370 | transactions.insert(new_transaction.source.to_owned(), new_transaction); |
336 | Ok(warp::reply::with_status( | 371 | Ok(warp::reply::with_status( |