Skip to content

Commit b85db6b

Browse files
committed
Convert from in memory to a real database
Had a bunch of issues following the text - corresponding_question vs question_id in a bunch of queries - not RETURNING id, content, corresponding_question" from insert - comile errors between rust 1.73 and sqlx 0.7.4 ``` error[E0658]: `async fn` return type cannot contain a projection or `Self` that references lifetimes from a parent scope error: could not compile `sqlx-core` ``` - launchbadge/sqlx#2855 (comment) - updated with ```sh rustc --version rustc 1.73 rustup update rustc --version rustc 1.78.0 cargo --version cargo 1.78.0 ``` - but got there in the end - try: just just clean just setup-db cargo run just demo-crud
1 parent 1dc666c commit b85db6b

File tree

14 files changed

+1368
-206
lines changed

14 files changed

+1368
-206
lines changed

โ€Žlang/rust/rust_web_development/question_in_db_crud/Cargo.lock

+877-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

โ€Žlang/rust/rust_web_development/question_in_db_crud/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ handle-errors = { path = "handle-errors" }
1313
uuid = { version = "1.8.0", features = ["v4"] }
1414
tracing = { version = "0.1.40", features = ["log"] }
1515
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
16+
sqlx = { version = "0.7.4", features = [ "runtime-tokio-rustls", "postgres" ] }

โ€Žlang/rust/rust_web_development/question_in_db_crud/handle-errors/Cargo.lock

+115
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

โ€Žlang/rust/rust_web_development/question_in_db_crud/handle-errors/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ edition = "2021"
77

88
[dependencies]
99
warp = "0.3.7"
10+
tracing = { version = "0.1.40", features = ["log"] }
11+
sqlx = { version = "0.7.4" }

โ€Žlang/rust/rust_web_development/question_in_db_crud/handle-errors/src/lib.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use warp::{
44
reject::Reject,
55
Rejection, Reply,
66
};
7+
use tracing::{instrument};
8+
use sqlx::error::Error as SqlxError;
79

