Skip to content

Commit

Permalink
Merge pull request #17 from Favo02/feature/university
Browse files Browse the repository at this point in the history
Add university to toilets
  • Loading branch information
Favo02 authored Apr 23, 2024
2 parents 26be885 + f8f5c96 commit d4d718c
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 56 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- add university edition branding
- round average rating (issue #15)
- dynamically fetch know issues
- add university field to toilets

## Changelog init (Version 1.0)

Expand Down
5 changes: 4 additions & 1 deletion backend/src/handlers/login.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ module Query = struct

let find_user =
string ->? tup2 string string @@
"SELECT id, password FROM users WHERE username = ?"
"SELECT
u.id, u.password
FROM cessadvisor.users u
WHERE u.username = ?"
end

module Models = struct
Expand Down
31 changes: 16 additions & 15 deletions backend/src/handlers/reviews.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ module Query = struct

let get_all =
unit ->* tup3
(tup2 (tup3 string string string) (tup2 string string))
(tup2 (tup3 string string string) (tup3 string string string))
(tup3 string string string)
(tup2 (tup4 int string bool bool) (tup4 bool bool int int)) @@
"SELECT
r.id, t.id AS toilet_id, t.title, t.place, t.building,
r.id, t.id AS toilet_id, t.title, t.university, t.place, t.building,
r.author AS author_id, u.username AS author_name, r.date,
r.rating, r.description, r.paper, r.soap, r.dryer, r.hotwater, r.clean, r.temperature
FROM reviews r
INNER JOIN toilets t ON r.toilet = t.id
INNER JOIN users u ON r.author = u.id
FROM cessadvisor.reviews r
INNER JOIN cessadvisor.toilets t ON r.toilet = t.id
INNER JOIN cessadvisor.users u ON r.author = u.id
ORDER BY date DESC"

let create =
Expand All @@ -24,22 +24,22 @@ module Query = struct
(tup4 bool bool bool bool)
(tup2 int int)
->. unit @@
"INSERT INTO reviews
"INSERT INTO cessadvisor.reviews
(author, toilet, rating, description, paper, soap, dryer, hotwater, clean, temperature)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"

let get_by_toilet =
string ->* tup3
(tup2 (tup3 string string string) (tup2 string string))
(tup2 (tup3 string string string) (tup3 string string string))
(tup3 string string string)
(tup2 (tup4 int string bool bool) (tup4 bool bool int int)) @@
"SELECT
r.id, t.id AS toilet_id, t.title, t.place, t.building,
r.id, t.id AS toilet_id, t.title, t.university, t.place, t.building,
r.author AS author_id, u.username AS author_name, r.date,
r.rating, r.description, r.paper, r.soap, r.dryer, r.hotwater, r.clean, r.temperature
FROM reviews r
INNER JOIN toilets t ON r.toilet = t.id
INNER JOIN users u ON r.author = u.id
FROM cessadvisor.reviews r
INNER JOIN cessadvisor.toilets t ON r.toilet = t.id
INNER JOIN cessadvisor.users u ON r.author = u.id
WHERE t.id = ?"
end

Expand All @@ -48,6 +48,7 @@ module Models = struct
id : string;
toilet_id : string;
title : string;
university : string;
place : string;
building : string;
author_id : string;
Expand Down Expand Up @@ -87,11 +88,11 @@ let get_all _ =
let logic () =
let%lwt toilets = DB.collect Q.get_all () in
List.map (fun (
((id, toilet_id, title), (place, building)),
((id, toilet_id, title), (university, place, building)),
(author_id, author_name, date),
((rating, description, paper, soap), (dryer, hotwater, clean, temperature))
) -> M.yojson_of_get M.({
id; toilet_id; title; place; building;
id; toilet_id; title; university; place; building;
author_id; author_name; date;
rating; description; paper; soap; dryer; hotwater; clean; temperature
})) toilets
Expand Down Expand Up @@ -122,11 +123,11 @@ let get_by_toilet req =
let logic toilet_id =
let%lwt toilets = DB.collect Q.get_by_toilet toilet_id in
List.map (fun (
((id, toilet_id, title), (place, building)),
((id, toilet_id, title), (university, place, building)),
(author_id, author_name, date),
((rating, description, paper, soap), (dryer, hotwater, clean, temperature))
) -> M.yojson_of_get M.({
id; toilet_id; title; place; building;
id; toilet_id; title; university; place; building;
author_id; author_name; date;
rating; description; paper; soap; dryer; hotwater; clean; temperature
})) toilets
Expand Down
50 changes: 26 additions & 24 deletions backend/src/handlers/toilets.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ module Query = struct
string ->! tup3
(tup4 string string string string)
(tup4 string string string string)
(tup2 (option float) int) @@
(tup3 string (option float) int) @@
"SELECT
t.id, u.username AS creator_name, t.creator AS creator_id, t.creation AS creation_date,
t.title, t.building, t.place, t.description,
t.title, t.university, t.building, t.place, t.description,
AVG(r.rating) AS rating, COUNT(r.rating) as reviews_count
FROM toilets t
INNER JOIN users u ON t.creator = u.id
LEFT JOIN reviews r ON t.id = r.toilet
FROM cessadvisor.toilets t
INNER JOIN cessadvisor.users u ON t.creator = u.id
LEFT JOIN cessadvisor.reviews r ON t.id = r.toilet
WHERE t.id = ?
GROUP BY t.id, u.username"

let get_all =
unit ->* tup3
(tup4 string string string string)
(tup4 string string string string)
(tup2 (option float) int) @@
(tup3 string (option float) int) @@
"SELECT
t.id, u.username AS creator_name, t.creator AS creator_id, t.creation AS creation_date,
t.title, t.building, t.place, t.description,
t.title, t.university, t.building, t.place, t.description,
AVG(r.rating) AS rating, COUNT(r.rating) as reviews_count
FROM toilets t
INNER JOIN users u ON t.creator = u.id
LEFT JOIN reviews r ON t.id = r.toilet
FROM cessadvisor.toilets t
INNER JOIN cessadvisor.users u ON t.creator = u.id
LEFT JOIN cessadvisor.reviews r ON t.id = r.toilet
GROUP BY t.id, u.username
ORDER BY reviews_count DESC"

let create =
tup2
(tup3 string string string)
(tup2 string string)
(tup3 string string string)
->. unit @@
"INSERT INTO toilets
(creator, title, building, place, description)
VALUES (?, ?, ?, ?, ?)"
"INSERT INTO cessadvisor.toilets
(creator, title, university, building, place, description)
VALUES (?, ?, ?, ?, ?, ?)"
end

module Models = struct
Expand All @@ -51,6 +51,7 @@ module Models = struct
creator_id : string;
creation_date : string;
title : string;
university : string;
building : string;
place : string;
description : string;
Expand All @@ -60,6 +61,7 @@ module Models = struct

type create = {
title : string; [@regex "^[\t\n\x20-\xFF]{6,50}$"]
university : string; [@regex "^[\t\n\x20-\xFF]{6,50}$"]
building : string; [@regex "^[\t\n\x20-\xFF]{6,50}$"]
place : string; [@regex "^[\t\n\x20-\xFF]{6,50}$"]
description : string; [@regex "^[\t\n\x20-\xFF]{6,250}$"]
Expand All @@ -77,13 +79,13 @@ let get req =
let logic toilet_id =
let%lwt
(id, creator_name, creator_id, creation_date),
(title, building, place, description),
(rating, reviews_count)
(title, university, building, place),
(description, rating, reviews_count)
= DB.find Q.get toilet_id in
M.yojson_of_get M.({
id; creator_name; creator_id; creation_date;
title; building; place; description;
rating; reviews_count;
title; university; building; place;
description; rating; reviews_count;
})
|> return_json 200
in try%lwt
Expand All @@ -96,12 +98,12 @@ let get_all _ =
let%lwt toilets = DB.collect Q.get_all () in
List.map (fun (
(id, creator_name, creator_id, creation_date),
(title, building, place, description),
(rating, reviews_count)
(title, university, building, place),
(description, rating, reviews_count)
) -> M.yojson_of_get M.({
id; creator_name; creator_id; creation_date;
title; building; place; description;
rating; reviews_count;
title; university; building; place;
description; rating; reviews_count;
})) toilets
|> return_json_list 200
in try%lwt
Expand All @@ -114,8 +116,8 @@ let create req =
| None -> error 400 "invalid request" "no session id found"
| Some creator ->
let%lwt () = DB.exec Q.create (
(creator, json.title, json.building),
(json.place, json.description)
(creator, json.title, json.university),
(json.building, json.place, json.description)
) in
return 200 [("message", "toilet created")]
in try%lwt
Expand Down
10 changes: 5 additions & 5 deletions backend/src/handlers/users.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ module Query = struct
COUNT(DISTINCT t.id) AS toilets_created,
COUNT(DISTINCT r.id) AS reviews_authored,
AVG(r.rating) AS average_review_rating
FROM users u
LEFT JOIN toilets t ON u.id = t.creator
LEFT JOIN reviews r ON u.id = r.author
FROM cessadvisor.users u
LEFT JOIN cessadvisor.toilets t ON u.id = t.creator
LEFT JOIN cessadvisor.reviews r ON u.id = r.author
WHERE u.id = ?
GROUP BY u.id, u.username"

let check_username_free =
string ->! bool @@
"SELECT EXISTS (SELECT * FROM users WHERE username = ?)"
"SELECT EXISTS (SELECT * FROM cessadvisor.users u WHERE u.username = ?)"

let create_user =
tup2 string string ->! string @@
"INSERT INTO users (username, password) VALUES (?, ?) RETURNING id"
"INSERT INTO cessadvisor.users (username, password) VALUES (?, ?) RETURNING id"
end

module Models = struct
Expand Down
6 changes: 3 additions & 3 deletions backend/src/handlers/various.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ module Query = struct
let stats =
unit ->! tup3 int int int @@
"SELECT
(SELECT COUNT(*) FROM users) AS users,
(SELECT COUNT(*) FROM toilets) AS toilets,
(SELECT COUNT(*) FROM reviews) AS reviews"
(SELECT COUNT(*) FROM cessadvisor.users) AS users,
(SELECT COUNT(*) FROM cessadvisor.toilets) AS toilets,
(SELECT COUNT(*) FROM cessadvisor.reviews) AS reviews"
end

module Models = struct
Expand Down
1 change: 1 addition & 0 deletions database/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CREATE TABLE toilets (
creator uuid NOT NULL REFERENCES users(id),
creation TIMESTAMPTZ NOT NULL DEFAULT now(),
title TEXT NOT NULL CHECK (title ~ '^[\t\n\x20-\xFF]{6,50}$'),
university TEXT NOT NULL CHECK (university ~ '^[\t\n\x20-\xFF]{6,50}$'),
building TEXT NOT NULL CHECK (building ~ '^[\t\n\x20-\xFF]{6,50}$'),
place TEXT NOT NULL CHECK (place ~ '^[\t\n\x20-\xFF]{6,50}$'),
description TEXT NOT NULL CHECK (description ~ '^[\t\n\x20-\xFF]{6,250}$')
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/routes/reviews/+page.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frontend/src/routes/reviews/reviewCard.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/src/routes/toilets/+page.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions frontend/src/routes/toilets/new/+page.server.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 14 additions & 2 deletions frontend/src/routes/toilets/new/+page.svelte

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d4d718c

Please sign in to comment.