-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature-friend-request #20
base: main
Are you sure you want to change the base?
Changes from all commits
16937c3
415d57b
8b457ac
cdd6bf7
7b60629
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE friend_requests; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE friend_requests ( | ||
id SERIAL, | ||
sender_username text, | ||
recipient_username text, | ||
request_state text, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using postgres Enums for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could just be |
||
msg text, | ||
creation_timestamp timestamptz, | ||
PRIMARY KEY (id) | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
(ns chaat.db.friend-request | ||
(:require | ||
[next.jdbc :as jdbc] | ||
[next.jdbc.sql :as sql] | ||
[chaat.handler.errors :refer [error-table]] | ||
[chaat.db.user :as user] | ||
[java-time.api :as jt])) | ||
|
||
;; Will be more logical to have the user-exists? and exists? check in the model layer. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a tradeoff here.
|
||
;; Same for the friend request exists? check | ||
(defn exists? | ||
"Check if friend request id exists in friend_requests table" | ||
[connection id] | ||
(not (empty? (sql/find-by-keys connection :friend_requests {:id id})))) | ||
|
||
(defn insert | ||
"Insert a friend request into the db" | ||
[db {:keys [sender_username recipient_username] :as content}] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's usually worth doing the hypen <> underscore translation at the db boundary to make the rest of your clojure code consistent. |
||
(jdbc/with-transaction [tx (db)] | ||
(if (and (user/user-exists? tx sender_username) | ||
(user/user-exists? tx recipient_username)) | ||
(let [query-result (sql/insert! tx :friend_requests content)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to handle what happens when the insert fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The let block might be superfluous if we're not handling those errors. We could just inline |
||
{:result query-result :error nil}) | ||
{:result nil :error (:username-not-exists error-table)}))) | ||
|
||
(defn accept | ||
[db id] | ||
(jdbc/with-transaction [tx (db)] | ||
(if (exists? tx id) | ||
(let [query-result (sql/update! (db) :friend_requests {:request_state "accepted"} {:id id}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use |
||
update-count (:next.jdbc/update-count query-result)] | ||
(if (= 1 update-count) | ||
{:result id :error nil} | ||
{:result nil :error (:db-state-error error-table)})) | ||
{:result nil :error (:friend-request-not-exists error-table)}))) | ||
|
||
(defn reject | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract the common code out into an |
||
[db id] | ||
(jdbc/with-transaction [tx (db)] | ||
(if (exists? tx id) | ||
(let [query-result (sql/update! (db) :friend_requests {:request_state "rejected"} {:id id}) | ||
update-count (:next.jdbc/update-count query-result)] | ||
(if (= 1 update-count) | ||
{:result id :error nil} | ||
{:result nil :error (:db-state-error error-table)})) | ||
{:result nil :error (:friend-request-not-exists error-table)}))) | ||
|
||
(comment | ||
(def db (:db chaat.app/chaat-system)) | ||
(chaat.model.user/create db "shahn" "12345678") | ||
(chaat.model.user/create db "neena" "12345678") | ||
(def sample {:sender_username "shahn" | ||
:recipient_username "neena" | ||
:request_state "pending" | ||
:msg "hola" | ||
:creation_timestamp (jt/instant)}) | ||
(insert db sample) | ||
(sql/update! (db) :friend_requests {:request_state "accepted"} {:id 0}) | ||
(accept db 1) | ||
(reject db 1)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
(ns chaat.handler.handler | ||
(:require [ring.util.response :as res] | ||
[chaat.model.user :as model.user] | ||
[chaat.model.friend-request :as friend-request] | ||
[java-time.api :as jt] | ||
[chaat.handler.validation :as handler.validation] | ||
[chaat.db.utils :as db.utils] | ||
|
@@ -61,6 +62,29 @@ | |
(model.user/delete db username))] | ||
(send-response result))) | ||
|
||
;; need handler layer validation | ||
(defn create-friend-request | ||
[db {:keys [params] :as request}] | ||
(let [username (:username params) | ||
content (select-keys params [:username :recipient :message]) | ||
result (until-err-> (is-auth-user request username) | ||
(friend-request/create db content))] | ||
(send-response result))) | ||
|
||
;; need authentication | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And authorization – Shahn |
||
(defn accept-friend-request | ||
[db {:keys [route-params]}] | ||
(let [id (Integer/parseInt (:id route-params)) | ||
result (friend-request/accept db id)] | ||
(send-response result))) | ||
|
||
;; need authentication | ||
(defn reject-friend-request | ||
[db {:keys [route-params]}] | ||
(let [id (Integer/parseInt (:id route-params)) | ||
result (friend-request/reject db id)] | ||
(send-response result))) | ||
|
||
(defn test-page | ||
"Display the request made to this endpoint for debugging purposes" | ||
[request] | ||
|
@@ -73,5 +97,8 @@ | |
|
||
(comment | ||
(def db (:db chaat.app/chaat-system)) | ||
(is-auth-user {} "shahn") | ||
(delete-user)) | ||
(def params {:username "shahn" | ||
:recipient "neena" | ||
:message "hola"}) | ||
(def request {:params params}) | ||
(create-friend-request db request)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
(ns chaat.model.friend-request | ||
(:require | ||
[chaat.db.friend-request :as db.friend-request] | ||
[java-time.api :as jt])) | ||
|
||
|
||
(defn gen-friend-request | ||
[{:keys [username recipient message]}] | ||
{:sender_username username | ||
:recipient_username recipient | ||
:request_state "pending" | ||
:msg message | ||
:creation_timestamp (jt/instant)}) | ||
|
||
(defn create | ||
[db content] | ||
(let [;; put model layer validation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You may want to keep validations at your boundaries. |
||
friend-request (gen-friend-request content) | ||
result (db.friend-request/insert db friend-request)] | ||
result)) | ||
|
||
(defn accept | ||
[db id] | ||
(let [;; put model layer validation | ||
result (db.friend-request/accept db id)] | ||
result)) | ||
|
||
(defn reject | ||
[db id] | ||
(let [;; put model layer validation | ||
result (db.friend-request/reject db id)] | ||
result)) | ||
|
||
(comment | ||
(def db (:db chaat.app/chaat-system)) | ||
(def content {:username "shahn" :recipient "neena" :message "hello"}) | ||
(create db content)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use foreign keys to ensure consistency.