From 968c68e12ba7396858b10fdba257180effcea45f Mon Sep 17 00:00:00 2001 From: philipp Date: Tue, 18 Jul 2023 17:32:34 +0200 Subject: [PATCH 01/15] clean code w/ clippy --- src/rest/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/rest/mod.rs b/src/rest/mod.rs index 706a646..b90bac0 100644 --- a/src/rest/mod.rs +++ b/src/rest/mod.rs @@ -1,6 +1,4 @@ -use rocket::{ - form::Form, fs::FileServer, http::CookieJar, post, routes, Build, FromForm, Rocket, State, -}; +use rocket::{form::Form, fs::FileServer, post, routes, Build, FromForm, Rocket, State}; use serde_json::json; use sqlx::SqlitePool; From 5a27c5a225c1c3ad9d83731e198b29261c7ebc49 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 10:26:50 +0200 Subject: [PATCH 02/15] add first tera test --- src/tera/cox.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/tera/cox.rs b/src/tera/cox.rs index 6684566..bbc6ed0 100644 --- a/src/tera/cox.rs +++ b/src/tera/cox.rs @@ -159,3 +159,63 @@ async fn remove(db: &State, planned_event_id: i64, cox: CoxUser) -> pub fn routes() -> Vec { routes![create, join, remove, remove_trip, update] } + +#[cfg(test)] +mod test { + use chrono::NaiveDate; + use rocket::{ + http::{ContentType, Status}, + local::asynchronous::Client, + }; + use sqlx::SqlitePool; + + use crate::{model::trip::Trip, testdb}; + + #[sqlx::test] + fn test_trip_create() { + let db = testdb!(); + + assert_eq!( + 0, + Trip::get_for_day(&db, NaiveDate::from_ymd_opt(2999, 12, 30).unwrap()) + .await + .len() + ); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=cox&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client + .post("/cox/trip") + .header(ContentType::Form) + .body("day=2999-12-30&planned_starting_time=12:34&max_people=42&allow_guests=false"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!( + flash_cookie.value(), + "7:successAusfahrt erfolgreich erstellt." + ); + + assert_eq!( + 1, + Trip::get_for_day(&db, NaiveDate::from_ymd_opt(2999, 12, 30).unwrap()) + .await + .len() + ); + } +} From e9d06d785e3f6a263ae418bdacf97b4b694d3363 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 10:32:30 +0200 Subject: [PATCH 03/15] add todos --- README.md | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db8dea1..0c31783 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ - [] exactly same time -> deny registration - [] automatically add regular planned trip - [] User sync w/ nextcloud -- [] Rocket tests for /rest +- [] Rocket tests for /rest (see below) - [] same day+time: aggregate stats (x people, of which y cox and z rower) # Frontend Process @@ -27,9 +27,41 @@ ´npm install´ ´npm run (watch/build)´ +# Missing backend tests + +- [ ] (index) GET / +- [ ] (faq) GET /faq +- [ ] (cal) GET /cal +- [ ] (FileServer: svelte/build) GET / +- [ ] (join) GET /join/ +- [ ] (remove) GET /remove/ +- [x] (create) POST /cox/trip +- [ ] (update) POST /cox/trip/ +- [ ] (join) GET /cox/join/ +- [ ] (remove) GET /cox/remove/ +- [ ] (remove_trip) GET /cox/remove/trip/ +- [ ] (index) GET /auth/ +- [ ] (login) POST /auth/ +- [ ] (logout) GET /auth/logout +- [ ] (updatepw) POST /auth/set-pw +- [ ] (setpw) GET /auth/set-pw/ +- [ ] (rss) GET /admin/rss? +- [ ] (index) GET /admin/user +- [ ] (update) POST /admin/user +- [ ] (create) POST /admin/planned-event +- [ ] (update) PUT /admin/planned-event +- [ ] (create) POST /admin/user/new +- [ ] (delete) GET /admin/user//delete +- [ ] (resetpw) GET /admin/user//reset-pw +- [ ] (delete) GET /admin/planned-event//delete +- [ ] (FileServer: static/) GET /public/ [10] +- [ ] (login) POST /api/login/ + ---- +# Thoughts on nextcloud sync tables + user - UID - pw From d6b7f61729a2b9533e92ad34bab4be1310cdace3 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 10:45:55 +0200 Subject: [PATCH 04/15] restrucutre readme --- README.md | 70 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 0c31783..8f97b05 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,55 @@ +# Backend +- [] **Create missing backend tests (see below)** +- [] ics for registered trips + +## New large features +### Logbuch +- Log with activities + +### Guest-Scheckbuch +### Bootsreservierungen + +## Backlog (i.e. don't work on this now) +### Sync w/ nextcloud +- remove most fields (names, ...) from users and add uid +- create user_nextcloud table; to be re-created every day(?) + +user +- UID +- pw +- last_access + +user_details +- UID +- fn (formatted name) +- is_cox (if CATEGORIES = {Steuerleute, Bootsführer}) +- is_admin (if CATEGORIES = Admin) +- is_guest (if person not in nextcloud) + +### Misc +- [] Don't show events if time > 1h(?) ago +- [] exactly same time -> deny registration +- [] automatically add regular planned trip +- [] same day+time: aggregate stats (x people, of which y cox and z rower) + + + +# Frontend Process +´cd frontend´ +´npm install´ +´npm run (watch/build)´ + # Notes / Bugfixes ## Frontend - [] add UI for `trip_type` - [] support esc to close sidebar - [] after an hour(?) of inactivity -> show large popup w/ "maybe old data (ignore) (reload page)" (ignore bc maybe use is actively doing something -> don't throw input away!) -## Backend -- [] Don't show events if time > 1h(?) ago -- [] Sync w/ nextcloud - - remove most fields (names, ...) from users and add uid - - create user_nextcloud table; to be re-created every day(?) -- [] ics for registered trips # Nice to have ## Frontend - [] my trips for cox -## Backend -- [] exactly same time -> deny registration -- [] automatically add regular planned trip -- [] User sync w/ nextcloud -- [] Rocket tests for /rest (see below) -- [] same day+time: aggregate stats (x people, of which y cox and z rower) - -# Frontend Process -´cd frontend´ -´npm install´ -´npm run (watch/build)´ - # Missing backend tests - [ ] (index) GET / @@ -62,14 +85,3 @@ # Thoughts on nextcloud sync tables -user -- UID -- pw -- last_access - -user_details -- UID -- fn (formatted name) -- is_cox (if CATEGORIES = {Steuerleute, Bootsführer}) -- is_admin (if CATEGORIES = Admin) -- is_guest (if person not in nextcloud) From 67c5f20511ca5d794cccc5b80b155f958f2331b5 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 10:48:19 +0200 Subject: [PATCH 05/15] fix tests --- .gitignore | 1 - static/images/favicon.ico | Bin 0 -> 4286 bytes static/main.css | 1 + static/main.js | 1 + static/manifest.json | 14 ++++++++++++++ 5 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 static/images/favicon.ico create mode 100644 static/main.css create mode 100644 static/main.js create mode 100644 static/manifest.json diff --git a/.gitignore b/.gitignore index 3049400..b098b84 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,4 @@ target/ db.sqlite .history/ Rocket.toml -static/* frontend/node_modules/* diff --git a/static/images/favicon.ico b/static/images/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..925e82c565a87b8fd6112bab5f0e746148e0ff16 GIT binary patch literal 4286 zcmc&$F-sgl6dofO2%@P{ie-L)1PMYcQiLE@K_mq6pB&+^4q|B`DJ1y=BB?yN!Xk)8 z3b8U^5kiU>f`mh7p5OO&p1Zhv9^P%{-tlgRZ@%}v**8105R>O+ylzAuIOjiP0&yA z?YE3o^{&E`j8o&492>un?*z4hES)G`UfkSSccQMvA2+msUv+xCeT#+9b$CLvuW9l${c4|`- z_pIRIJ7r~J98(UsoMRm|keAS+-Y-1c`>bE}%_XKaA_rWZFJcEZza4cyp*@Ydugj_e zOy>!XCl_;O>>$Ry9xkHRR@CSFU2)bD7m-PL&ZXnt}TgZ$L!*TP}c`w`ne)wMT$T?24*4i*EAe$8(} z>o+{s(y#YcB1iKx2C>u_#AFQ5ya1h#XmR%TGIErQ7{=ah^7lw?yt5@@e)U4cJTyei n4fA_B_wqE;I6Dw&w?&%!nUA(aqKDB|BRu?``{a2;CeP&>Pnf0# literal 0 HcmV?d00001 diff --git a/static/main.css b/static/main.css new file mode 100644 index 0000000..8f7f0e4 --- /dev/null +++ b/static/main.css @@ -0,0 +1 @@ +*,:before,:after{box-sizing:border-box;border-width:0;border-style:solid;border-color:#e2e8f0}:before,:after{--tw-content: ""}html{line-height:1.5;-webkit-text-size-adjust:100%;-moz-tab-size:4;-o-tab-size:4;tab-size:4;font-family:ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,"Apple Color Emoji","Segoe UI Emoji",Segoe UI Symbol,"Noto Color Emoji";font-feature-settings:normal;font-variation-settings:normal}body{margin:0;line-height:inherit}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,samp,pre{font-family:ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,Liberation Mono,Courier New,monospace;font-size:1em}small{font-size:80%}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}button,input,optgroup,select,textarea{font-family:inherit;font-size:100%;font-weight:inherit;line-height:inherit;color:inherit;margin:0;padding:0}button,select{text-transform:none}button,[type=button],[type=reset],[type=submit]{-webkit-appearance:button;background-color:transparent;background-image:none}:-moz-focusring{outline:auto}:-moz-ui-invalid{box-shadow:none}progress{vertical-align:baseline}::-webkit-inner-spin-button,::-webkit-outer-spin-button{height:auto}[type=search]{-webkit-appearance:textfield;outline-offset:-2px}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}summary{display:list-item}blockquote,dl,dd,h1,h2,h3,h4,h5,h6,hr,figure,p,pre{margin:0}fieldset{margin:0;padding:0}legend{padding:0}ol,ul,menu{list-style:none;margin:0;padding:0}textarea{resize:vertical}input::-moz-placeholder,textarea::-moz-placeholder{opacity:1;color:#94a3b8}input::placeholder,textarea::placeholder{opacity:1;color:#94a3b8}button,[role=button]{cursor:pointer}:disabled{cursor:default}img,svg,video,canvas,audio,iframe,embed,object{display:block;vertical-align:middle}img,video{max-width:100%;height:auto}[hidden]{display:none}*,:before,:after{--tw-border-spacing-x: 0;--tw-border-spacing-y: 0;--tw-translate-x: 0;--tw-translate-y: 0;--tw-rotate: 0;--tw-skew-x: 0;--tw-skew-y: 0;--tw-scale-x: 1;--tw-scale-y: 1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness: proximity;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width: 0px;--tw-ring-offset-color: #fff;--tw-ring-color: rgb(59 130 246 / .5);--tw-ring-offset-shadow: 0 0 #0000;--tw-ring-shadow: 0 0 #0000;--tw-shadow: 0 0 #0000;--tw-shadow-colored: 0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }::backdrop{--tw-border-spacing-x: 0;--tw-border-spacing-y: 0;--tw-translate-x: 0;--tw-translate-y: 0;--tw-rotate: 0;--tw-skew-x: 0;--tw-skew-y: 0;--tw-scale-x: 1;--tw-scale-y: 1;--tw-pan-x: ;--tw-pan-y: ;--tw-pinch-zoom: ;--tw-scroll-snap-strictness: proximity;--tw-ordinal: ;--tw-slashed-zero: ;--tw-numeric-figure: ;--tw-numeric-spacing: ;--tw-numeric-fraction: ;--tw-ring-inset: ;--tw-ring-offset-width: 0px;--tw-ring-offset-color: #fff;--tw-ring-color: rgb(59 130 246 / .5);--tw-ring-offset-shadow: 0 0 #0000;--tw-ring-shadow: 0 0 #0000;--tw-shadow: 0 0 #0000;--tw-shadow-colored: 0 0 #0000;--tw-blur: ;--tw-brightness: ;--tw-contrast: ;--tw-grayscale: ;--tw-hue-rotate: ;--tw-invert: ;--tw-saturate: ;--tw-sepia: ;--tw-drop-shadow: ;--tw-backdrop-blur: ;--tw-backdrop-brightness: ;--tw-backdrop-contrast: ;--tw-backdrop-grayscale: ;--tw-backdrop-hue-rotate: ;--tw-backdrop-invert: ;--tw-backdrop-opacity: ;--tw-backdrop-saturate: ;--tw-backdrop-sepia: }.sr-only{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border-width:0}.fixed{position:fixed}.absolute{position:absolute}.relative{position:relative}.inset-y-0{top:0px;bottom:0px}.left-0{left:0px}.z-10{z-index:10}.mx-1{margin-left:.25rem;margin-right:.25rem}.mx-auto{margin-left:auto;margin-right:auto}.mb-1{margin-bottom:.25rem}.mb-2{margin-bottom:.5rem}.mb-3{margin-bottom:.75rem}.mb-4{margin-bottom:1rem}.ml-1{margin-left:.25rem}.ml-4{margin-left:1rem}.mr-1{margin-right:.25rem}.mr-2{margin-right:.5rem}.mr-3{margin-right:.75rem}.mt-1{margin-top:.25rem}.mt-2{margin-top:.5rem}.mt-4{margin-top:1rem}.mt-6{margin-top:1.5rem}.mt-8{margin-top:2rem}.block{display:block}.inline-block{display:inline-block}.inline{display:inline}.flex{display:flex}.inline-flex{display:inline-flex}.grid{display:grid}.hidden{display:none}.h-10{height:2.5rem}.h-16{height:4rem}.h-3{height:.75rem}.h-4{height:1rem}.h-5{height:1.25rem}.h-8{height:2rem}.min-h-screen{min-height:100vh}.w-28{width:7rem}.w-3{width:.75rem}.w-4{width:1rem}.w-5{width:1.25rem}.w-auto{width:auto}.w-full{width:100%}.max-w-md{max-width:28rem}.max-w-screen-lg{max-width:1024px}.max-w-screen-xl{max-width:1280px}.flex-shrink-0{flex-shrink:0}.rotate-45{--tw-rotate: 45deg;transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skew(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.transform{transform:translate(var(--tw-translate-x),var(--tw-translate-y)) rotate(var(--tw-rotate)) skew(var(--tw-skew-x)) skewY(var(--tw-skew-y)) scaleX(var(--tw-scale-x)) scaleY(var(--tw-scale-y))}.cursor-pointer{cursor:pointer}.list-disc{list-style-type:disc}.grid-cols-1{grid-template-columns:repeat(1,minmax(0,1fr))}.grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.flex-col{flex-direction:column}.items-start{align-items:flex-start}.items-end{align-items:flex-end}.items-center{align-items:center}.justify-center{justify-content:center}.justify-between{justify-content:space-between}.gap-10{gap:2.5rem}.gap-2{gap:.5rem}.gap-3{gap:.75rem}.gap-4{gap:1rem}.-space-y-px>:not([hidden])~:not([hidden]){--tw-space-y-reverse: 0;margin-top:calc(-1px * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(-1px * var(--tw-space-y-reverse))}.space-y-6>:not([hidden])~:not([hidden]){--tw-space-y-reverse: 0;margin-top:calc(1.5rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(1.5rem * var(--tw-space-y-reverse))}.space-y-8>:not([hidden])~:not([hidden]){--tw-space-y-reverse: 0;margin-top:calc(2rem * calc(1 - var(--tw-space-y-reverse)));margin-bottom:calc(2rem * var(--tw-space-y-reverse))}.rounded-md{border-radius:.375rem}.rounded-b-md{border-bottom-right-radius:.375rem;border-bottom-left-radius:.375rem}.rounded-t-md{border-top-left-radius:.375rem;border-top-right-radius:.375rem}.rounded-bl-md{border-bottom-left-radius:.375rem}.rounded-br-md{border-bottom-right-radius:.375rem}.border{border-width:1px}.border-0{border-width:0px}.border-t{border-top-width:1px}.border-t-0{border-top-width:0px}.border-\[\#f43f5e\]{--tw-border-opacity: 1;border-color:rgb(244 63 94 / var(--tw-border-opacity))}.bg-\[\#f43f5e\]{--tw-bg-opacity: 1;background-color:rgb(244 63 94 / var(--tw-bg-opacity))}.bg-gray-100{--tw-bg-opacity: 1;background-color:rgb(241 245 249 / var(--tw-bg-opacity))}.bg-gray-200{--tw-bg-opacity: 1;background-color:rgb(226 232 240 / var(--tw-bg-opacity))}.bg-primary-100{--tw-bg-opacity: 1;background-color:rgb(219 234 254 / var(--tw-bg-opacity))}.bg-primary-200{--tw-bg-opacity: 1;background-color:rgb(191 219 254 / var(--tw-bg-opacity))}.bg-primary-50{--tw-bg-opacity: 1;background-color:rgb(239 246 255 / var(--tw-bg-opacity))}.bg-primary-500{--tw-bg-opacity: 1;background-color:rgb(59 130 246 / var(--tw-bg-opacity))}.bg-primary-600{--tw-bg-opacity: 1;background-color:rgb(37 99 235 / var(--tw-bg-opacity))}.bg-primary-900{--tw-bg-opacity: 1;background-color:rgb(30 58 138 / var(--tw-bg-opacity))}.bg-primary-950{--tw-bg-opacity: 1;background-color:rgb(23 37 84 / var(--tw-bg-opacity))}.bg-white{--tw-bg-opacity: 1;background-color:rgb(255 255 255 / var(--tw-bg-opacity))}.bg-opacity-80{--tw-bg-opacity: .8}.p-1{padding:.25rem}.p-2{padding:.5rem}.p-3{padding:.75rem}.px-2{padding-left:.5rem;padding-right:.5rem}.px-3{padding-left:.75rem;padding-right:.75rem}.px-4{padding-left:1rem;padding-right:1rem}.py-1{padding-top:.25rem;padding-bottom:.25rem}.py-1\.5{padding-top:.375rem;padding-bottom:.375rem}.py-12{padding-top:3rem;padding-bottom:3rem}.py-2{padding-top:.5rem;padding-bottom:.5rem}.py-3{padding-top:.75rem;padding-bottom:.75rem}.pb-3{padding-bottom:.75rem}.pl-3{padding-left:.75rem}.ps-1{-webkit-padding-start:.25rem;padding-inline-start:.25rem}.pt-2{padding-top:.5rem}.pt-8{padding-top:2rem}.text-left{text-align:left}.text-center{text-align:center}.text-right{text-align:right}.text-lg{font-size:1.125rem;line-height:1.75rem}.text-sm{font-size:.875rem;line-height:1.25rem}.text-xs{font-size:.75rem;line-height:1rem}.font-bold{font-weight:700}.font-semibold{font-weight:600}.uppercase{text-transform:uppercase}.tracking-wide{letter-spacing:.025em}.text-\[\#f43f5e\]{--tw-text-opacity: 1;color:rgb(244 63 94 / var(--tw-text-opacity))}.text-\[\#ff0000\]{--tw-text-opacity: 1;color:rgb(255 0 0 / var(--tw-text-opacity))}.text-black{--tw-text-opacity: 1;color:rgb(0 0 0 / var(--tw-text-opacity))}.text-gray-200{--tw-text-opacity: 1;color:rgb(226 232 240 / var(--tw-text-opacity))}.text-gray-500{--tw-text-opacity: 1;color:rgb(100 116 139 / var(--tw-text-opacity))}.text-gray-600{--tw-text-opacity: 1;color:rgb(71 85 105 / var(--tw-text-opacity))}.text-gray-900{--tw-text-opacity: 1;color:rgb(15 23 42 / var(--tw-text-opacity))}.text-primary-300{--tw-text-opacity: 1;color:rgb(147 197 253 / var(--tw-text-opacity))}.text-primary-600{--tw-text-opacity: 1;color:rgb(37 99 235 / var(--tw-text-opacity))}.text-primary-900{--tw-text-opacity: 1;color:rgb(30 58 138 / var(--tw-text-opacity))}.text-primary-950{--tw-text-opacity: 1;color:rgb(23 37 84 / var(--tw-text-opacity))}.text-white{--tw-text-opacity: 1;color:rgb(255 255 255 / var(--tw-text-opacity))}.underline{text-decoration-line:underline}.accent-gray-200{accent-color:#e2e8f0}.accent-primary-600{accent-color:#2563eb}.shadow{--tw-shadow: 0 1px 3px 0 rgb(0 0 0 / .1), 0 1px 2px -1px rgb(0 0 0 / .1);--tw-shadow-colored: 0 1px 3px 0 var(--tw-shadow-color), 0 1px 2px -1px var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000),var(--tw-ring-shadow, 0 0 #0000),var(--tw-shadow)}.shadow-sm{--tw-shadow: 0 1px 2px 0 rgb(0 0 0 / .05);--tw-shadow-colored: 0 1px 2px 0 var(--tw-shadow-color);box-shadow:var(--tw-ring-offset-shadow, 0 0 #0000),var(--tw-ring-shadow, 0 0 #0000),var(--tw-shadow)}.ring-1{--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow, 0 0 #0000)}.ring-inset{--tw-ring-inset: inset}.ring-gray-300{--tw-ring-opacity: 1;--tw-ring-color: rgb(203 213 225 / var(--tw-ring-opacity))}.h1{text-align:center;font-size:1.875rem;line-height:2.25rem;font-weight:700;text-transform:uppercase;letter-spacing:.025em;--tw-text-opacity: 1;color:rgb(30 58 138 / var(--tw-text-opacity))}.sidebar{position:fixed;overflow-y:scroll;top:0;background:white;z-index:2000;width:0;max-width:0;box-shadow:0 1rem 3rem #0000002d}.sidebar.open{background-color:#fff;display:block;height:100vh;right:0;top:0;width:100%;max-width:375px;z-index:40000}.sidebar.slide-in{transition-duration:75ms;transition-property:all;transition-timing-function:cubic-bezier(.4,0,.2,1);transition-timing-function:cubic-bezier(0,0,.2,1)}.sidebar.from-right{right:-24rem}.sidebar.from-right.open{right:0}.sidebar.from-left{left:-24rem}.sidebar.from-left.open{left:0}.sidebar-overlay{display:none}.sidebar-overlay.show{background-color:#0003;content:"";display:block;height:100%;left:0;position:fixed;top:0;width:100%;z-index:1025}.sidebar-close{border-radius:100%;flex:0 0 auto;height:1.5rem;width:1.5rem}.sidebar-footer{position:fixed;margin:0 -8px -4px;width:374px;bottom:0}.overlay{overflow:hidden}.btn{display:inline-block;cursor:pointer;border-radius:.375rem;padding:.5rem .75rem;text-align:center;font-size:.875rem;line-height:1.25rem;font-weight:600;--tw-text-opacity: 1;color:rgb(255 255 255 / var(--tw-text-opacity))}.btn:focus-visible{outline-style:solid;outline-width:2px;outline-offset:2px}.btn-primary{--tw-bg-opacity: 1;background-color:rgb(37 99 235 / var(--tw-bg-opacity))}.btn-primary:hover{--tw-bg-opacity: 1;background-color:rgb(59 130 246 / var(--tw-bg-opacity))}.btn-primary:focus-visible{outline-color:#2563eb}.btn-dark{--tw-bg-opacity: 1;background-color:rgb(30 58 138 / var(--tw-bg-opacity))}.btn-dark:hover{--tw-bg-opacity: 1;background-color:rgb(23 37 84 / var(--tw-bg-opacity))}.btn-dark:focus-visible{outline-color:#172554}.btn-gray{--tw-bg-opacity: 1;background-color:rgb(148 163 184 / var(--tw-bg-opacity))}.btn-gray:hover{--tw-bg-opacity: 1;background-color:rgb(100 116 139 / var(--tw-bg-opacity))}.btn-gray:focus-visible{outline-color:#3b82f6}.btn-attention{--tw-bg-opacity: 1;background-color:rgb(244 63 94 / var(--tw-bg-opacity))}.btn-attention:hover{--tw-bg-opacity: 1;background-color:rgb(255 0 0 / var(--tw-bg-opacity))}.btn-attention:focus-visible{outline-color:red}.btn-alert,.btn-alert:hover{--tw-bg-opacity: 1;background-color:rgb(255 0 0 / var(--tw-bg-opacity))}.btn-alert:focus-visible{outline-color:red}.btn-fw{width:7rem}.btn[aria-pressed=true]{--tw-bg-opacity: 1;background-color:rgb(219 234 254 / var(--tw-bg-opacity));--tw-text-opacity: 1;color:rgb(23 37 84 / var(--tw-text-opacity));outline-style:solid;outline-width:2px;outline-offset:2px;outline-color:#2563eb}.link-primary{--tw-text-opacity: 1;color:rgb(37 99 235 / var(--tw-text-opacity));text-decoration-line:underline}.link-primary:hover{--tw-text-opacity: 1;color:rgb(30 58 138 / var(--tw-text-opacity))}.link-dark{--tw-text-opacity: 1;color:rgb(30 58 138 / var(--tw-text-opacity));text-decoration-line:underline}.link-dark:hover{--tw-text-opacity: 1;color:rgb(23 37 84 / var(--tw-text-opacity))}.link-white{--tw-text-opacity: 1;color:rgb(255 255 255 / var(--tw-text-opacity));text-decoration-line:underline}.link-white:hover{--tw-text-opacity: 1;color:rgb(219 234 254 / var(--tw-text-opacity))}.input{position:relative;display:block;width:100%;border-width:0px;padding:.375rem .5rem;--tw-text-opacity: 1;color:rgb(15 23 42 / var(--tw-text-opacity));--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(1px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow, 0 0 #0000);--tw-ring-inset: inset;--tw-ring-opacity: 1;--tw-ring-color: rgb(203 213 225 / var(--tw-ring-opacity))}.input::-moz-placeholder{--tw-text-opacity: 1;color:rgb(148 163 184 / var(--tw-text-opacity))}.input::placeholder{--tw-text-opacity: 1;color:rgb(148 163 184 / var(--tw-text-opacity))}.input:focus{z-index:10;--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow, 0 0 #0000);--tw-ring-inset: inset;--tw-ring-opacity: 1;--tw-ring-color: rgb(37 99 235 / var(--tw-ring-opacity))}@media (min-width: 640px){.input{font-size:.875rem;line-height:1.5rem}}.alert-success{--tw-bg-opacity: 1;background-color:rgb(21 128 61 / var(--tw-bg-opacity))}.alert-error{--tw-bg-opacity: 1;background-color:rgb(244 63 94 / var(--tw-bg-opacity))}.placeholder\:text-gray-400::-moz-placeholder{--tw-text-opacity: 1;color:rgb(148 163 184 / var(--tw-text-opacity))}.placeholder\:text-gray-400::placeholder{--tw-text-opacity: 1;color:rgb(148 163 184 / var(--tw-text-opacity))}.hover\:bg-gray-200:hover{--tw-bg-opacity: 1;background-color:rgb(226 232 240 / var(--tw-bg-opacity))}.hover\:bg-primary-500:hover{--tw-bg-opacity: 1;background-color:rgb(59 130 246 / var(--tw-bg-opacity))}.hover\:bg-primary-950:hover{--tw-bg-opacity: 1;background-color:rgb(23 37 84 / var(--tw-bg-opacity))}.hover\:text-gray-100:hover{--tw-text-opacity: 1;color:rgb(241 245 249 / var(--tw-text-opacity))}.hover\:text-gray-900:hover{--tw-text-opacity: 1;color:rgb(15 23 42 / var(--tw-text-opacity))}.hover\:text-primary-900:hover{--tw-text-opacity: 1;color:rgb(30 58 138 / var(--tw-text-opacity))}.hover\:text-primary-950:hover{--tw-text-opacity: 1;color:rgb(23 37 84 / var(--tw-text-opacity))}.focus\:z-10:focus{z-index:10}.focus\:bg-gray-200:focus{--tw-bg-opacity: 1;background-color:rgb(226 232 240 / var(--tw-bg-opacity))}.focus\:bg-primary-50:focus{--tw-bg-opacity: 1;background-color:rgb(239 246 255 / var(--tw-bg-opacity))}.focus\:bg-primary-950:focus{--tw-bg-opacity: 1;background-color:rgb(23 37 84 / var(--tw-bg-opacity))}.focus\:text-primary-950:focus{--tw-text-opacity: 1;color:rgb(23 37 84 / var(--tw-text-opacity))}.focus\:ring-2:focus{--tw-ring-offset-shadow: var(--tw-ring-inset) 0 0 0 var(--tw-ring-offset-width) var(--tw-ring-offset-color);--tw-ring-shadow: var(--tw-ring-inset) 0 0 0 calc(2px + var(--tw-ring-offset-width)) var(--tw-ring-color);box-shadow:var(--tw-ring-offset-shadow),var(--tw-ring-shadow),var(--tw-shadow, 0 0 #0000)}.focus\:ring-inset:focus{--tw-ring-inset: inset}.focus\:ring-primary-600:focus{--tw-ring-opacity: 1;--tw-ring-color: rgb(37 99 235 / var(--tw-ring-opacity))}.focus-visible\:outline:focus-visible{outline-style:solid}.focus-visible\:outline-2:focus-visible{outline-width:2px}.focus-visible\:outline-offset-2:focus-visible{outline-offset:2px}.focus-visible\:outline-primary-600:focus-visible{outline-color:#2563eb}.group:hover .group-hover\:text-primary-400{--tw-text-opacity: 1;color:rgb(96 165 250 / var(--tw-text-opacity))}@media (min-width: 640px){.sm\:col-span-2{grid-column:span 2 / span 2}.sm\:mt-0{margin-top:0}.sm\:flex{display:flex}.sm\:grid-cols-2{grid-template-columns:repeat(2,minmax(0,1fr))}.sm\:px-6{padding-left:1.5rem;padding-right:1.5rem}.sm\:text-sm{font-size:.875rem;line-height:1.25rem}.sm\:leading-6{line-height:1.5rem}}@media (min-width: 768px){.md\:mb-0{margin-bottom:0}.md\:grid-cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}.md\:items-center{align-items:center}}@media (min-width: 1024px){.lg\:col-span-3{grid-column:span 3 / span 3}.lg\:grid-cols-3{grid-template-columns:repeat(3,minmax(0,1fr))}.lg\:grid-cols-4{grid-template-columns:repeat(4,minmax(0,1fr))}.lg\:px-8{padding-left:2rem;padding-right:2rem}}@media (min-width: 1280px){.xl\:grid-cols-5{grid-template-columns:repeat(5,minmax(0,1fr))}} diff --git a/static/main.js b/static/main.js new file mode 100644 index 0000000..f05a44a --- /dev/null +++ b/static/main.js @@ -0,0 +1 @@ +var u=Object.defineProperty;var y=(e,t,s)=>t in e?u(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var l=(e,t,s)=>(y(e,typeof t!="symbol"?t+"":t,s),s);class p{constructor(t){l(this,"element");l(this,"trigger");l(this,"contentWrapper");l(this,"closeBtn");l(this,"overlay");l(this,"isOpen");this.trigger=t,this.element=document.getElementById(t),this.contentWrapper=document.querySelector("body"),this.overlay=document.querySelector(".sidebar-overlay[data-trigger="+this.trigger+"]"),this.element&&(this.closeBtn=this.element.querySelector("[data-trigger="+this.trigger+"]")),this.isOpen=!1}toggle(){var t;if(this.isOpen=!this.isOpen,this.trigger&&((t=document.getElementById(this.trigger))==null||t.classList.toggle("open")),this.contentWrapper&&this.contentWrapper.classList.toggle("overlay"),this.overlay&&this.overlay.classList.toggle("show"),this.element&&(this.element.ariaModal=this.element.ariaModal==="true"?"false":"true",this.isOpen)){const s=this.element.querySelector(".focus-js");s?s.focus():this.closeBtn.focus()}}}document.addEventListener("DOMContentLoaded",function(){q(),E(),h()});function h(){const e=document.querySelectorAll(".filter-trips-js");let t=new Map;e&&Array.prototype.forEach.call(e,r=>{t.set(r.dataset.action,r.ariaPressed),r.addEventListener("click",()=>{let o=sessionStorage.getItem("tripsFilter");if(o){let i=new Map(JSON.parse(o));for(let a of i.entries())a[0]===r.dataset.action&&a[1]!=="true"?i.set(a[0],"true"):i.set(a[0],"false");sessionStorage.setItem("tripsFilter",JSON.stringify(Array.from(i.entries())))}g(),r.getAttribute("aria-pressed")==="false"?(Array.prototype.forEach.call(e,i=>{i.setAttribute("aria-pressed","false")}),d(r.dataset.action)):r.setAttribute("aria-pressed","false")})});let s=sessionStorage.getItem("tripsFilter");if(s){let r=new Map(JSON.parse(s));for(let o of r.entries())o[1]==="true"&&d(o[0])}else sessionStorage.setItem("tripsFilter",JSON.stringify(Array.from(t.entries())))}function g(){const e=document.querySelectorAll(".reset-js.hidden");e&&Array.prototype.forEach.call(e,t=>{t.classList.remove("hidden")})}function d(e){const t=document.querySelector('button[data-action="'+e+'"]');t&&(t.setAttribute("aria-pressed","true"),m(e))}function m(e){switch(e){case"filter-days":{S();break}case"filter-coxs":{A();break}case"filter-months":{b(e);break}}}function S(){const e=document.querySelectorAll('div[data-trips="0"]');Array.prototype.forEach.call(e,t=>{t.classList.toggle("hidden")})}function A(){const e=document.querySelectorAll('div[data-coxneeded="false"]');Array.prototype.forEach.call(e,t=>{t.classList.toggle("hidden")})}function b(e){const t=["01","02","03","04","05","06","07","08","09","10","11","12"],s=document.querySelector('button[data-action="'+e+'"]');if(s){const r=s.dataset.month;if(r){const o=t.indexOf(r);o>-1&&t.splice(o,1),Array.prototype.forEach.call(t,i=>{const a=document.querySelectorAll('div[data-month="'+i+'"]');Array.prototype.forEach.call(a,n=>{n.classList.toggle("hidden")})})}}}function q(){const e=document.querySelector("#filter-js");e&&(f(e.value),e.addEventListener("input",()=>{f(e.value)}))}function f(e){const t=document.querySelectorAll('form[data-filterable="true"]');let s=document.querySelector("#filter-result-js"),r=0;Array.prototype.forEach.call(t,o=>{var a;let i=(a=o.dataset.filter)==null?void 0:a.toLocaleLowerCase();o.style.display="none",i!=null&&i.includes(e.toLocaleLowerCase())&&(o.style.display="flex",r++)}),s&&(s.innerHTML=r===0?"Kein Ergebnis gefunden":""+r+""+(r>1?" Ergebnisse":" Ergebnis")+" gefunden")}function E(){const e=document.querySelectorAll("[data-trigger]");e&&Array.prototype.forEach.call(e,t=>{if(t.dataset.trigger){const s=new p(t.dataset.trigger);t.addEventListener("click",r=>{r.preventDefault(),t.dataset.trigger==="sidebar"&&L(t),s.toggle()})}})}function L(e){const t=document.querySelector("#sidebar");if(t&&e.dataset.body&&e.dataset.header){let r=document.querySelector(e.dataset.body).cloneNode(!0),o=t.querySelector(".body-js");const i=r.querySelectorAll('input[type="checkbox"]');if(Array.prototype.forEach.call(i,n=>{var c;n&&((c=n.parentElement)==null||c.setAttribute("for",n.id+"js"),n.id+="js")}),o&&(o.innerHTML="",o.append(r)),e.dataset.day){let n=r.querySelector(".day-js");n&&(n.value=e.dataset.day)}let a=t.querySelector(".header-js");a&&(a.innerHTML=e.dataset.header)}} diff --git a/static/manifest.json b/static/manifest.json new file mode 100644 index 0000000..f9513c8 --- /dev/null +++ b/static/manifest.json @@ -0,0 +1,14 @@ +{ + "main.css": { + "file": "main.css", + "src": "main.css" + }, + "main.ts": { + "css": [ + "main.css" + ], + "file": "main.js", + "isEntry": true, + "src": "main.ts" + } +} \ No newline at end of file From efad76f970097f6f50ed7596790acaef8a629189 Mon Sep 17 00:00:00 2001 From: PhilippHofer Date: Fri, 21 Jul 2023 08:56:05 +0000 Subject: [PATCH 06/15] fix tests --- Rocket.toml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Rocket.toml diff --git a/Rocket.toml b/Rocket.toml new file mode 100644 index 0000000..f52a93b --- /dev/null +++ b/Rocket.toml @@ -0,0 +1,3 @@ +[default] +secret_key = "secret-key-for-ci" +rss_key = "rss-key-for-ci" From 757e6d630044e4789cbffc4f8b6b9da9b3c823e1 Mon Sep 17 00:00:00 2001 From: PhilippHofer Date: Fri, 21 Jul 2023 09:01:32 +0000 Subject: [PATCH 07/15] add key from https://api.rocket.rs/master/rocket/config/struct.SecretKey.html for the sake of being able to do integration tests --- Rocket.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rocket.toml b/Rocket.toml index f52a93b..5938561 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,3 +1,3 @@ [default] -secret_key = "secret-key-for-ci" +secret_key = "hPrYyЭRiMyµ5sBB1π+CMæ1køFsåqKvBiQJxBVHQk=" rss_key = "rss-key-for-ci" From 6ee049d6b1e57bec1c070eca68649d3f96f60faa Mon Sep 17 00:00:00 2001 From: PhilippHofer Date: Fri, 21 Jul 2023 09:07:25 +0000 Subject: [PATCH 08/15] add unique key for ci testing --- Rocket.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rocket.toml b/Rocket.toml index 5938561..f774acf 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,3 +1,3 @@ [default] -secret_key = "hPrYyЭRiMyµ5sBB1π+CMæ1køFsåqKvBiQJxBVHQk=" +secret_key = "/NtVGizglEoyoxBLzsRDWTy4oAG1qDw4J4O+CWJSv+fypD7W9sam8hUY4j90EZsbZk8wEradS5zBoWtWKi3k8w==" rss_key = "rss-key-for-ci" From 80c3f821dc6dbab2e428c1fca1bb5da1f643e21b Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 11:39:52 +0200 Subject: [PATCH 09/15] add todo --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8f97b05..31f3b89 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ user_details - [] exactly same time -> deny registration - [] automatically add regular planned trip - [] same day+time: aggregate stats (x people, of which y cox and z rower) +- [] Lock trip; noone can register anymore From 6d41193e216a78713c5e14fa70a16f443b0eaf07 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 12:03:01 +0200 Subject: [PATCH 10/15] clean readme --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 31f3b89..b7692a4 100644 --- a/README.md +++ b/README.md @@ -81,8 +81,3 @@ user_details - [ ] (FileServer: static/) GET /public/ [10] - [ ] (login) POST /api/login/ - ----- - -# Thoughts on nextcloud sync tables - From 18d0b906dafc051cb1f573a1bed89a466f7f8e94 Mon Sep 17 00:00:00 2001 From: philipp Date: Fri, 21 Jul 2023 12:47:21 +0200 Subject: [PATCH 11/15] wokr on db model for logbook --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/README.md b/README.md index b7692a4..c488205 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,46 @@ ### Logbuch - Log with activities +#### Tables +- boats + - id + - name + - amount_seats +- session + - id + - ship_master (Schiffsführer) + - ship_master_only_steered (default: false) + - departure (Datetime) + - Option (Datetime) // None -> on water + - destination + - Option (prefilled from destination) + - comments + - type (normal, Wanderfahrt) +- rowers + - session_id + - user_id +- damages + - boat_id + - desc + - user_id_created + - created_at + - user_id_fixed + - fixed_at + - lock_boat (default: false), if true: noone can use this boat + ### Guest-Scheckbuch +- guest_trip + - guest_user_id + - amount_trips + - paid_to_user_id +- guest_trip_session + - guest_trip_id + - session_id + ### Bootsreservierungen +- Confirmation required? +- How long in advance is it possible? +- Default reservations for some regular events (A+F, USI, ...)? ## Backlog (i.e. don't work on this now) ### Sync w/ nextcloud From d71285956651eccda0c574ad0dd5f44c9def3fa9 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 22 Jul 2023 12:24:29 +0200 Subject: [PATCH 12/15] add tests --- README.md | 12 ++-- src/tera/misc.rs | 50 +++++++++++++++++ src/tera/mod.rs | 141 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 173 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c488205..d7e1002 100644 --- a/README.md +++ b/README.md @@ -91,12 +91,12 @@ user_details # Missing backend tests -- [ ] (index) GET / -- [ ] (faq) GET /faq -- [ ] (cal) GET /cal -- [ ] (FileServer: svelte/build) GET / -- [ ] (join) GET /join/ -- [ ] (remove) GET /remove/ +- [x] (index) GET / +- [x] (faq) GET /faq +- [x] (cal) GET /cal +- [x] (FileServer: svelte/build) GET / +- [x] (join) GET /join/ +- [x] (remove) GET /remove/ - [x] (create) POST /cox/trip - [ ] (update) POST /cox/trip/ - [ ] (join) GET /cox/join/ diff --git a/src/tera/misc.rs b/src/tera/misc.rs index 7190750..d449390 100644 --- a/src/tera/misc.rs +++ b/src/tera/misc.rs @@ -11,9 +11,59 @@ async fn faq(user: User) -> Template { #[get("/cal")] async fn cal(db: &State) -> (ContentType, String) { + //TODO: add unit test once proper functionality is there (ContentType::Calendar, PlannedEvent::get_ics_feed(db).await) } pub fn routes() -> Vec { routes![faq, cal] } + +#[cfg(test)] +mod test { + use rocket::{ + http::{ContentType, Status}, + local::asynchronous::Client, + }; + use sqlx::SqlitePool; + + use crate::testdb; + + #[sqlx::test] + fn test_faq() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=cox&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/faq"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + + assert!(response.into_string().await.unwrap().contains("FAQs")); + } + + #[sqlx::test] + fn test_without_login() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + + let req = client.get("/"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/auth")); + } +} diff --git a/src/tera/mod.rs b/src/tera/mod.rs index 7a4e6bc..fff78e4 100644 --- a/src/tera/mod.rs +++ b/src/tera/mod.rs @@ -123,27 +123,120 @@ pub fn config(rocket: Rocket) -> Rocket { .attach(AdHoc::config::()) } -//#[cfg(test)] -//mod test { -// use crate::testdb; -// -// use super::start; -// use rocket::http::Status; -// use rocket::local::asynchronous::Client; -// use rocket::uri; -// use sqlx::SqlitePool; -// -// #[sqlx::test] -// fn test_not_logged_in() { -// let pool = testdb!(); -// -// let client = Client::tracked(start(pool)) -// .await -// .expect("valid rocket instance"); -// let response = client.get(uri!(super::index)).dispatch().await; -// -// assert_eq!(response.status(), Status::SeeOther); -// let location = response.headers().get("Location").next().unwrap(); -// assert_eq!(location, "/auth"); -// } -//} +#[cfg(test)] +mod test { + use rocket::{ + http::{ContentType, Status}, + local::asynchronous::Client, + }; + use sqlx::SqlitePool; + + use crate::testdb; + + #[sqlx::test] + fn test_index() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=cox&password=cox"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::Ok); + + assert!(response.into_string().await.unwrap().contains("Ausfahrten")); + } + + #[sqlx::test] + fn test_without_login() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + + let req = client.get("/"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/auth")); + } + + #[sqlx::test] + fn test_join_and_remove() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=rower&password=rower"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/join/1"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!(flash_cookie.value(), "7:successErfolgreich angemeldet!"); + + let req = client.get("/remove/1"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!(flash_cookie.value(), "7:successErfolgreich abgemeldet!"); + } + + #[sqlx::test] + fn test_join_invalid_event() { + let db = testdb!(); + + let rocket = rocket::build().manage(db.clone()); + let rocket = crate::tera::config(rocket); + + let client = Client::tracked(rocket).await.unwrap(); + let login = client + .post("/auth") + .header(ContentType::Form) // Set the content type to form + .body("name=rower&password=rower"); // Add the form data to the request body; + login.dispatch().await; + + let req = client.get("/join/9999"); + let response = req.dispatch().await; + + assert_eq!(response.status(), Status::SeeOther); + assert_eq!(response.headers().get("Location").next(), Some("/")); + + let flash_cookie = response + .cookies() + .get("_flash") + .expect("Expected flash cookie"); + + assert_eq!(flash_cookie.value(), "5:errorTrip_details do not exist."); + } +} From 6e61cce1ece428731b1807ec0f02adbeffeee56a Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 22 Jul 2023 13:10:13 +0200 Subject: [PATCH 13/15] create db tables for logbook --- README.md | 40 +++++++++++--------------------------- migration.sql | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++ seeds.sql | 13 +++++++++++++ 3 files changed, 78 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index d7e1002..d199a1f 100644 --- a/README.md +++ b/README.md @@ -6,47 +6,29 @@ ### Logbuch - Log with activities -#### Tables -- boats - - id - - name - - amount_seats -- session - - id - - ship_master (Schiffsführer) - - ship_master_only_steered (default: false) - - departure (Datetime) - - Option (Datetime) // None -> on water - - destination - - Option (prefilled from destination) - - comments - - type (normal, Wanderfahrt) -- rowers - - session_id - - user_id -- damages - - boat_id - - desc - - user_id_created - - created_at - - user_id_fixed - - fixed_at - - lock_boat (default: false), if true: noone can use this boat - ### Guest-Scheckbuch - guest_trip - guest_user_id - amount_trips - paid_to_user_id -- guest_trip_session +- guest_trip_logbook - guest_trip_id - - session_id + - logbook_id ### Bootsreservierungen - Confirmation required? - How long in advance is it possible? - Default reservations for some regular events (A+F, USI, ...)? +### Notifications +- notifcations + - id + - message + - category + - created_at + - read_at: Option + - user_id + ## Backlog (i.e. don't work on this now) ### Sync w/ nextcloud - remove most fields (names, ...) from users and add uid diff --git a/migration.sql b/migration.sql index 7fb8f45..479350d 100644 --- a/migration.sql +++ b/migration.sql @@ -65,3 +65,57 @@ CREATE TABLE IF NOT EXISTS "log" ( "created_at" text NOT NULL DEFAULT CURRENT_TIMESTAMP ); +CREATE TABLE IF NOT EXISTS "location" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" text NOT NULL +); + +CREATE TABLE IF NOT EXISTS "boat" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" text NOT NULL UNIQUE, + "amount_seats" integer NOT NULL, + "location_id" INTEGER NOT NULL REFERENCES location(id), + "owner" INTEGER REFERENCES user(id), -- null: club is owner + "year_built" INTEGER, + "boatbuilder" TEXT, + "default_shipmaster_only_steering" boolean default false not null, + "skull" boolean default true NOT NULL, -- false => riemen + "external" boolean default false NOT NULL -- false => owned by different club +); + +CREATE TABLE IF NOT EXISTS "logbook_type" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "name" text NOT NULL -- e.g. 'Wanderfahrt', 'Regatta' +); + +CREATE TABLE IF NOT EXISTS "logbook" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "boat_id" INTEGER NOT NULL REFERENCES boat(id), + "shipmaster" INTEGER NOT NULL REFERENCES user(id), -- null: club is owner + "shipmaster_only_steering" boolean not null, + "departure" text not null, + "arrival" text, -- None -> ship is on water + "destination" text, + "distance_in_km" integer, + "comments" text, + "type" INTEGER REFERENCES logbook_type(id) +); + +CREATE TABLE IF NOT EXISTS "rower" ( + "logbook_id" INTEGER NOT NULL REFERENCES logbook(id), + "rower_id" INTEGER NOT NULL REFERENCES user(id) +); + +CREATE TABLE IF NOT EXISTS "boat_damage" ( + "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, + "boat_id" INTEGER NOT NULL REFERENCES boat(id), + "desc" text not null, + "user_id_created" INTEGER NOT NULL REFERENCES user(id), + "created_at" text not null, + "user_id_fixed" INTEGER REFERENCES user(id), -- none: not fixed yet + "fixed_at" text, + "user_id_verified" INTEGER REFERENCES user(id), + "verified_at" text, + "lock_boat" boolean not null default false -- if true: noone can use the boat +); + diff --git a/seeds.sql b/seeds.sql index 7c56b80..497ac8f 100644 --- a/seeds.sql +++ b/seeds.sql @@ -15,3 +15,16 @@ INSERT INTO "trip" (cox_id, trip_details_id) VALUES(4, 2); INSERT INTO "trip_type" (name, desc, question, icon) VALUES ('Regatta', 'Regatta!', 'Kein normales Event. Das ist eine Regatta! Willst du wirklich teilnehmen?', '🏅'); INSERT INTO "trip_type" (name, desc, question, icon) VALUES ('Lange Ausfahrt', 'Lange Ausfahrt!', 'Das ist eine lange Ausfahrt! Willst du wirklich teilnehmen?', '💪'); INSERT INTO "trip_type" (name, desc, question, icon) VALUES ('Wanderfahrt', 'Wanderfahrt!', 'Kein normales Event. Das ist eine Wanderfahrt! Bitte überprüfe ob du alle Anforderungen erfüllst. Willst du wirklich teilnehmen?', '⛱'); +INSERT INTO "location" (name) VALUES ('Linz'); +INSERT INTO "location" (name) VALUES ('Ottensheim'); +INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Haichenbach', 1, 1); +INSERT INTO "boat" (name, amount_seats, location_id, owner) VALUES ('private_boat_from_rower', 1, 1, 2); +INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Joe', 2, 1); +INSERT INTO "boat" (name, amount_seats, location_id) VALUES ('Kaputtes Boot :-(', 7, 1); +INSERT INTO "logbook_type" (name) VALUES ('Wanderfahrt'); +INSERT INTO "logbook_type" (name) VALUES ('Regatta'); +INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure) VALUES (2, 2, false, '2142-12-24 10:00'); +INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (1, 4, false, '2141-12-24 10:00', '2141-12-24 15:00', 'Ottensheim', 25); +INSERT INTO "logbook" (boat_id, shipmaster, shipmaster_only_steering, departure, arrival, destination, distance_in_km) VALUES (3, 4, false, '2142-12-24 10:00', '2142-12-24 11:30', 'Ottensheim + Regattastrecke', 29); +INSERT INTO "rower" (logbook_id, rower_id) VALUES(3,3); +INSERT INTO "boat_damage" (boat_id, desc, user_id_created, created_at) VALUES(4,'Dolle bei Position 2 fehlt', 5, '2142-12-24 15:02'); From c0bb6d51dec9805792aeb0738e0f7a5cbc4d0af4 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 22 Jul 2023 13:57:17 +0200 Subject: [PATCH 14/15] create create/delete/view function for boats --- README.md | 6 +- migration.sql | 4 +- src/model/boat.rs | 232 +++++++++++++++++++++++++++ src/model/mod.rs | 1 + src/tera/admin/boat.rs | 113 +++++++++++++ src/tera/admin/mod.rs | 2 + templates/admin/boat/index.html.tera | 78 +++++++++ templates/includes/macros.html.tera | 6 + 8 files changed, 438 insertions(+), 4 deletions(-) create mode 100644 src/model/boat.rs create mode 100644 src/tera/admin/boat.rs create mode 100644 templates/admin/boat/index.html.tera diff --git a/README.md b/README.md index d199a1f..e9e966b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,9 @@ ## New large features ### Logbuch -- Log with activities +Next: +- Make boats updateable (incl. rower + location) +- Write tests for model/boat.rs ### Guest-Scheckbuch - guest_trip @@ -100,4 +102,4 @@ user_details - [ ] (delete) GET /admin/planned-event//delete - [ ] (FileServer: static/) GET /public/ [10] - [ ] (login) POST /api/login/ - +- [ ] /tera/admin/boat.rs diff --git a/migration.sql b/migration.sql index 479350d..8414ee0 100644 --- a/migration.sql +++ b/migration.sql @@ -74,7 +74,7 @@ CREATE TABLE IF NOT EXISTS "boat" ( "id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" text NOT NULL UNIQUE, "amount_seats" integer NOT NULL, - "location_id" INTEGER NOT NULL REFERENCES location(id), + "location_id" INTEGER NOT NULL REFERENCES location(id) DEFAULT 1, "owner" INTEGER REFERENCES user(id), -- null: club is owner "year_built" INTEGER, "boatbuilder" TEXT, @@ -111,7 +111,7 @@ CREATE TABLE IF NOT EXISTS "boat_damage" ( "boat_id" INTEGER NOT NULL REFERENCES boat(id), "desc" text not null, "user_id_created" INTEGER NOT NULL REFERENCES user(id), - "created_at" text not null, + "created_at" text not null default CURRENT_TIMESTAMP, "user_id_fixed" INTEGER REFERENCES user(id), -- none: not fixed yet "fixed_at" text, "user_id_verified" INTEGER REFERENCES user(id), diff --git a/src/model/boat.rs b/src/model/boat.rs new file mode 100644 index 0000000..0db76c1 --- /dev/null +++ b/src/model/boat.rs @@ -0,0 +1,232 @@ +use serde::{Deserialize, Serialize}; +use sqlx::{FromRow, SqlitePool}; + +#[derive(FromRow, Debug, Serialize, Deserialize)] +pub struct Boat { + pub id: i64, + pub name: String, + pub amount_seats: i64, + pub location_id: i64, + pub owner: Option, + pub year_built: Option, + pub boatbuilder: Option, + #[serde(default = "bool::default")] + default_shipmaster_only_steering: bool, + #[serde(default = "bool::default")] + skull: bool, + #[serde(default = "bool::default")] + external: bool, +} + +impl Boat { + pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option { + sqlx::query_as!( + Self, + " + SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external + FROM boat + WHERE id like ? + ", + id + ) + .fetch_one(db) + .await + .ok() + } + // + // pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option { + // sqlx::query_as!( + // User, + // " + //SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access + //FROM user + //WHERE name like ? + // ", + // name + // ) + // .fetch_one(db) + // .await + // .ok() + // } + // + pub async fn all(db: &SqlitePool) -> Vec { + sqlx::query_as!( + Boat, + " +SELECT id, name, amount_seats, location_id, owner, year_built, boatbuilder, default_shipmaster_only_steering, skull, external +FROM boat +ORDER BY amount_seats DESC + " + ) + .fetch_all(db) + .await + .unwrap() //TODO: fixme + } + + pub async fn create( + db: &SqlitePool, + name: &str, + amount_seats: i64, + year_built: Option, + boatbuilder: Option<&str>, + default_shipmaster_only_steering: bool, + skull: bool, + external: bool, + ) -> bool { + sqlx::query!( + "INSERT INTO boat(name, amount_seats, year_built, boatbuilder, default_shipmaster_only_steering, skull, external) VALUES (?,?,?,?,?,?,?)", + name, + amount_seats, + year_built, + boatbuilder, + default_shipmaster_only_steering, + skull, + external + ) + .execute(db) + .await + .is_ok() + } + + // pub async fn update(&self, db: &SqlitePool, is_cox: bool, is_admin: bool, is_guest: bool) { + // sqlx::query!( + // "UPDATE user SET is_cox = ?, is_admin = ?, is_guest = ? where id = ?", + // is_cox, + // is_admin, + // is_guest, + // self.id + // ) + // .execute(db) + // .await + // .unwrap(); //Okay, because we can only create a User of a valid id + // } + // + pub async fn delete(&self, db: &SqlitePool) { + sqlx::query!("DELETE FROM boat WHERE id=?", self.id) + .execute(db) + .await + .unwrap(); //Okay, because we can only create a User of a valid id + } +} + +//#[cfg(test)] +//mod test { +// use crate::testdb; +// +// use super::User; +// use sqlx::SqlitePool; +// +// #[sqlx::test] +// fn test_find_correct_id() { +// let pool = testdb!(); +// let user = User::find_by_id(&pool, 1).await.unwrap(); +// assert_eq!(user.id, 1); +// } +// +// #[sqlx::test] +// fn test_find_wrong_id() { +// let pool = testdb!(); +// let user = User::find_by_id(&pool, 1337).await; +// assert!(user.is_none()); +// } +// +// #[sqlx::test] +// fn test_find_correct_name() { +// let pool = testdb!(); +// let user = User::find_by_name(&pool, "admin".into()).await.unwrap(); +// assert_eq!(user.id, 1); +// } +// +// #[sqlx::test] +// fn test_find_wrong_name() { +// let pool = testdb!(); +// let user = User::find_by_name(&pool, "name-does-not-exist".into()).await; +// assert!(user.is_none()); +// } +// +// #[sqlx::test] +// fn test_all() { +// let pool = testdb!(); +// let res = User::all(&pool).await; +// assert!(res.len() > 3); +// } +// +// #[sqlx::test] +// fn test_succ_create() { +// let pool = testdb!(); +// +// assert_eq!( +// User::create(&pool, "new-user-name".into(), false).await, +// true +// ); +// } +// +// #[sqlx::test] +// fn test_duplicate_name_create() { +// let pool = testdb!(); +// +// assert_eq!(User::create(&pool, "admin".into(), false).await, false); +// } +// +// #[sqlx::test] +// fn test_update() { +// let pool = testdb!(); +// +// let user = User::find_by_id(&pool, 1).await.unwrap(); +// user.update(&pool, false, false, false).await; +// +// let user = User::find_by_id(&pool, 1).await.unwrap(); +// assert_eq!(user.is_admin, false); +// } +// +// #[sqlx::test] +// fn succ_login_with_test_db() { +// let pool = testdb!(); +// User::login(&pool, "admin".into(), "admin".into()) +// .await +// .unwrap(); +// } +// +// #[sqlx::test] +// fn wrong_pw() { +// let pool = testdb!(); +// assert!(User::login(&pool, "admin".into(), "admi".into()) +// .await +// .is_err()); +// } +// +// #[sqlx::test] +// fn wrong_username() { +// let pool = testdb!(); +// assert!(User::login(&pool, "admi".into(), "admin".into()) +// .await +// .is_err()); +// } +// +// #[sqlx::test] +// fn reset() { +// let pool = testdb!(); +// let user = User::find_by_id(&pool, 1).await.unwrap(); +// +// user.reset_pw(&pool).await; +// +// let user = User::find_by_id(&pool, 1).await.unwrap(); +// assert_eq!(user.pw, None); +// } +// +// #[sqlx::test] +// fn update_pw() { +// let pool = testdb!(); +// let user = User::find_by_id(&pool, 1).await.unwrap(); +// +// assert!(User::login(&pool, "admin".into(), "abc".into()) +// .await +// .is_err()); +// +// user.update_pw(&pool, "abc".into()).await; +// +// User::login(&pool, "admin".into(), "abc".into()) +// .await +// .unwrap(); +// } +//} diff --git a/src/model/mod.rs b/src/model/mod.rs index 76e92fb..6d5c012 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -7,6 +7,7 @@ use self::{ trip::{Trip, TripWithUserAndType}, }; +pub mod boat; pub mod log; pub mod planned_event; pub mod trip; diff --git a/src/tera/admin/boat.rs b/src/tera/admin/boat.rs new file mode 100644 index 0000000..c1ccc32 --- /dev/null +++ b/src/tera/admin/boat.rs @@ -0,0 +1,113 @@ +use crate::model::{boat::Boat, user::AdminUser}; +use rocket::{ + form::Form, + get, post, + request::FlashMessage, + response::{Flash, Redirect}, + routes, FromForm, Route, State, +}; +use rocket_dyn_templates::{tera::Context, Template}; +use sqlx::SqlitePool; + +#[get("/boat")] +async fn index( + db: &State, + admin: AdminUser, + flash: Option>, +) -> Template { + let boats = Boat::all(db).await; + + let mut context = Context::new(); + if let Some(msg) = flash { + context.insert("flash", &msg.into_inner()); + } + context.insert("boats", &boats); + context.insert("loggedin_user", &admin.user); + + Template::render("admin/boat/index", context.into_json()) +} + +#[get("/boat//delete")] +async fn delete(db: &State, _admin: AdminUser, boat: i32) -> Flash { + let boat = Boat::find_by_id(db, boat).await; + match boat { + Some(boat) => { + boat.delete(db).await; + Flash::success( + Redirect::to("/admin/boat"), + format!("Sucessfully deleted boat {}", boat.name), + ) + } + None => Flash::error(Redirect::to("/admin/boat"), "Boat does not exist"), + } +} + +//#[derive(FromForm)] +//struct UserEditForm { +// id: i32, +// is_guest: bool, +// is_cox: bool, +// is_admin: bool, +//} +// +//#[post("/user", data = "")] +//async fn update( +// db: &State, +// data: Form, +// _admin: AdminUser, +//) -> Flash { +// let user = User::find_by_id(db, data.id).await; +// let Some(user) = user else { +// return Flash::error( +// Redirect::to("/admin/user"), +// format!("User with ID {} does not exist!", data.id), +// ) +// }; +// +// user.update(db, data.is_cox, data.is_admin, data.is_guest) +// .await; +// +// Flash::success(Redirect::to("/admin/user"), "Successfully updated user") +//} +// +#[derive(FromForm)] +struct BoatAddForm<'r> { + name: &'r str, + amount_seats: i64, + year_built: Option, + boatbuilder: Option<&'r str>, + default_shipmaster_only_steering: bool, + skull: bool, + external: bool, +} + +#[post("/boat/new", data = "")] +async fn create( + db: &State, + data: Form>, + _admin: AdminUser, +) -> Flash { + if Boat::create( + db, + data.name, + data.amount_seats, + data.year_built, + data.boatbuilder, + data.default_shipmaster_only_steering, + data.skull, + data.external, + ) + .await + { + Flash::success(Redirect::to("/admin/boat"), "Successfully created boat") + } else { + Flash::error( + Redirect::to("/admin/boat"), + format!("Error while creating boat {} in DB", data.name), + ) + } +} + +pub fn routes() -> Vec { + routes![index, create, delete] //, update] +} diff --git a/src/tera/admin/mod.rs b/src/tera/admin/mod.rs index b0cc07d..ee4f5fb 100644 --- a/src/tera/admin/mod.rs +++ b/src/tera/admin/mod.rs @@ -3,6 +3,7 @@ use sqlx::SqlitePool; use crate::{model::log::Log, tera::Config}; +pub mod boat; pub mod planned_event; pub mod user; @@ -17,6 +18,7 @@ async fn rss(db: &State, key: Option<&str>, config: &State) pub fn routes() -> Vec { let mut ret = Vec::new(); ret.append(&mut user::routes()); + ret.append(&mut boat::routes()); ret.append(&mut planned_event::routes()); ret.append(&mut routes![rss]); ret diff --git a/templates/admin/boat/index.html.tera b/templates/admin/boat/index.html.tera new file mode 100644 index 0000000..cdf01c4 --- /dev/null +++ b/templates/admin/boat/index.html.tera @@ -0,0 +1,78 @@ +{% import "includes/macros" as macros %} + +{% extends "base" %} + +{% block content %} +
+ {% if flash %} + {{ macros::alert(message=flash.1, type=flash.0, class="sm:col-span-2 lg:col-span-3") }} + {% endif %} + +

