Skip to content

Commit 704545a

Browse files
committed
added full crud api with controller example
1 parent 66be9c1 commit 704545a

5 files changed

Lines changed: 552 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
In this lecture we talk about Top Level Middleware, Using Postman, and using Axios in our API.
44

5+
Examples can be found inside the `.js` files of this repo.
6+
57
## Top Level Middleware
68

79
`Top Level Middleware` is the term we use to refer to some logic that will be executed upon every request that is made to our API.
@@ -54,4 +56,33 @@ Now your API is setup to follow the `CORS` protocol.
5456
5557
## Postman
5658
57-
`Postman` is a suite that we can use to act as a `client` to interact with the `API` that we build.
59+
`Postman` is a suite that we can use to act as a `client` to interact with the `API` that we build.
60+
61+
The browser, by default, will only let us make `get` requests from the URL. So we can use `postman` to test out a `full CRUD API`.
62+
63+
## Full CRUD API
64+
65+
So far we have built an API that has only allowed `get` requests to be made. We need to also allow `post`, `put`, and `delete` requests to be made to become a `full CRUD API`.
66+
67+
POST:
68+
```js
69+
app.post('path for the endpoint', handlerFunc)
70+
```
71+
72+
PUT:
73+
```js
74+
app.put('path for the endpoint', handlerFunc)
75+
```
76+
77+
DELETE:
78+
```js
79+
app.delete('path for the endpoint', handlerFunc)
80+
```
81+
82+
We now have a "fully funcitonal" API because are following the full CRUD pattern.
83+
84+
## Controller
85+
86+
A `controller` is what we can use to store all of our functions that we use to handle the requests being made.
87+
88+
We usually will create a seperate javascript file that will hold the controller functions.

controller.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Sample Data
2+
const users = [
3+
{
4+
"id": 1,
5+
"first_name": "Raul",
6+
"last_name": "Wynter",
7+
"email": "rwynter0@google.com"
8+
},
9+
{
10+
"id": 2,
11+
"first_name": "Demetri",
12+
"last_name": "Dakin",
13+
"email": "ddakin1@google.com"
14+
},
15+
{
16+
"id": 3,
17+
"first_name": "Lira",
18+
"last_name": "Collishaw",
19+
"email": "lcollishaw2@google.com"
20+
},
21+
{
22+
"id": 4,
23+
"first_name": "Jennette",
24+
"last_name": "O'Brollachain",
25+
"email": "jobrollachain3@msn.com"
26+
},
27+
{
28+
"id": 5,
29+
"first_name": "Annaliese",
30+
"last_name": "Marishenko",
31+
"email": "amarishenko4@msn.com"
32+
},
33+
{
34+
"id": 6,
35+
"first_name": "Delano",
36+
"last_name": "Millins",
37+
"email": "dmillins5@yahoo.com"
38+
},
39+
{
40+
"id": 7,
41+
"first_name": "Allister",
42+
"last_name": "Rizzetti",
43+
"email": "arizzetti6@yahoo.com"
44+
},
45+
{
46+
"id": 8,
47+
"first_name": "Yul",
48+
"last_name": "Slite",
49+
"email": "yslite7@yahoo.com"
50+
},
51+
{
52+
"id": 9,
53+
"first_name": "Stu",
54+
"last_name": "Liveing",
55+
"email": "sliveing8@devmountain.com"
56+
},
57+
{
58+
"id": 10,
59+
"first_name": "Carla",
60+
"last_name": "Gisbye",
61+
"email": "cgisbye9@devmountain.com"
62+
}
63+
];
64+
65+
66+
// Get Controller
67+
const sendUsers = (req, res) => {
68+
res.status(200).send(users);
69+
};
70+
71+
// Post Controller
72+
const addUser = (req, res) => {
73+
// get user obj from body
74+
const {user} = req.body;
75+
// add user to the array
76+
users = users.push(user);
77+
// send new users array
78+
res.status(200).send(user);
79+
}
80+
81+
// Put Controller
82+
const updateUser = (req, res) => {
83+
// take id from params
84+
const {id} = req.params;
85+
// find the user and update it
86+
for(let i = 0; i < users.length; i++){
87+
if(users[i].id === +id){
88+
users[i].first_name = 'New Name'
89+
}
90+
};
91+
// send users back
92+
res.status(200).send(users);
93+
};
94+
95+
// Delete Controller
96+
const deleteUser = (req, res) => {
97+
// take id from query
98+
const {id} = req.query;
99+
// find user and delete it
100+
const updatedUsers = users.filter(user => user.id !== +id);
101+
// return updated users
102+
return updatedUsers;
103+
}
104+
105+
// Export The Controller Functions
106+
module.exports = {
107+
sendUsers,
108+
addUser,
109+
updateUser,
110+
deleteUser
111+
};

index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const express = require('express');
2+
const cors = require('cors');
3+
// require the controllers
4+
const ctrl = require('./controller');
5+
6+
// Setup App
7+
const app = express();
8+
9+
// Top Level Middleware
10+
app.use(express.json());
11+
app.use(cors());
12+
13+
// End Points
14+
app.get('/api/users', ctrl.sendUsers);
15+
app.post('/api/users/add', ctrl.addUser);
16+
app.put('/api/users/update/:id', ctrl.updateUser);
17+
app.delete('/api/user');
18+
19+
// Server Listening
20+
app.listen(3005, () => {
21+
console.log('Server running!')
22+
});

0 commit comments

Comments
 (0)