From e52d1d978f698cffedbb5f1e09b394ee3da9c7fc Mon Sep 17 00:00:00 2001
From: Ferenc Schulcz <schulcz.ferenc@gmail.com>
Date: Fri, 29 Apr 2022 23:27:05 +0200
Subject: [PATCH] Swiping and matching works

---
 db/db.js                | 62 +++++++++++++++++++++++++++++++++++++++--
 index.js                | 14 ++++++----
 middleware/browseMW.js  | 10 +++++--
 middleware/dislikeMW.js |  4 ++-
 middleware/likeMW.js    | 12 ++++++--
 views/browse.ejs        |  4 +--
 views/nomore.ejs        |  7 +++++
 7 files changed, 97 insertions(+), 16 deletions(-)
 create mode 100644 views/nomore.ejs

diff --git a/db/db.js b/db/db.js
index 16e9230..a2cdd3b 100644
--- a/db/db.js
+++ b/db/db.js
@@ -14,7 +14,9 @@ const User = mongoose.model('user', {
     member2: String,
     Member3: String,
     contact: String,
-    active: Boolean
+    active: Boolean,
+    likes: [String], // usernames that this user liked
+    dislikes: [String], // usernames that this user disliked
 });
 
 function emptyUser() {
@@ -27,7 +29,9 @@ function emptyUser() {
         member2: "",
         Member3: "",
         contact: "",
-        active: false
+        active: false,
+        likes: [],
+        dislikes: [],
     });
 }
 
@@ -35,7 +39,7 @@ function createUser(username, password, callback) {
     const user = emptyUser();
     user.username = username; user.password = password; // using double ROT-13 for password encryption
     user.save(err => {
-        console.error(err);
+        if (err != null) console.error(err);
         callback(err);
     });
 }
@@ -84,10 +88,62 @@ function updateProfile(username, name, bio, member1, member2, member3, contact,
     })
 }
 
+function getUnseenProfilenameForUser(username) {
+    return new Promise((resolve, reject) => {
+        getUser(username).then(user => {
+            likes = user.likes;
+            dislikes = user.dislikes;
+            User.findOne({
+                $and: [
+                    {
+                        username: {
+                            $nin: likes
+                        }
+                    },
+                    {
+                        username: {
+                            $nin: dislikes
+                        }
+                    },
+                    {
+                        username: {
+                            $ne: username // no self-select
+                        }
+                    }
+                ]
+            }).then(value => {
+                if (value == null) resolve(null);
+                else resolve(value.username);
+            });
+        });
+    });
+}
+
+function like(who, whom) {
+    return new Promise((resolve, reject) => {
+        getUser(who).then(user => {
+            user.likes.push(whom);
+            User.updateOne({ username: who }, user).then(() => resolve());
+        });
+    });
+}
+
+function dislike(who, whom) {
+    return new Promise((resolve, reject) => {
+        getUser(who).then(user => {
+            user.dislikes.push(whom);
+            User.updateOne({ username: who }, user).then(() => resolve());
+        });
+    });
+}
+
 module.exports = {
     createUser: createUser,
     doesUserExist: doesUserExist,
     checkUserCredentials: checkUserCredentials,
     getUser: getUser,
     updateProfile: updateProfile,
+    getUnseenProfilenameForUser: getUnseenProfilenameForUser,
+    like: like,
+    dislike: dislike,
 };
\ No newline at end of file
diff --git a/index.js b/index.js
index f601fec..25f68a7 100644
--- a/index.js
+++ b/index.js
@@ -43,17 +43,19 @@ app.get('/register', renderMW(objectrepository, 'register'));
 app.post('/register', registerMW(objectrepository));
 app.get('/logout', logoutMW(objectrepository));
 
+app.param('username', (req, res, next, username) => {
+    res.locals.userToLoad = username;
+    return next();
+});
+
 app.get('/browse', authMW(), browseMW(objectrepository), profileloadMW(), renderMW(objectrepository, 'browse'));
-app.post('/browse/like', authMW(), selectMW(), profileloadMW(), likeMW());
+app.get('/browse/like/:username', authMW(), profileloadMW(), likeMW());
 app.get('/browse/match', authMW(), renderMW(objectrepository, 'newmatch'));
