|
| 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