Skip to content
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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

feature-friend-request #20

wants to merge 5 commits into from

Conversation

shahn95
Copy link
Contributor

@shahn95 shahn95 commented Jul 28, 2023

@shahn95 shahn95 marked this pull request as draft July 28, 2023 11:47
@neenaoffline neenaoffline changed the base branch from main to feature-login August 11, 2023 06:26
@@ -0,0 +1,9 @@
CREATE TABLE friend_requests (
id SERIAL,
sender_username text,
Copy link
Member

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,
Copy link
Member

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.

Copy link
Member

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.
Copy link
Member

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}]
Copy link
Member

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

Copy link
Member

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)]
Copy link
Member

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.

Copy link
Member

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})
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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.

Base automatically changed from feature-login to main August 11, 2023 07:54
@shahn95
Copy link
Contributor Author

shahn95 commented Aug 11, 2023

Thank you Neena. We just discussed most of your comments. Also, since chaat development is now parked, I will not continue the discussion here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants