-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingles.js
69 lines (66 loc) · 2.54 KB
/
singles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const router = require('express').Router();
const db = require('./db.json');
const userdb = require('./userdb.json');
const _intersection = require('lodash').intersection;
const getTags = (tags) => tags.match(/\w+(?:\s\w+)?/g).map(tag => tag.toLowerCase());
const createMatches = (gender, personId) => {
const personLookingFor = db[gender][personId];
if (personLookingFor.matches && personLookingFor.matches.length) return personLookingFor.matches;
const oppositeGender = gender === 'boys' ? 'girls' : 'boys';
const tags = getTags(personLookingFor.tags);
console.log("TAGS:", tags, "OPPOSITE GENDER", oppositeGender);
const findMatches = db[oppositeGender].filter((person) => {
const potentialMatchTags = getTags(person.tags);
console.log("POTENTIAL MATCH TAGS", potentialMatchTags);
const matchedTags = _intersection(tags, potentialMatchTags);
person.numberOfMatchedTags = matchedTags.length;
if (person.numberOfMatchedTags > 0) return person;
}).sort((matchA, matchB) => matchB.numberOfMatchedTags - matchA.numberOfMatchedTags);
return findMatches;
};
module.exports = router
.post('/login', (req, res, next) => {
const user = userdb.users.find(person => {
return person.email === req.body.email && person.password === req.body.password;
});
if (user) {
req.session.userId = user.id;
res.json(user);
} else {
res.sendStatus(401);
}
})
.get('/logout', (req, res, next) => {
req.session.destroy();
res.status(204).redirect('/');
})
.post('/me', function (req, res, next) {
if (req.session.userId) res.json(userdb.users.find(person => person.id === req.session.userId));
else res.sendStatus(401);
})
.get('/all', (req, res, next) => {
if (req.session.userId) res.json(db);
else res.sendStatus(401);
})
.get('/boys', (req, res, next) => {
if (req.session.userId) res.json(db.boys);
else res.sendStatus(401);
})
.get('/boys/:id', (req, res, next) => {
if (req.session.userId) res.json(db.boys[req.params.id]);
else res.sendStatus(401);
})
.get('/girls', (req, res, next) => {
if (req.session.userId) res.json(db.girls);
else res.sendStatus(401);
})
.get('/girls/:id', (req, res, next) => {
if (req.session.userId) res.json(db.girls[req.params.id]);
else res.sendStatus(401);
})
.get('/:gender/match/:id', (req, res, next) => {
console.log("REQ>PARAMS", req.params.gender, req.params.id);
const matches = createMatches(`${req.params.gender}s`, req.params.id);
console.log("MATCHES:", matches);
res.json(matches);
});