make it so you can only request a hug from a non connected person

This commit is contained in:
mcbloch 2019-06-09 23:29:33 +02:00
parent f4999fae6b
commit 1762d5970c
5 changed files with 59 additions and 40 deletions

View file

@ -105,7 +105,7 @@
<div class="card">
<header class="card-header">
<p class="card-header-title">
Your cuddles
Your hugs
</p>
</header>
@ -114,15 +114,13 @@
<table class="table">
<thead>
<tr>
<th>Person 1</th>
<th>Person 2</th>
<th>With</th>
</tr>
</thead>
<tbody>
{% for relation in user-relations %}
<tr>
<td>{{relation.name}}</td>
<td>{{relation.name_2}}</td>
<td>{{relation.other_name}}</td>
</tr>
{% endfor %}
</tbody>
@ -147,14 +145,14 @@
<div class="select">
<select name="to_id" id="to_id">
<option hidden disabled selected value> -- select an option -- </option>
{% for user in non_requested_users %}
{% for user in non_connected_users %}
<option value="{{user.id}}">{{user.name}}</option>
{% endfor %}
</select>
</div>
</div>
<div class="control">
<input class="button is-link" type="submit" value="Request cuddle!">
<input class="button is-link" type="submit" value="Request hug!">
</div>
</div>
</form>
@ -283,9 +281,9 @@
</table>
</div>
<div class="column">
<h3 class="title is-3">Cozy cuddles</h3>
<h3 class="title is-3">Cozy hugs</h3>
<div>
<h3 class="subtitle is-4">Add cuddle</h3>
<h3 class="subtitle is-4">Add hug</h3>
<form action="/relations" method="post">
{% csrf-field %}
<div class="field">
@ -319,15 +317,15 @@
<table class="table">
<thead>
<tr>
<th>Person 1</th>
<th>Person 2</th>
<th>From</th>
<th>To</th>
</tr>
</thead>
<tbody>
{% for relation in relations %}
<tr>
<td>{{relation.name}}</td>
<td>{{relation.name_2}}</td>
<td>{{relation.from_name}}</td>
<td>{{relation.to_name}}</td>
</tr>
{% endfor %}
</tbody>

View file

@ -28,6 +28,6 @@ body {
}
#view {
height: 600px;
height: 550px;
max-height: 100%;
}

View file

@ -34,7 +34,7 @@ VALUES (:from_id, :to_id)
-- :name get-relations :? :*
-- :doc retrieves all relations
SELECT * FROM relations
SELECT u_from.name from_name, u_to.name to_name, from_id, to_id FROM relations
JOIN users u_from on relations.from_id = u_from.id
JOIN users u_to on relations.to_id = u_to.id
@ -68,6 +68,22 @@ WHERE from_id = :from_id
-- :name get-relation-requests-to-user :? :*
-- :doc retrieves all relations requests send to a user
SELECT rr.id as rr_id, rr.status, u_from.name as from_name FROM relation_requests as rr
SELECT rr.id as rr_id, rr.status, u_from.name as from_name, u_from.id as from_id FROM relation_requests as rr
JOIN users u_from on rr.from_id = u_from.id
WHERE to_id = :to_id
/*
COMPLEX QUERIES
*/
-- :name get-connection-existence :? :1
-- :doc retrieves all relations-requests or relations between 2 users
SELECT count(*) count
FROM(
SELECT rr.from_id FROM relation_requests rr WHERE rr.from_id = :user_id and rr.to_id = :other_id
UNION ALL
SELECT rr.to_id FROM relation_requests rr WHERE rr.from_id = :other_id and rr.to_id = :user_id
UNION ALL
SELECT rel.from_id FROM relations rel WHERE rel.from_id = :user_id and rel.to_id = :other_id
UNION ALL
SELECT rel.to_id FROM relations rel WHERE rel.from_id = :other_id and rel.to_id = :user_id) x

View file

@ -20,7 +20,7 @@
(GET "/relations_zeroed" [] (show-relations)))
(defroutes user-routes
(POST "/relation_request/:id/status" [id & body] (update-relationrequest-status id body)) ; STATUS ENUM: (open, accepted, rejected)
(POST "/relation_request/:id/status" [id & body :as req] (update-relationrequest-status id body req)) ; STATUS ENUM: (open, accepted, rejected)
(POST "/request_relation" req (create-relation-request req)))
(defroutes oauth-routes

View file

@ -15,11 +15,6 @@
(defn- home-page [params]
(layout/render "home.html" params))
(defn- get-relations []
(map
(fn [relation] (select-keys relation [:name :name_2]))
(db/get-relations)))
(defn- get-users []
(db/get-users))
@ -30,33 +25,43 @@
(defn show-home [req]
(let [users (get-users)
relations (get-relations)
user (-> (get-in req [:session :user]))
relations (db/get-relations)
user (get-in req [:session :user])
user-relations (when user
(seq (filter (fn [rel]
;; This can probably be compacted to one reduce operation
;; -> filter for only our relations, remove our name
(->> relations
(filter (fn [rel]
(or
(= (:name rel) (:name user))
(= (:name_2 rel) (:name user))))
relations)))
(= (:from_name rel) (:name user))
(= (:to_name rel) (:name user)))))
(map (fn [rel] (if (= (:from_name rel) (:name user))
(-> rel
(assoc :other_name (:to_name rel))
(assoc :other_id (:to_id rel)))
(-> rel
(assoc :other_name (:from_name rel))
(assoc :other_id (:from_id rel))))))))
other_users (when user
(seq (filter (fn [usr] (not (= (:id usr) (:id user))))
users)))
rel-requests-out (seq (db/get-relation-requests-from-user {:from_id (:id user)}))
rel-requests-in (seq (db/get-relation-requests-to-user {:to_id (:id user)}))
non_requested_users (seq (filter (fn [other-user] (not (some (partial = (:id other-user)) (map :to_id rel-requests-out)))) other_users))]
(filter (fn [usr] (not (= (:id usr) (:id user))))
users))
rel-requests-out (db/get-relation-requests-from-user {:from_id (:id user)})
rel-requests-in (db/get-relation-requests-to-user {:to_id (:id user)})
;; This can be done in one SQL query but since we already have the data for the other operations...
non_connected_users (filter (fn [other_user] (not (some (partial = (:id other_user))
(concat
(map :from_id rel-requests-in)
(map :to_id rel-requests-out)
(map :other_id user-relations)))))
other_users)]
(log/debug (str "Session: " (:session req)))
;(log/info (str "Relation requests: \n OUTGOING: " rel-requests-out "\n INCOMING: " rel-requests-in))
;(log/info (str "User relations: " user-relations))
;(log/info (str "Other Users: " other_users))
;(log/info (str "rel reqs out: " rel-requests-out))
;(log/info (str "rel reqs out id: " (seq (map :to_id rel-requests-out))))
(home-page {:relations relations
:users users
:user user
:user-relations user-relations
:rel-requests-out rel-requests-out
:rel-requests-in rel-requests-in
:non_requested_users non_requested_users
:non_connected_users non_connected_users
:flash (:flash req)})))
(defn show-relations