-app.post('/browse/dislike', authMW(), selectMW(), profileloadMW(), dislikeMW());
+app.get('/browse/dislike/:username', authMW(), profileloadMW(), dislikeMW());
+app.get('/nomore', authMW(), renderMW(objectrepository, 'nomore'));
 
 app.get('/matches', authMW(), matchlistMW(), renderMW(objectrepository, 'matches'));
 
-app.param('username', (req, res, next, username) => {
-    res.locals.userToLoad = username;
-    return next();
-});
 app.get('/user/:username', authMW(), profileloadMW(), renderMW(objectrepository, 'match'));
 
 app.get('/profile', authMW(), profileloadMW(true), renderMW(objectrepository, 'profile'));
diff --git a/middleware/browseMW.js b/middleware/browseMW.js
index fe51faa..cded31a 100644
--- a/middleware/browseMW.js
+++ b/middleware/browseMW.js
@@ -1,8 +1,14 @@
+db = require('../db/db');
 
 module.exports = function (objectrepository) {
     return function (req, res, next) {
         // selects a username that the current user has no relation with yet, and places it on res.locals.userToLoad.
-        res.locals.userToLoad = "test";
-        return next();
+        db.getUnseenProfilenameForUser(res.locals.username).then(username => {
+            if (username == null) {
+                return res.redirect('/nomore');
+            }
+            res.locals.userToLoad = username;
+            return next();
+        });
     }
 }
\ No newline at end of file
diff --git a/middleware/dislikeMW.js b/middleware/dislikeMW.js
index f3209f0..f4b5e19 100644
--- a/middleware/dislikeMW.js
+++ b/middleware/dislikeMW.js
@@ -2,6 +2,8 @@
 module.exports = function (objectrepository) {
     return function (req, res, next) {
         // creates a negative relation with the current user and the one on res.locals.profile. Redirects to the referer page.
-        return next();
+        db.dislike(res.locals.username, res.locals.profile.username).then(() => {
+            return res.redirect('/browse');
+        })
     }
 }
\ No newline at end of file
diff --git a/middleware/likeMW.js b/middleware/likeMW.js
index e96fe28..b09d878 100644
--- a/middleware/likeMW.js
+++ b/middleware/likeMW.js
@@ -1,8 +1,16 @@
+const db = require("../db/db");
 
 module.exports = function (objectrepository) {
     return function (req, res, next) {
         // creates a positive relation with the current user and the one on res.locals.profile. If it's a match, redirects to /browse/match, else to /browse.
-        if (Math.random * 2 >= 1) return res.redirect('/browse/match');
-        else return res.redirect('/browse');
+
+        db.like(res.locals.username, res.locals.profile.username).then(() => {
+            if (res.locals.profile.likes.includes(res.locals.username)) {
+                // there'a a match!
+                return res.redirect('/browse/match');
+            } else {
+                return res.redirect('/browse');
+            }
+        })
     }
 }
\ No newline at end of file
diff --git a/views/browse.ejs b/views/browse.ejs
index 221dc1e..51566a2 100644
--- a/views/browse.ejs
+++ b/views/browse.ejs
@@ -9,8 +9,8 @@
     </p>
 
     <section id="actionbar">
-        <a class="button" href="/browse">Dislike</a>
-        <a class="button" href="/browse/match">Like</a>
+        <a class="button" href="/browse/dislike/<%= profile.username %>">Dislike</a>
+        <a class="button" href="/browse/like/<%= profile.username %>">Like</a>
     </section>
 
     <%- include('_tail', {}) %>
\ No newline at end of file
diff --git a/views/nomore.ejs b/views/nomore.ejs
new file mode 100644
index 0000000..5cae9fa
--- /dev/null
+++ b/views/nomore.ejs
@@ -0,0 +1,7 @@
+<%- include('_head', {}) %>
+
+    <h1>No unseen profiles!</h1>
+
+    <p>Check back later.</p>
+
+    <%- include('_tail', {}) %>
\ No newline at end of file
-- 
GitLab