forked from sveltedummy/vercel-simple-crud
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa9c99c
commit 75e37cb
Showing
4 changed files
with
332 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,33 @@ | ||
let people = [{name: 'Patrick'}, {name: 'Sandy'}, {name: 'Bob'}] | ||
|
||
module.exports = function (req, res) { | ||
if (req.method === 'GET') { | ||
res.status(200).json(people); | ||
} else if (req.method === 'POST') { | ||
people.push(req.body) | ||
res.status(200).json(people) | ||
} else if (req.method === 'PUT') { | ||
const {person, index} = req.body; | ||
people[index] = person; | ||
res.status(200).json(people) | ||
} else if (req.method === 'DELETE') { | ||
const {index} = req.body; | ||
people = people.filter((_, i) => i !== index); | ||
res.status(200).json(people) | ||
} | ||
}; | ||
if (req.method === 'GET') { | ||
res.status(200).json(people); | ||
} else if (req.method === 'POST') { | ||
people.push(req.body) | ||
res.status(200).json(people) | ||
} else if (req.method === 'PUT') { | ||
const {person, index} = req.body; | ||
people[index] = person; | ||
res.status(200).json(people) | ||
} else if (req.method === 'DELETE') { | ||
const {index} = req.body; | ||
people = people.filter(function(_, i) { return i !== index }); | ||
res.status(200).json(people) | ||
} | ||
}; | ||
|
||
/* To integrate with MongoDB.. | ||
1. On MongoDB site, Create.. | ||
- Account | ||
- Cluster | ||
- Database Access User | ||
- Network Access | ||
2. Copy URI from MongoDB site and then.. | ||
- Add as Env variable | ||
3. Setup API! | ||
- npm install mongoose | ||
- create schema | ||
- connect to database with URI | ||
- write CRUD functions | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const uri = process.env.DB_URI | ||
|
||
const personSchema = new mongoose.Schema({ | ||
name: String, | ||
age: Number | ||
}) | ||
|
||
const Person = mongoose.model( | ||
'Person', | ||
personSchema, | ||
'People' | ||
) | ||
|
||
mongoose.connect( | ||
uri, | ||
{useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false}, | ||
function(error) { if (error) console.log("Error!" + error); } | ||
); | ||
|
||
module.exports = function (req, res) { | ||
if (req.method === 'GET') { | ||
Person.find() | ||
.then(people => { | ||
res.status(200).json(people); | ||
}) | ||
.catch((error) => res.status(500).json(error.message)) | ||
} else if (req.method === 'POST') { | ||
const newPerson = new Person(req.body); | ||
newPerson.save() | ||
.then(newPerson => { | ||
res.status(200).json(newPerson) | ||
}) | ||
.catch((error) => res.status(500).json(error.message)) | ||
} else if (req.method === 'PUT') { | ||
const {person, _id} = req.body; | ||
Person.findByIdAndUpdate(_id, person, {returnOriginal: false}) | ||
.then(updatedPerson => { | ||
res.status(200).json(updatedPerson) | ||
}) | ||
.catch((error) => res.status(500).json(error.message)) | ||
} else if (req.method === 'DELETE') { | ||
const {_id} = req.body; | ||
Person.findByIdAndDelete(_id) | ||
.then(deletedPerson => { | ||
res.status(200).json(deletedPerson) | ||
}).catch((error) => res.status(500).json(error.message)) | ||
} | ||
}; |
Oops, something went wrong.