-
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?
Conversation
@@ -0,0 +1,9 @@ | |||
CREATE TABLE friend_requests ( | |||
id SERIAL, | |||
sender_username text, |
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.
id SERIAL, | ||
sender_username text, | ||
recipient_username text, | ||
request_state text, |
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.
Consider using postgres Enums for this.
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.
This could just be state
[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 comment
The reason will be displayed to describe this comment to others. Learn more.
There's a tradeoff here.
- Putting it in the model layer means you're able to visualize the flow better at the correct layer. Putting it in the db layer lets you use db features to do this better.
- If you're doing the check in the model layer, you'll have to use a transaction. Otherwise, you'll have to check again in the db layer anyway (or implicitly, it will fail the uniqueness check which you should handle)
- Just relying on the uniqueness check, will allow you to skip any explicit checks entirely. The downside to this is that it isn't clear from just the code that there is a uniqueness guarantee. However, this may be okay since any complex enough system is relying on the db for a variety of guarantees. It depends on how much you want to tie your app to DB features. The more you rely on them, the more valuable the dependency (but also the more dependent you are and moving away will be harder).
|
||
(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 comment
The reason will be displayed to describe this comment to others. Learn more.
content
could be renamed to friend-request
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.
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 comment
The 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 comment
The 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 (sql/insert! tx :friend_requests content)
[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 comment
The reason will be displayed to describe this comment to others. Learn more.
This should use tx
{: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 comment
The reason will be displayed to describe this comment to others. Learn more.
Extract the common code out into an update-request-state
(friend-request/create db content))] | ||
(send-response result))) | ||
|
||
;; need authentication |
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.
And authorization – Shahn
|
||
(defn create | ||
[db content] | ||
(let [;; put model layer validation |
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.
You may want to keep validations at your boundaries.
Thank you Neena. We just discussed most of your comments. Also, since chaat development is now parked, I will not continue the discussion here. |
Story: As a user I should be able to send friend requests to other users#3