Skip to content

Commit

Permalink
discuss likes model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-G committed May 29, 2015
1 parent 125961e commit 7d2e3fe
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
Binary file not shown.
80 changes: 80 additions & 0 deletions P16-Implementing-Likes/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,83 @@ Firstly, typos can cause bugs that are difficult to debug. The compiler won't ve
Secondly, if we use those strings in multiple places, we need to remember to update every occurrence of them if something in our Parse backend changes.

Instead of using plain strings it would be much better to use constants!
Let's add constants for all the different Parse classes and fields we are going to access! Since this isn't too exciting of an exercise, I'll provide you with the full list of constants that we need.

<div id="class"></div>
Add the following constants to the `ParseHelper` class:

class ParseHelper {

// Following Relation
static let ParseFollowClass = "Follow"
static let ParseFollowFromUser = "fromUser"
static let ParseFollowToUser = "toUser"

// Like Relation
static let ParseLikeClass = "Like"
static let ParseLikeToPost = "toPost"
static let ParseLikeFromUser = "fromUser"

// Post Relation
static let ParsePostUser = "user"
static let ParsePostCreatedAt = "createdAt"

// Flagged Content Relation
static let ParseFlaggedContentClass = "FlaggedContent"
static let ParseFlaggedContentFromUser = "fromUser"
static let ParseFlaggedContentToPost = "toPost"

// User Relation
static let ParseUserUsername = "username"

// ...

This approach has a third advantage that we did not discuss yet: all of the fields are now unambiguous. Before this change we could have used _"toPost"_ to refer to either the _toPost_ column in the _FlaggedContent_ class, or to the _toPost_ column in the _Like_ class. Through giving all of these constants a name, we have removed this ambiguity.

Note that the constant names follow this pattern: _Parse[ClassName][FieldName]_. Consistent naming is important to make your life as a developer easier!

With these constants in place, we can now update our timeline query to use them.

<div class="action"></div>
Change the timeline query to use constants instead of string literals:

static func timelineRequestforCurrentUser(completionBlock: PFArrayResultBlock) {
let followingQuery = PFQuery(className: ParseFollowClass)
followingQuery.whereKey(ParseLikeFromUser, equalTo:PFUser.currentUser()!)

let postsFromFollowedUsers = Post.query()
postsFromFollowedUsers!.whereKey(ParsePostUser, matchesKey: ParseFollowToUser, inQuery: followingQuery)

let postsFromThisUser = Post.query()
postsFromThisUser!.whereKey(ParsePostUser, equalTo: PFUser.currentUser()!)

let query = PFQuery.orQueryWithSubqueries([postsFromFollowedUsers!, postsFromThisUser!])
query.includeKey(ParsePostUser)
query.orderByDescending(ParsePostCreatedAt)

query.findObjectsInBackgroundWithBlock(completionBlock)
}

Same, same but nicer! Now we have a solid foundation to add more queries!

#Adding Parse queries for likes

There are three types of things our app needs to with likes:

1. Create a like, when a user likes a post
2. Delete a like, when a user unlikes a post
3. Fetch all likes for a given post

These three requirements translate directly into three different queries that we're going to
add to our `ParseHelper`.

Just as a refresher, here's how we are modeling likes in `Makestagram`:

![image](likes_model.png)

It's a pretty simple model, that only stores a reference to the user that performed the like and the post that has been liked (ignoring all of the fields that Parse provides automatically).




##Creating Likes
Binary file added P16-Implementing-Likes/likes_model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7d2e3fe

Please sign in to comment.