{"action":"created","comment":{"url":"https://api.github.com/repos/JohnStarich/ee461l/pulls/comments/59824248","id":59824248,"diff_hunk":"@@ -0,0 +1,191 @@\n+package com.johnstarich.moviematcher.models;\n+\n+import com.google.gson.Gson;\n+import com.google.gson.GsonBuilder;\n+import com.johnstarich.moviematcher.app.HttpException;\n+import com.johnstarich.moviematcher.app.HttpStatus;\n+import com.johnstarich.moviematcher.store.MovieMatcherDatabase;\n+import com.mongodb.Block;\n+import com.mongodb.client.AggregateIterable;\n+import com.mongodb.client.MongoCollection;\n+import com.mongodb.client.result.DeleteResult;\n+import org.bson.Document;\n+import org.bson.types.ObjectId;\n+import org.mindrot.jbcrypt.BCrypt;\n+import static java.util.Arrays.asList;\n+\n+import java.util.ArrayList;\n+import java.util.Collection;\n+import java.util.List;\n+\n+import static com.mongodb.client.model.Filters.eq;\n+\n+/**\n+ * Created by Josue on 4/7/2016.\n+ */\n+public class User {\n+    private static class LazyUsersCollection {\n+        public static final MongoCollection<Document> usersCollection = MovieMatcherDatabase.getCollection(\"users\");\n+    }\n+    private static MongoCollection<Document> getCollection() { return LazyUsersCollection.usersCollection; }\n+\n+    /* need to decide how to store user preferences ... */\n+\n+    public final ObjectId _id;\n+    public final String email;\n+    public final String first_name;\n+    public final String last_name;\n+    public final List<User> friends;\n+    public final List<?> groups; // ? should be replaced with Group class\n+    public final String password;\n+\n+    public User(ObjectId _id) {\n+        this._id = _id;\n+        this.email = null;\n+        this.first_name = null;\n+        this.last_name = null;\n+        this.friends = new ArrayList<>(0);\n+        this.groups = new ArrayList<>(0);\n+        this.password = null;\n+    }\n+\n+    public User(ObjectId _id, String email, String first_name, String last_name, List<User> friends, List groups, String password) {\n+        this._id = _id;\n+        this.email = email;\n+        this.first_name = first_name;\n+        this.last_name = last_name;\n+        this.friends = friends;\n+        this.groups = groups;\n+        this.password = password;\n+    }\n+\n+    public User(ObjectId _id, String email, String first_name, String last_name, String password) {\n+        this._id = _id;\n+        this.email = email;\n+        this.first_name = first_name;\n+        this.last_name = last_name;\n+        this.friends = new ArrayList<>(0);\n+        this.groups = new ArrayList<>(0);\n+        this.password = password;\n+    }\n+\n+    public User(ObjectId _id, String email, String first_name, String last_name) {\n+        this._id = _id;\n+        this.email = email;\n+        this.first_name = first_name;\n+        this.last_name = last_name;\n+        this.friends = new ArrayList<>(0);\n+        this.groups = new ArrayList<>(0);\n+        this.password = null;\n+    }\n+\n+    @Override\n+    public boolean equals(Object o) { return o == this || o instanceof User && ((User) o)._id == _id && ((User) o).email == email; }\n+\n+    @Override\n+    public int hashCode() {\n+        return _id.hashCode();\n+    }\n+\n+    public User load() {\n+        Document user = getCollection().find(eq(\"_id\", _id)).first();\n+        Gson gson = new GsonBuilder().create();\n+        return gson.fromJson(user.toJson(), User.class);\n+    }\n+\n+    public User register(String password) throws HttpException {\n+        if(this.load().equals(this)) { //need to test this, too see if it actually works!! ...\n+            throw new HttpException(HttpStatus.BAD_REQUEST);\n+        }\n+\n+        User u = new User(_id, email, first_name, last_name, friends, groups, BCrypt.hashpw(password, BCrypt.gensalt()));\n+        u.insert();\n+        return u;\n+    }\n+\n+    /* not sure how to add friends or groups to mongoDB since they are lists*/\n+    /* will create the collection, once we insert our first user */\n+    public void insert() {\n+        getCollection().insertOne(\n+                new Document(\"_id\", _id).append(\"email\" , email).append(\"first_name\" , first_name)\n+                        .append(\"last_name\", last_name).append(\"password\", password)\n+                        .append(\"friends\", friends).append(\"groups\", groups)\n+        );\n+    }\n+\n+    public static List<User> search(String query) {\n+        AggregateIterable<Document> iterable = getCollection().aggregate(\n+            asList(\n+                new Document(\"$match\", new Document(\"$text\", new Document(\"$search\", query))),\n+                new Document(\n+                    \"$project\",\n+                    new Document(\"email\", true)\n+                        .append(\"first_name\", true)\n+                        .append(\"last_name\", true)\n+                        .append(\"friends\", true)\n+                        .append(\"groups\", true)\n+                        .append(\"password\", true)\n+                        .append(\"score\", new Document(\"$meta\", \"textScore\"))\n+                ),\n+                new Document(\"$sort\", new Document(\"score\",-1))\n+            )\n+        );\n+\n+        ArrayList<User> queryResults = new ArrayList<User>();\n+        Gson gson = new GsonBuilder().create();\n+\n+        iterable\n+                .map(document -> gson.fromJson(document.toJson(), User.class))\n+                .forEach((Block<User>) u -> queryResults.add(u));\n+\n+        return queryResults;\n+    }\n+\n+    public static User loadByUsername(String email) {\n+        Document user = getCollection().find(eq(\"email\", email)).first();\n+        Gson gson = new GsonBuilder().create();","path":"src/main/java/com/johnstarich/moviematcher/models/User.java","position":146,"original_position":146,"commit_id":"14dcf763f444acc688d846192ff4b5c171794f01","original_commit_id":"14dcf763f444acc688d846192ff4b5c171794f01","user":{"login":"JohnStarich","id":1009441,"avatar_url":"https://avatars.githubusercontent.com/u/1009441?v=3","gravatar_id":"","url":"https://api.github.com/users/JohnStarich","html_url":"https://github.com/JohnStarich","followers_url":"https://api.github.com/users/JohnStarich/followers","following_url":"https://api.github.com/users/JohnStarich/following{/other_user}","gists_url":"https://api.github.com/users/JohnStarich/gists{/gist_id}","starred_url":"https://api.github.com/users/JohnStarich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JohnStarich/subscriptions","organizations_url":"https://api.github.com/users/JohnStarich/orgs","repos_url":"https://api.github.com/users/JohnStarich/repos","events_url":"https://api.github.com/users/JohnStarich/events{/privacy}","received_events_url":"https://api.github.com/users/JohnStarich/received_events","type":"User","site_admin":false},"body":"reuse aforementioned gson instance","created_at":"2016-04-15T03:49:58Z","updated_at":"2016-04-15T03:49:58Z","html_url":"https://github.com/JohnStarich/ee461l/pull/23#discussion_r59824248","pull_request_url":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23","_links":{"self":{"href":"https://api.github.com/repos/JohnStarich/ee461l/pulls/comments/59824248"},"html":{"href":"https://github.com/JohnStarich/ee461l/pull/23#discussion_r59824248"},"pull_request":{"href":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23"}}},"pull_request":{"url":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23","id":66583291,"html_url":"https://github.com/JohnStarich/ee461l/pull/23","diff_url":"https://github.com/JohnStarich/ee461l/pull/23.diff","patch_url":"https://github.com/JohnStarich/ee461l/pull/23.patch","issue_url":"https://api.github.com/repos/JohnStarich/ee461l/issues/23","number":23,"state":"open","locked":false,"title":"Userclass","user":{"login":"jjalfaro9","id":17068807,"avatar_url":"https://avatars.githubusercontent.com/u/17068807?v=3","gravatar_id":"","url":"https://api.github.com/users/jjalfaro9","html_url":"https://github.com/jjalfaro9","followers_url":"https://api.github.com/users/jjalfaro9/followers","following_url":"https://api.github.com/users/jjalfaro9/following{/other_user}","gists_url":"https://api.github.com/users/jjalfaro9/gists{/gist_id}","starred_url":"https://api.github.com/users/jjalfaro9/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jjalfaro9/subscriptions","organizations_url":"https://api.github.com/users/jjalfaro9/orgs","repos_url":"https://api.github.com/users/jjalfaro9/repos","events_url":"https://api.github.com/users/jjalfaro9/events{/privacy}","received_events_url":"https://api.github.com/users/jjalfaro9/received_events","type":"User","site_admin":false},"body":"So I changed the name of the mongoDB database from MoviesDatabase to MovieMatcherDatabase since we are also storing User data. \r\n\r\nWe have decided to work with BCrypt in order to store an encrypted version of  users' passwords.\r\n\r\nUser class has been, for the most part, fully implemented. Need to work on methods that will use a Group class. Also, need to finish resetPassword and login methods. \r\n\r\nLast thing, need to work on saving user ratings! ","created_at":"2016-04-15T02:46:30Z","updated_at":"2016-04-15T03:49:58Z","closed_at":null,"merged_at":null,"merge_commit_sha":"00de69157e9c4c5346465c2de486d8eb57eb7df3","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23/commits","review_comments_url":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23/comments","review_comment_url":"https://api.github.com/repos/JohnStarich/ee461l/pulls/comments{/number}","comments_url":"https://api.github.com/repos/JohnStarich/ee461l/issues/23/comments","statuses_url":"https://api.github.com/repos/JohnStarich/ee461l/statuses/14dcf763f444acc688d846192ff4b5c171794f01","head":{"label":"JohnStarich:userclass","ref":"userclass","sha":"14dcf763f444acc688d846192ff4b5c171794f01","user":{"login":"JohnStarich","id":1009441,"avatar_url":"https://avatars.githubusercontent.com/u/1009441?v=3","gravatar_id":"","url":"https://api.github.com/users/JohnStarich","html_url":"https://github.com/JohnStarich","followers_url":"https://api.github.com/users/JohnStarich/followers","following_url":"https://api.github.com/users/JohnStarich/following{/other_user}","gists_url":"https://api.github.com/users/JohnStarich/gists{/gist_id}","starred_url":"https://api.github.com/users/JohnStarich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JohnStarich/subscriptions","organizations_url":"https://api.github.com/users/JohnStarich/orgs","repos_url":"https://api.github.com/users/JohnStarich/repos","events_url":"https://api.github.com/users/JohnStarich/events{/privacy}","received_events_url":"https://api.github.com/users/JohnStarich/received_events","type":"User","site_admin":false},"repo":{"id":51084630,"name":"ee461l","full_name":"JohnStarich/ee461l","owner":{"login":"JohnStarich","id":1009441,"avatar_url":"https://avatars.githubusercontent.com/u/1009441?v=3","gravatar_id":"","url":"https://api.github.com/users/JohnStarich","html_url":"https://github.com/JohnStarich","followers_url":"https://api.github.com/users/JohnStarich/followers","following_url":"https://api.github.com/users/JohnStarich/following{/other_user}","gists_url":"https://api.github.com/users/JohnStarich/gists{/gist_id}","starred_url":"https://api.github.com/users/JohnStarich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JohnStarich/subscriptions","organizations_url":"https://api.github.com/users/JohnStarich/orgs","repos_url":"https://api.github.com/users/JohnStarich/repos","events_url":"https://api.github.com/users/JohnStarich/events{/privacy}","received_events_url":"https://api.github.com/users/JohnStarich/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/JohnStarich/ee461l","description":"","fork":false,"url":"https://api.github.com/repos/JohnStarich/ee461l","forks_url":"https://api.github.com/repos/JohnStarich/ee461l/forks","keys_url":"https://api.github.com/repos/JohnStarich/ee461l/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JohnStarich/ee461l/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JohnStarich/ee461l/teams","hooks_url":"https://api.github.com/repos/JohnStarich/ee461l/hooks","issue_events_url":"https://api.github.com/repos/JohnStarich/ee461l/issues/events{/number}","events_url":"https://api.github.com/repos/JohnStarich/ee461l/events","assignees_url":"https://api.github.com/repos/JohnStarich/ee461l/assignees{/user}","branches_url":"https://api.github.com/repos/JohnStarich/ee461l/branches{/branch}","tags_url":"https://api.github.com/repos/JohnStarich/ee461l/tags","blobs_url":"https://api.github.com/repos/JohnStarich/ee461l/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JohnStarich/ee461l/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JohnStarich/ee461l/git/refs{/sha}","trees_url":"https://api.github.com/repos/JohnStarich/ee461l/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JohnStarich/ee461l/statuses/{sha}","languages_url":"https://api.github.com/repos/JohnStarich/ee461l/languages","stargazers_url":"https://api.github.com/repos/JohnStarich/ee461l/stargazers","contributors_url":"https://api.github.com/repos/JohnStarich/ee461l/contributors","subscribers_url":"https://api.github.com/repos/JohnStarich/ee461l/subscribers","subscription_url":"https://api.github.com/repos/JohnStarich/ee461l/subscription","commits_url":"https://api.github.com/repos/JohnStarich/ee461l/commits{/sha}","git_commits_url":"https://api.github.com/repos/JohnStarich/ee461l/git/commits{/sha}","comments_url":"https://api.github.com/repos/JohnStarich/ee461l/comments{/number}","issue_comment_url":"https://api.github.com/repos/JohnStarich/ee461l/issues/comments{/number}","contents_url":"https://api.github.com/repos/JohnStarich/ee461l/contents/{+path}","compare_url":"https://api.github.com/repos/JohnStarich/ee461l/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JohnStarich/ee461l/merges","archive_url":"https://api.github.com/repos/JohnStarich/ee461l/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JohnStarich/ee461l/downloads","issues_url":"https://api.github.com/repos/JohnStarich/ee461l/issues{/number}","pulls_url":"https://api.github.com/repos/JohnStarich/ee461l/pulls{/number}","milestones_url":"https://api.github.com/repos/JohnStarich/ee461l/milestones{/number}","notifications_url":"https://api.github.com/repos/JohnStarich/ee461l/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JohnStarich/ee461l/labels{/name}","releases_url":"https://api.github.com/repos/JohnStarich/ee461l/releases{/id}","deployments_url":"https://api.github.com/repos/JohnStarich/ee461l/deployments","created_at":"2016-02-04T15:26:04Z","updated_at":"2016-02-04T15:52:40Z","pushed_at":"2016-04-15T03:06:31Z","git_url":"git://github.com/JohnStarich/ee461l.git","ssh_url":"git@github.com:JohnStarich/ee461l.git","clone_url":"https://github.com/JohnStarich/ee461l.git","svn_url":"https://github.com/JohnStarich/ee461l","homepage":null,"size":1042,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"JohnStarich:master","ref":"master","sha":"48eb09f30e832bc2d5b07149ed9e09dd0e38a14d","user":{"login":"JohnStarich","id":1009441,"avatar_url":"https://avatars.githubusercontent.com/u/1009441?v=3","gravatar_id":"","url":"https://api.github.com/users/JohnStarich","html_url":"https://github.com/JohnStarich","followers_url":"https://api.github.com/users/JohnStarich/followers","following_url":"https://api.github.com/users/JohnStarich/following{/other_user}","gists_url":"https://api.github.com/users/JohnStarich/gists{/gist_id}","starred_url":"https://api.github.com/users/JohnStarich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JohnStarich/subscriptions","organizations_url":"https://api.github.com/users/JohnStarich/orgs","repos_url":"https://api.github.com/users/JohnStarich/repos","events_url":"https://api.github.com/users/JohnStarich/events{/privacy}","received_events_url":"https://api.github.com/users/JohnStarich/received_events","type":"User","site_admin":false},"repo":{"id":51084630,"name":"ee461l","full_name":"JohnStarich/ee461l","owner":{"login":"JohnStarich","id":1009441,"avatar_url":"https://avatars.githubusercontent.com/u/1009441?v=3","gravatar_id":"","url":"https://api.github.com/users/JohnStarich","html_url":"https://github.com/JohnStarich","followers_url":"https://api.github.com/users/JohnStarich/followers","following_url":"https://api.github.com/users/JohnStarich/following{/other_user}","gists_url":"https://api.github.com/users/JohnStarich/gists{/gist_id}","starred_url":"https://api.github.com/users/JohnStarich/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JohnStarich/subscriptions","organizations_url":"https://api.github.com/users/JohnStarich/orgs","repos_url":"https://api.github.com/users/JohnStarich/repos","events_url":"https://api.github.com/users/JohnStarich/events{/privacy}","received_events_url":"https://api.github.com/users/JohnStarich/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/JohnStarich/ee461l","description":"","fork":false,"url":"https://api.github.com/repos/JohnStarich/ee461l","forks_url":"https://api.github.com/repos/JohnStarich/ee461l/forks","keys_url":"https://api.github.com/repos/JohnStarich/ee461l/keys{/key_id}","collaborators_url":"https://api.github.com/repos/JohnStarich/ee461l/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/JohnStarich/ee461l/teams","hooks_url":"https://api.github.com/repos/JohnStarich/ee461l/hooks","issue_events_url":"https://api.github.com/repos/JohnStarich/ee461l/issues/events{/number}","events_url":"https://api.github.com/repos/JohnStarich/ee461l/events","assignees_url":"https://api.github.com/repos/JohnStarich/ee461l/assignees{/user}","branches_url":"https://api.github.com/repos/JohnStarich/ee461l/branches{/branch}","tags_url":"https://api.github.com/repos/JohnStarich/ee461l/tags","blobs_url":"https://api.github.com/repos/JohnStarich/ee461l/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/JohnStarich/ee461l/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/JohnStarich/ee461l/git/refs{/sha}","trees_url":"https://api.github.com/repos/JohnStarich/ee461l/git/trees{/sha}","statuses_url":"https://api.github.com/repos/JohnStarich/ee461l/statuses/{sha}","languages_url":"https://api.github.com/repos/JohnStarich/ee461l/languages","stargazers_url":"https://api.github.com/repos/JohnStarich/ee461l/stargazers","contributors_url":"https://api.github.com/repos/JohnStarich/ee461l/contributors","subscribers_url":"https://api.github.com/repos/JohnStarich/ee461l/subscribers","subscription_url":"https://api.github.com/repos/JohnStarich/ee461l/subscription","commits_url":"https://api.github.com/repos/JohnStarich/ee461l/commits{/sha}","git_commits_url":"https://api.github.com/repos/JohnStarich/ee461l/git/commits{/sha}","comments_url":"https://api.github.com/repos/JohnStarich/ee461l/comments{/number}","issue_comment_url":"https://api.github.com/repos/JohnStarich/ee461l/issues/comments{/number}","contents_url":"https://api.github.com/repos/JohnStarich/ee461l/contents/{+path}","compare_url":"https://api.github.com/repos/JohnStarich/ee461l/compare/{base}...{head}","merges_url":"https://api.github.com/repos/JohnStarich/ee461l/merges","archive_url":"https://api.github.com/repos/JohnStarich/ee461l/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/JohnStarich/ee461l/downloads","issues_url":"https://api.github.com/repos/JohnStarich/ee461l/issues{/number}","pulls_url":"https://api.github.com/repos/JohnStarich/ee461l/pulls{/number}","milestones_url":"https://api.github.com/repos/JohnStarich/ee461l/milestones{/number}","notifications_url":"https://api.github.com/repos/JohnStarich/ee461l/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/JohnStarich/ee461l/labels{/name}","releases_url":"https://api.github.com/repos/JohnStarich/ee461l/releases{/id}","deployments_url":"https://api.github.com/repos/JohnStarich/ee461l/deployments","created_at":"2016-02-04T15:26:04Z","updated_at":"2016-02-04T15:52:40Z","pushed_at":"2016-04-15T03:06:31Z","git_url":"git://github.com/JohnStarich/ee461l.git","ssh_url":"git@github.com:JohnStarich/ee461l.git","clone_url":"https://github.com/JohnStarich/ee461l.git","svn_url":"https://github.com/JohnStarich/ee461l","homepage":null,"size":1042,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23"},"html":{"href":"https://github.com/JohnStarich/ee461l/pull/23"},"issue":{"href":"https://api.github.com/repos/JohnStarich/ee461l/issues/23"},"comments":{"href":"https://api.github.com/repos/JohnStarich/ee461l/issues/23/comments"},"review_comments":{"href":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23/comments"},"review_comment":{"href":"https://api.github.com/repos/JohnStarich/ee461l/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/JohnStarich/ee461l/pulls/23/commits"},"statuses":{"href":"https://api.github.com/repos/JohnStarich/ee461l/statuses/14dcf763f444acc688d846192ff4b5c171794f01"}}}}