-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
124 lines (107 loc) · 3.43 KB
/
server.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import express from 'express';
import fs from 'fs';
const getAllPeople = () => {
return JSON.parse(fs.readFileSync('./database.json')).people;
}
const getPersonById = id => {
return getAllPeople().find(person => person.id === +id)
}
let personInSpotlight
let chosenPeople = []
let unchosenPeople = [...getAllPeople()]
let peopleToRevisit = []
/**
* Selects a random person from the unchosenPeople, removes them from
* unchosenPeople, and adds them to chosenPeople.
* @returns The current/chosen random person
*/
const getRandomPerson = () => {
//removed the logic here that puts the spotlight person into chosen people list
personInSpotlight = unchosenPeople[Math.floor(Math.random() * unchosenPeople.length)]
unchosenPeople = unchosenPeople.filter(p => p !== personInSpotlight);
return personInSpotlight;
}
const addPersonToRevisitList = () => {
peopleToRevisit = [...peopleToRevisit, personInSpotlight]
personInSpotlight = getNextPerson()
return personInSpotlight;
}
const getNextPerson = () => {
if(unchosenPeople.length){
return getRandomPerson();
}
if (peopleToRevisit.length){
return peopleToRevisit.shift();
}
return {};
//to-do: create logic to handle an empty object, signifying that both lists are empty
}
const port = 3001;
const app = express();
app.get('/people', (req, res) => {
res.send(getAllPeople())
})
app.get('/people/chosen', (req, res) => {
res.send(chosenPeople);
})
app.get('/people/unchosen', (req, res) => {
res.send(unchosenPeople);
})
app.get('/people/revisit', (req, res) => {
res.send(peopleToRevisit);
})
/**
* Basically starts over.
* Resets chosenPeople to empty. Resets unchosenPeople to all people.
*/
app.post('/people/reset', (req, res) => {
//reset current person
personInSpotlight = undefined;
chosenPeople = [];
unchosenPeople = [...getAllPeople()];
peopleToRevisit = [];
res.sendStatus(204); // No content.
})
/**
* Calls getRandomPerson(). Notice that it changes personInSpotlight, chosenPeople, and unchosenPeople
*/
app.post('/people/getNextPerson', (req, res) => {
/*This route is actually only used by the "pick person" button
That's why we're putting the current person in the chosen array
the revisit route has its own way of handling the "current person"
*/
if (personInSpotlight){
chosenPeople = [...chosenPeople, personInSpotlight]
}
const currentPerson = getNextPerson();
console.log(currentPerson)
res.send(currentPerson)
})
/**
* Calls addPersonToRevisitList(). Notice that it changes personInSpotlight and peopleToRevisit
*/
app.post('/person/revisit', (req, res) => {
//Revisit move the person in the spotlight to the revist list but will not assign the next person to the spotlight so it is possible that the spotlight is empty.
if (!personInSpotlight) {
res.status(400).send('There is no one in the spotlight to revisit.')
} else {
console.log(`Revisit ${personInSpotlight.first} at the end.`);
addPersonToRevisitList();
res.status(200).send(personInSpotlight);
}
})
app.get('/person/getpersonInSpotlight', (req, res) => {
console.log(personInSpotlight)
res.send(personInSpotlight)
})
app.get('/people/:id', (req, res) => {
const { id } = req.params;
const person = getPersonById(id);
if (!person)
res.status(404).send(`Person ${id} was not found.`)
res.send(person);
})
app.use(express.static("./public"));
app.listen(port, () => {
console.log(`listening for HTTP requests on port ${port}`);
})