Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
loiane committed Jan 1, 2020
1 parent c01f8ff commit 1de1c0e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 26 deletions.
23 changes: 0 additions & 23 deletions .eslintrc.json

This file was deleted.

7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -4,17 +4,18 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mongodb": "^3.4.1"
"mongoose": "^5.8.3"
},
"devDependencies": {
"eslint": "^6.8.0",
"eslint-plugin-node": "^11.0.0"
"nodemon": "^2.0.2"
}
}
13 changes: 13 additions & 0 deletions src/index.js
@@ -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}`));
8 changes: 8 additions & 0 deletions src/models/contact.js
@@ -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);
15 changes: 15 additions & 0 deletions src/routes/contact.js
@@ -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;
58 changes: 58 additions & 0 deletions src/services/contact.js
@@ -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
};
14 changes: 14 additions & 0 deletions src/services/mongo.js
@@ -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
};
27 changes: 27 additions & 0 deletions testAPI.http
@@ -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

0 comments on commit 1de1c0e

Please sign in to comment.