Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");
const mongoose = require("mongoose");
const routes = require("./src/routes/akarisRoutes");
const contactRouter = require('./src/routes/akarisRoutes');
const app = express();


Expand All @@ -16,7 +16,7 @@ app.use(express.json());


//==SERVING ROUTES

app.use('/api/contacts', contactRouter)


//====SERVER===
Expand Down
52 changes: 11 additions & 41 deletions src/controllers/akarisControllers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@

const Contact = require('../models/akarisModels')

module.exports = addNewContact = (req, res) => {
let newContact = new Contact(req.body);

newContact.save((err, contact) => {
if (err) {
res.send(err);
}
res.json(contact);
});
};


//==POST
//Create contact
exports.addNewContact = async (req, res) => {
Expand All @@ -32,36 +20,18 @@ exports.addNewContact = async (req, res) => {

}

module.exports = getContacts = (req, res) => {
Contact.find({}, (err, contact) => {
if (err) {
res.send(err);
}
res.json(contact);
});
//GET ALL CONTACTS
exports.getContacts = async (req, res) => {
try {
const contacts = await Contact.find()
res.status(201).json({
contacts
})
} catch (error) {
res.send(error)
}

};


module.exports = getContactWithID = (req, res) => {
Contact.findById(req.params.contactId, (err, contact) => {
if (err) {
res.send(err);
}
res.json(contact);
});
};

//UPDATE
module.exports = updateContact = (req, res) => {
Contact.findOneAndUpdate(
{ _id: req.params.contactId },
req.body,
{ new: true },
(err, contact) => {
if (err) {
res.send(err);
}
res.json(contact);
}
);
};
36 changes: 7 additions & 29 deletions src/routes/akarisRoutes.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
const express = require('express');
const router = express.Router();
//Requiring controllers
const {
addNewContact,
getContacts,
getContactWithID,
updateContact
} = require("../controllers/akarisControllers");
addNewContact
} = require('../controllers/akarisControllers');

const routes = app => {
app
.route("/contact")
.get((req, res, next) => {
// middleware
console.log(`Request from: ${req.originalUrl}`);
console.log(`Request type: ${req.method}`);
next();
}, getContacts)

// POST endpoint
.post(addNewContact);
router.route('/').post(addNewContact)

app
.route("/contact/:contactId")
// get specific contact
.get(getContactWithID)

// put request
.put(updateContact)

// delete request
.delete((req, res) => res.send("DELETE request successfull!!!"));
};

module.exports = routes;
module.exports = router;