A "friends list" is a great example of a many-to-many relationship. Seems to me that a tutorial of how to add a "friends list" would solve both these questions, so that's what I'm doing.
RequirementsFirst lets think of what a "friends list" should entail:
- Find a person
- Add them to my friends list
- Find a person
- Highlight if I already have them in my friends list, or have asked
- List requests from others waiting for my approve/reject action
- List my friends
- List my requests that are pending
- List my rejections
- List requests I have rejected
- Request to add someone as a friend
- Approve a request
- Reject a request
- Things we could add later:
- Un-reject (change to approved, in case someone makes a mistake)
- Re-request (possibly with a time-window to prevent abuse)
Status (string: requested/approved/rejected/etc)
So, lets see how we can handle the above lists and actions:
Find a person (highlight existing status)Once your existing "find a person" query is run, you would need an array of User objects. Using this list we could ask for all FriendRequest objects where RequestFrom is in the list or RequestTo is in the list.
List requests from others waiting for my approver/reject action
List my friends
List my requests that are pending
List my rejections
List requests I have rejected
Request to add someone as a friendFirst make sure there's no existing record with the current User and the target User as RequestFrom/RequestTo or the other way around.
If that requirement is met then you just need to create a new FriendRequest object with the following properties:
RequestFrom: current User RequestTo: target User Status: requested
Approve a requestChange the Status from "requested" to "approved".
Reject a requestChange the Status from "requested" to "rejected".
SummaryIn theory it looks like everything is covered, though I welcome comments and feedback if you think I'm missed anything or made a mistake.