forked from sf-wdi-25/express_self_api
-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathprofileController.js
More file actions
107 lines (90 loc) · 3.51 KB
/
profileController.js
File metadata and controls
107 lines (90 loc) · 3.51 KB
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
/************
* DATABASE *
************/
const db = require('../models');
// GET renderSearchResults => on load of the page what to be able to render the search results page which loads the 10 people in the database
// append profileSRP from the SRP section
// fill in the correct user information
// Function to send back everything
// function searchResultsPage(req, res) {
// console.log('SRP is getting data')
// db.Profile.find({}, function(err, allProfiles) {
// res.json(allProfiles)
// })
// res.status(200)
// };
// function that ONLY sends back ones that are NOT marked for delation => used for initial SRP
function searchResultsPage(req, res) {
console.log('SRP is getting data')
db.Profile.find({}, function(err, allProfiles) {
let arrayOfProfilesToBeShown = [];
let arrayOfProfilesMarkedForDeletion = [];
allProfiles.forEach(function(profile) {
if ( profile.markedForDeletion === false) {
arrayOfProfilesToBeShown.push(profile);
} else {
arrayOfProfilesMarkedForDeletion.push(profile);
}
});
res.json(arrayOfProfilesToBeShown);
console.log('DONT SEND BACK', arrayOfProfilesMarkedForDeletion);
});
};
// GET (SEND user ID) and renderProfile need to be able to click on a person, take that id and route them to a profile page
function showOneProfile(req, res) {
console.log('showOneProfile Route is getting hit', req.params.profileId);
db.Profile.findById(req.params.profileId, function(err, foundProfile) {
if (err) {
console.log('showOneProfile in controller had an error', err);
}
// send back the Profile info the DB via json file
res.json(foundProfile);
});
};
// POST createNewUser => able to create a new user on the SRP and then render that new person
function createNewProfile(req, res) {
console.log('CREATE NEW PROFILE', req.body)
db.Profile.create(req.body, function(err, newProfile) {
if (err) {
console.log('ERROR ON CREATE', err)
}
res.json(newProfile);
console.log('NEW PROFILE INFO SENT BACK', newProfile)
})
};
// PUT able to update the user on the renderProfile page (able to update each spot individually)
function updateOneProfile(req, res) {
console.log('updateOneProfile Route is getting hit!!!', req.body)
db.Profile.findByIdAndUpdate(req.params.profileId, {$set: {
name: req.body.name,
title: req.body.title,
workPlace: req.body.workPlace,
quote: req.body.quote,
image: req.body.image,
}}, {new: true}, function(err, saveProfile) {
if (err) {
console.log('THERE WAS AN ERROR DURING updateOneProfile Save', err);
}
console.log('updateOneProfile SAVED AND JSON IS SENT BACK', saveProfile);
res.json(saveProfile)
})
};
// DELETE / PUT able to 'hit the delete flag' and not show up the user anymore vs actually deleting their information
function removeOneProfile(req, res) {
console.log('removeOneProfile IS GETTING HITTT!!!!!', req.body)
db.Profile.findByIdAndUpdate(req.params.profileId, {$set: {
markedForDeletion: req.body.markedForDeletion}}, {new: true}, function(err, removedProfile) {
if (err) {
console.log ('THERE WAS AN ERROR DURING removeOneProfile', err);
}
console.log('removeOneProfile SAVED and removed profile JSON sent back', removedProfile);
res.json(removedProfile);
});
};
module.exports = {
searchResultsPage: searchResultsPage,
createNewProfile: createNewProfile,
showOneProfile: showOneProfile,
updateOneProfile: updateOneProfile,
removeOneProfile: removeOneProfile
};