810
#[derive(Debug)]
911
pub enum Error {
@@ -12,26 +14,28 @@ pub enum Error {
1214
WrongIndex,
1315
QuestionNotFound,
1416
AnswerNotFound,
17+
DatabaseQueryError(SqlxError),
1518
}
1619

1720
impl std::fmt::Display for Error {
1821
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
19-
match *self {
22+
match &*self {
2023
Error::ParseError(ref err) => {
2124
write!(f, "Cannot parse parameter: {}", err)
2225
}
2326
Error::MissingParameters => write!(f, "Missing parameter"),
2427
Error::WrongIndex => write!(f, "Wrong index, start needs to be less than end"),
2528
Error::QuestionNotFound => write!(f, "Question not found"),
2629
Error::AnswerNotFound => write!(f, "Answer not found"),
30+
Error::DatabaseQueryError(e) => write!(f, "Error querying the database {}", e),
2731
}
2832
}
2933
}
3034

3135
impl Reject for Error {}
3236

37+
#[instrument]
3338
pub async fn return_error(r: Rejection) -> Result<impl Reply, Rejection> {
34-
// println!("{:?}", r);
3539
if let Some(error) = r.find::<Error>() {
3640
Ok(warp::reply::with_status(
3741
error.to_string(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
set shell := ["zsh", "-uc"]
2+
3+
# Colors
4+
RED := "\\u001b[31m"
5+
GREEN := "\\u001b[32m"
6+
YELLOW := "\\u001b[33m"
7+
BLUE := "\\u001b[34m"
8+
MAGENTA := "\\u001b[35m"
9+
BOLD := "\\u001b[1m"
10+
RESET := "\\u001b[0m"
11+
12+
_default:
13+
@just --list --unsorted
14+
15+
# setup the rustwedev database
16+
@setup-db:
17+
psql rustwebdev -XtAc "\q" > /dev/null 2>&1 || \
18+
psql -c "create database rustwebdev;" && \
19+
echo "rustwebdev created"
20+
21+
psql rustwebdev -c " \
22+
CREATE TABLE IF NOT EXISTS questions ( \
23+
id serial PRIMARY KEY, \
24+
title VARCHAR (255) NOT NULL, \
25+
content TEXT NOT NULL, \
26+
tags TEXT [], \
27+
created_on TIMESTAMP NOT NULL DEFAULT NOW() \
28+
);"
29+
psql rustwebdev -c " \
30+
CREATE TABLE IF NOT EXISTS answers ( \
31+
id serial PRIMARY KEY, \
32+
content TEXT NOT NULL, \
33+
created_on TIMESTAMP NOT NULL DEFAULT NOW(), \
34+
corresponding_question integer REFERENCES questions \
35+
);"
36+
psql rustwebdev -c "\dt"
37+
38+
QID := "1"
39+
# demo CRUD
40+
demo-crud:
41+
@echo "{{YELLOW}}attempting to use id: {{QID}} but should get this from create{{RESET}}"
42+
43+
@echo "{{GREEN}}๐Ÿ„ป ist{{RESET}}"
44+
curl localhost:1337/questions
45+
@echo
46+
47+
@echo "{{GREEN}}๐Ÿ„ฒ reate{{RESET}}"
48+
curl localhost:1337/questions \
49+
-H "Content-Type: application/json" \
50+
-d '{"id":"{{QID}}","title":"the title","content":"the content"}'
51+
@echo
52+
@echo "{{RED}}above create should return the :id{{RESET}}"
53+
54+
@echo "{{GREEN}}\n๐Ÿ… ead{{RESET}}"
55+
curl "localhost:1337/question/{{QID}}"
56+
57+
@echo "{{GREEN}}\n๐Ÿ…„ pdate{{RESET}}"
58+
curl -X PUT \
59+
"localhost:1337/questions/{{QID}}" \
60+
-H 'content-type: application/json' \
61+
-d '{"id":"'QID'","title":"NOT","content":"NOT"}'
62+
63+
# check again
64+
curl "localhost:1337/question/{{QID}}"
65+
66+
@echo "\n{{GREEN}}๐Ÿ…ฐ ๐Ÿ…ณ ๐Ÿ…ณ {{YELLOW}}๐“๐“ท๐“ผ๐”€๐“ฎ๐“ป{{RESET}}"
67+
curl localhost:1337/answers \
68+
-H "Content-Type: application/json" \
69+
-d '{"id":1,"content":"also via json","question_id":"{{QID}}"}'
70+
@echo
71+
@echo "{{RED}}above fails as only form encoding is supported{{RESET}}"
72+
73+
@echo "\n{{GREEN}}๐Ÿ…ฐ ๐Ÿ…ณ ๐Ÿ…ณ {{YELLOW}}๐“๐“ท๐“ผ๐”€๐“ฎ๐“ป{{RESET}}"
74+
curl localhost:1337/answers \
75+
-H "Content-Type: application/x-www-form-urlencoded" \
76+
--data-urlencode "id=1" \
77+
--data-urlencode "content=also from a form" \
78+
--data-urlencode "question_id={{QID}}"
79+
80+
@echo "\n{{GREEN}}๐Ÿ…ถ ๐Ÿ…ด ๐Ÿ†ƒ {{YELLOW}}๐“๐“ท๐“ผ๐”€๐“ฎ๐“ป๐“ผ{{RESET}}"
81+
curl "localhost:1337/questions/{{QID}}/answers"
82+
83+
@echo "{{GREEN}}\n๐Ÿ„ณ elete{{RESET}}"
84+
curl -X DELETE "localhost:1337/questions/{{QID}}"
85+
86+
@echo "{{GREEN}}\n๐Ÿ„ป ist{{RESET}}"
87+
curl localhost:1337/questions
88+
89+
# cleanup and remove rustwebdev database
90+
@clean:
91+
psql rustwebdev -c '\q' > /dev/null 2>&1 && \
92+
psql -c "drop database rustwebdev;" || \
93+
echo "rustwebdev alrady dropped"

0 commit comments

Comments
ย (0)