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
Showing
8 changed files
with
139 additions
and
26 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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,13 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const port = process.env.port || '3000'; | ||
|
||
const app = express(); | ||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
const routes = require('./routes/contact'); | ||
|
||
app.use('/api', routes); | ||
|
||
app.listen(port, () => console.log(`API running on port ${port}`)); |
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,8 @@ | ||
const mongoose = require('mongoose'); | ||
const Schema = mongoose.Schema; | ||
|
||
const ContactSchema = new Schema({ | ||
name: {type: String, required: true, max: 200} | ||
}); | ||
|
||
module.exports = mongoose.model('Contact', ContactSchema); |
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,15 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
const service = require('../services/contact'); | ||
const contactAPI = '/contact'; | ||
|
||
router.get('/', (req, res) => res.send('Hello World!')); | ||
|
||
router.get(contactAPI, (req, res) => service.getAll(req, res)); | ||
router.get(`${contactAPI}/:id`, (req, res) => service.getById(req, res)); | ||
router.post(contactAPI, (req, res) => service.post(req, res)); | ||
router.put(`${contactAPI}/:id`, (req, res) => service.put(req, res)); | ||
router.delete(`${contactAPI}/:id`, (req, res) => service.remove(req, res)); | ||
|
||
module.exports = router; |
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,58 @@ | ||
const Contact = require('../models/contact'); | ||
require('./mongo').connect(); | ||
|
||
async function getAll(req, res) { | ||
try { | ||
var records = await Contact.find().exec(); | ||
res.send(records); | ||
} catch (error) { | ||
res.status(500).send(error); | ||
} | ||
} | ||
|
||
async function getById(req, res) { | ||
try { | ||
var record = await Contact.findById(req.params.id).exec(); | ||
res.send(record); | ||
} catch (error) { | ||
res.status(500).send(error); | ||
} | ||
} | ||
|
||
async function post(req, res) { | ||
try { | ||
var record = new Contact(req.body); | ||
var record = await record.save(); | ||
res.status(200).json(record); | ||
} catch (error) { | ||
res.status(500).send(error); | ||
} | ||
} | ||
|
||
async function put(req, res) { | ||
try { | ||
var record = await Contact.findById(req.params.id).exec(); | ||
record.set(req.body); | ||
var record = await record.save(); | ||
res.send(record); | ||
} catch (error) { | ||
res.status(500).send(error); | ||
} | ||
} | ||
|
||
async function remove(req, res) { | ||
try { | ||
var record = await Contact.deleteOne({ _id: req.params.id }).exec(); | ||
res.send(record); | ||
} catch (error) { | ||
res.status(500).send(error); | ||
} | ||
} | ||
|
||
module.exports = { | ||
getAll, | ||
getById, | ||
post, | ||
put, | ||
remove | ||
}; |
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,14 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
mongoose.Promise = global.Promise; | ||
const mongoUri = 'mongodb://localhost/tutorials'; | ||
|
||
function connect() { | ||
mongoose.set('debug', true); | ||
return mongoose.connect(mongoUri, { useNewUrlParser: true }); | ||
} | ||
|
||
module.exports = { | ||
connect, | ||
mongoose | ||
}; |
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,27 @@ | ||
http://localhost:3000/api/contact | ||
|
||
### | ||
|
||
GET http://localhost:3000/api/contact/5e0ce5c57afd9b6ead5a87f1 | ||
|
||
### | ||
|
||
POST http://localhost:3000/api/contact HTTP/1.1 | ||
content-type: application/json | ||
|
||
{ | ||
"name": "Java" | ||
} | ||
|
||
### | ||
|
||
PUT http://localhost:3000/api/contact/5e0ce5c57afd9b6ead5a87f1 HTTP/1.1 | ||
content-type: application/json | ||
|
||
{ | ||
"name": "Java OO" | ||
} | ||
|
||
### | ||
|
||
DELETE http://localhost:3000/api/contact/5e0ce5c57afd9b6ead5a87f1 |