Boats

+ +
+
+

Neues Boot hinzufügen

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ default_shipmaster_only_steering: + + +
+
+ skull: + + +
+
+ external: + + +
+
+
+
+ +
+
+ + +
+
+ {% for boat in boats %} +
+
+ +
{{ boat.name }} +
+
+ +
+{% endfor %} +
+ +
+ +{% endblock content %} diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 7566399..951aeaa 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -13,6 +13,12 @@ FAQs {% if loggedin_user.is_admin %} + + BOATS + Bootsverwaltung + + + Userverwaltung From 524d1acee27c86df3c69b1a0d36aaf67c5e61bc0 Mon Sep 17 00:00:00 2001 From: philipp Date: Sat, 22 Jul 2023 15:34:42 +0200 Subject: [PATCH 15/15] add full CRUD for boats --- src/model/boat.rs | 232 +++++++++++---------------- src/model/location.rs | 94 +++++++++++ src/model/mod.rs | 1 + src/tera/admin/boat.rs | 92 +++++++---- templates/admin/boat/index.html.tera | 15 +- templates/forms/event.html.tera | 2 +- templates/forms/trip.html.tera | 2 +- templates/includes/macros.html.tera | 10 +- 8 files changed, 277 insertions(+), 171 deletions(-) create mode 100644 src/model/location.rs diff --git a/src/model/boat.rs b/src/model/boat.rs index 0db76c1..e791c1d 100644 --- a/src/model/boat.rs +++ b/src/model/boat.rs @@ -88,19 +88,37 @@ ORDER BY amount_seats DESC .is_ok() } - // pub async fn update(&self, db: &SqlitePool, is_cox: bool, is_admin: bool, is_guest: bool) { - // sqlx::query!( - // "UPDATE user SET is_cox = ?, is_admin = ?, is_guest = ? where id = ?", - // is_cox, - // is_admin, - // is_guest, - // self.id - // ) - // .execute(db) - // .await - // .unwrap(); //Okay, because we can only create a User of a valid id - // } - // + pub async fn update( + &self, + db: &SqlitePool, + name: &str, + amount_seats: i64, + year_built: Option, + boatbuilder: Option<&str>, + default_shipmaster_only_steering: bool, + skull: bool, + external: bool, + location_id: Option, + owner: Option, + ) -> bool { + sqlx::query!( + "UPDATE boat SET name=?, amount_seats=?, year_built=?, boatbuilder=?, default_shipmaster_only_steering=?, skull=?, external=?, location_id=?, owner=? WHERE id=?", + name, + amount_seats, + year_built, + boatbuilder, + default_shipmaster_only_steering, + skull, + external, + location_id, + owner, + self.id + ) + .execute(db) + .await + .is_ok() + } + pub async fn delete(&self, db: &SqlitePool) { sqlx::query!("DELETE FROM boat WHERE id=?", self.id) .execute(db) @@ -109,124 +127,70 @@ ORDER BY amount_seats DESC } } -//#[cfg(test)] -//mod test { -// use crate::testdb; -// -// use super::User; -// use sqlx::SqlitePool; -// -// #[sqlx::test] -// fn test_find_correct_id() { -// let pool = testdb!(); -// let user = User::find_by_id(&pool, 1).await.unwrap(); -// assert_eq!(user.id, 1); -// } -// -// #[sqlx::test] -// fn test_find_wrong_id() { -// let pool = testdb!(); -// let user = User::find_by_id(&pool, 1337).await; -// assert!(user.is_none()); -// } -// -// #[sqlx::test] -// fn test_find_correct_name() { -// let pool = testdb!(); -// let user = User::find_by_name(&pool, "admin".into()).await.unwrap(); -// assert_eq!(user.id, 1); -// } -// -// #[sqlx::test] -// fn test_find_wrong_name() { -// let pool = testdb!(); -// let user = User::find_by_name(&pool, "name-does-not-exist".into()).await; -// assert!(user.is_none()); -// } -// -// #[sqlx::test] -// fn test_all() { -// let pool = testdb!(); -// let res = User::all(&pool).await; -// assert!(res.len() > 3); -// } -// -// #[sqlx::test] -// fn test_succ_create() { -// let pool = testdb!(); -// -// assert_eq!( -// User::create(&pool, "new-user-name".into(), false).await, -// true -// ); -// } -// -// #[sqlx::test] -// fn test_duplicate_name_create() { -// let pool = testdb!(); -// -// assert_eq!(User::create(&pool, "admin".into(), false).await, false); -// } -// -// #[sqlx::test] -// fn test_update() { -// let pool = testdb!(); -// -// let user = User::find_by_id(&pool, 1).await.unwrap(); -// user.update(&pool, false, false, false).await; -// -// let user = User::find_by_id(&pool, 1).await.unwrap(); -// assert_eq!(user.is_admin, false); -// } -// -// #[sqlx::test] -// fn succ_login_with_test_db() { -// let pool = testdb!(); -// User::login(&pool, "admin".into(), "admin".into()) -// .await -// .unwrap(); -// } -// -// #[sqlx::test] -// fn wrong_pw() { -// let pool = testdb!(); -// assert!(User::login(&pool, "admin".into(), "admi".into()) -// .await -// .is_err()); -// } -// -// #[sqlx::test] -// fn wrong_username() { -// let pool = testdb!(); -// assert!(User::login(&pool, "admi".into(), "admin".into()) -// .await -// .is_err()); -// } -// -// #[sqlx::test] -// fn reset() { -// let pool = testdb!(); -// let user = User::find_by_id(&pool, 1).await.unwrap(); -// -// user.reset_pw(&pool).await; -// -// let user = User::find_by_id(&pool, 1).await.unwrap(); -// assert_eq!(user.pw, None); -// } -// -// #[sqlx::test] -// fn update_pw() { -// let pool = testdb!(); -// let user = User::find_by_id(&pool, 1).await.unwrap(); -// -// assert!(User::login(&pool, "admin".into(), "abc".into()) -// .await -// .is_err()); -// -// user.update_pw(&pool, "abc".into()).await; -// -// User::login(&pool, "admin".into(), "abc".into()) -// .await -// .unwrap(); -// } -//} +#[cfg(test)] +mod test { + use crate::{model::boat::Boat, testdb}; + + use sqlx::SqlitePool; + + #[sqlx::test] + fn test_find_correct_id() { + let pool = testdb!(); + let boat = Boat::find_by_id(&pool, 1).await.unwrap(); + assert_eq!(boat.id, 1); + } + + #[sqlx::test] + fn test_find_wrong_id() { + let pool = testdb!(); + let boat = Boat::find_by_id(&pool, 1337).await; + assert!(boat.is_none()); + } + + #[sqlx::test] + fn test_all() { + let pool = testdb!(); + let res = Boat::all(&pool).await; + assert!(res.len() > 3); + } + + #[sqlx::test] + fn test_succ_create() { + let pool = testdb!(); + + assert_eq!( + Boat::create( + &pool, + "new-boat-name".into(), + 42, + None, + "Best Boatbuilder".into(), + true, + true, + false + ) + .await, + true + ); + } + + #[sqlx::test] + fn test_duplicate_name_create() { + let pool = testdb!(); + + assert_eq!( + Boat::create( + &pool, + "Haichenbach".into(), + 42, + None, + "Best Boatbuilder".into(), + true, + true, + false + ) + .await, + false + ); + } +} diff --git a/src/model/location.rs b/src/model/location.rs new file mode 100644 index 0000000..b0e69c3 --- /dev/null +++ b/src/model/location.rs @@ -0,0 +1,94 @@ +use serde::{Deserialize, Serialize}; +use sqlx::{FromRow, SqlitePool}; + +#[derive(FromRow, Debug, Serialize, Deserialize)] +pub struct Location { + pub id: i64, + pub name: String, +} + +impl Location { + pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option { + sqlx::query_as!( + Self, + " + SELECT id, name + FROM location + WHERE id like ? + ", + id + ) + .fetch_one(db) + .await + .ok() + } + + pub async fn all(db: &SqlitePool) -> Vec { + sqlx::query_as!( + Self, + " +SELECT id, name +FROM location + " + ) + .fetch_all(db) + .await + .unwrap() //TODO: fixme + } + + pub async fn create(db: &SqlitePool, name: &str) -> bool { + sqlx::query!("INSERT INTO location(name) VALUES (?)", name,) + .execute(db) + .await + .is_ok() + } + + pub async fn delete(&self, db: &SqlitePool) { + sqlx::query!("DELETE FROM location WHERE id=?", self.id) + .execute(db) + .await + .unwrap(); //Okay, because we can only create a Location of a valid id + } +} + +#[cfg(test)] +mod test { + use crate::{model::location::Location, testdb}; + + use sqlx::SqlitePool; + + #[sqlx::test] + fn test_find_correct_id() { + let pool = testdb!(); + let location = Location::find_by_id(&pool, 1).await.unwrap(); + assert_eq!(location.id, 1); + } + + #[sqlx::test] + fn test_find_wrong_id() { + let pool = testdb!(); + let location = Location::find_by_id(&pool, 1337).await; + assert!(location.is_none()); + } + + #[sqlx::test] + fn test_all() { + let pool = testdb!(); + let res = Location::all(&pool).await; + assert!(res.len() > 1); + } + + #[sqlx::test] + fn test_succ_create() { + let pool = testdb!(); + + assert_eq!(Location::create(&pool, "new-loc-name".into(),).await, true); + } + + #[sqlx::test] + fn test_duplicate_name_create() { + let pool = testdb!(); + + assert_eq!(Location::create(&pool, "Linz".into(),).await, false); + } +} diff --git a/src/model/mod.rs b/src/model/mod.rs index 6d5c012..9500984 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -8,6 +8,7 @@ use self::{ }; pub mod boat; +pub mod location; pub mod log; pub mod planned_event; pub mod trip; diff --git a/src/tera/admin/boat.rs b/src/tera/admin/boat.rs index c1ccc32..89af9e1 100644 --- a/src/tera/admin/boat.rs +++ b/src/tera/admin/boat.rs @@ -1,4 +1,8 @@ -use crate::model::{boat::Boat, user::AdminUser}; +use crate::model::{ + boat::Boat, + location::Location, + user::{AdminUser, User}, +}; use rocket::{ form::Form, get, post, @@ -16,12 +20,16 @@ async fn index( flash: Option>, ) -> Template { let boats = Boat::all(db).await; + let locations = Location::all(db).await; + let users = User::all(db).await; let mut context = Context::new(); if let Some(msg) = flash { context.insert("flash", &msg.into_inner()); } context.insert("boats", &boats); + context.insert("locations", &locations); + context.insert("users", &users); context.insert("loggedin_user", &admin.user); Template::render("admin/boat/index", context.into_json()) @@ -42,34 +50,58 @@ async fn delete(db: &State, _admin: AdminUser, boat: i32) -> Flash, -// data: Form, -// _admin: AdminUser, -//) -> Flash { -// let user = User::find_by_id(db, data.id).await; -// let Some(user) = user else { -// return Flash::error( -// Redirect::to("/admin/user"), -// format!("User with ID {} does not exist!", data.id), -// ) -// }; -// -// user.update(db, data.is_cox, data.is_admin, data.is_guest) -// .await; -// -// Flash::success(Redirect::to("/admin/user"), "Successfully updated user") -//} -// +#[derive(FromForm)] +struct BoatEditForm<'r> { + id: i32, + name: &'r str, + amount_seats: i64, + year_built: Option, + boatbuilder: Option<&'r str>, + default_shipmaster_only_steering: bool, + skull: bool, + external: bool, + location_id: Option, + owner: Option, +} + +#[post("/boat", data = "")] +async fn update( + db: &State, + data: Form>, + _admin: AdminUser, +) -> Flash { + let boat = Boat::find_by_id(db, data.id).await; + let Some(boat) = boat else { + return Flash::error( + Redirect::to("/admin/boat"), + format!("Boat with ID {} does not exist!", data.id), + ) + }; + + if !boat + .update( + db, + data.name, + data.amount_seats, + data.year_built, + data.boatbuilder, + data.default_shipmaster_only_steering, + data.skull, + data.external, + data.location_id, + data.owner, + ) + .await + { + return Flash::error( + Redirect::to("/admin/boat"), + format!("Boat with ID {} could not be updated!", data.id), + ); + } + + Flash::success(Redirect::to("/admin/boat"), "Successfully updated boat") +} + #[derive(FromForm)] struct BoatAddForm<'r> { name: &'r str, @@ -109,5 +141,5 @@ async fn create( } pub fn routes() -> Vec { - routes![index, create, delete] //, update] + routes![index, create, delete, update] } diff --git a/templates/admin/boat/index.html.tera b/templates/admin/boat/index.html.tera index cdf01c4..1beda21 100644 --- a/templates/admin/boat/index.html.tera +++ b/templates/admin/boat/index.html.tera @@ -59,7 +59,20 @@
-
{{ boat.name }} +
{{ boat.name }}
+
+
+ + + {{ macros::input(label='Name', name='name', type='text', value=boat.name) }} + {{ macros::input(label='Amount Seats', name='amount_seats', type='number', min=0, value=boat.amount_seats) }} + {{ macros::select(data=locations, label='location', select_name='location_id', selected_id=boat.location_id) }} + {{ macros::select(data=users, label='users', select_name='owner', selected_id=boat.owner, default="Vereinsboot") }} + {{ macros::input(label='Baujahr', name='year_built', type='number', min=1950, value=boat.year_built) }} + {{ macros::input(label='Bootsbauer', name='boatbuilder', type='text', value=boat.boatbuilder) }} + {{ macros::checkbox(label='default_shipmaster_only_steering', name='default_shipmaster_only_steering', id=loop.index , checked=boat.default_shipmaster_only_steering) }} + {{ macros::checkbox(label='skull', name='skull', id=loop.index , checked=boat.skull) }} + {{ macros::checkbox(label='external', name='external', id=loop.index , checked=boat.external) }}
diff --git a/templates/forms/event.html.tera b/templates/forms/event.html.tera index 495dc39..6fd0450 100644 --- a/templates/forms/event.html.tera +++ b/templates/forms/event.html.tera @@ -10,7 +10,7 @@ {{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }} {{ macros::checkbox(label='Immer anzeigen', name='always_show') }} {{ macros::input(label='Anmerkungen', name='notes', type='input') }} - {{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }} + {{ macros::select(data=trip_types, select_name='trip_type', default='Reguläre Ausfahrt') }} diff --git a/templates/forms/trip.html.tera b/templates/forms/trip.html.tera index 742a792..8b7d0b5 100644 --- a/templates/forms/trip.html.tera +++ b/templates/forms/trip.html.tera @@ -7,7 +7,7 @@ {{ macros::input(label='Anzahl Ruderer (ohne Steuerperson)', name='max_people', type='number', required=true, min='0') }} {{ macros::checkbox(label='Gäste erlauben', name='allow_guests') }} {{ macros::input(label='Anmerkungen', name='notes', type='input') }} - {{ macros::select(select_name='trip_type', trip_types=trip_types, default='Reguläre Ausfahrt') }} + {{ macros::select(data=trip_types, select_name='trip_type', default='Reguläre Ausfahrt') }} diff --git a/templates/includes/macros.html.tera b/templates/includes/macros.html.tera index 951aeaa..9189382 100644 --- a/templates/includes/macros.html.tera +++ b/templates/includes/macros.html.tera @@ -47,11 +47,13 @@ {% endmacro checkbox %} -{% macro select(trip_types, select_name='trip_type', default='', selected_id='') %} +{% macro select(data, select_name='trip_type', default='', selected_id='') %} {% endmacro select %}