Skip to content

Commit ddfdb90

Browse files
authored
Merge pull request #7 from vitabletech/develop
Develop
2 parents 2115c09 + 4bc0d5f commit ddfdb90

File tree

10 files changed

+550
-21
lines changed

10 files changed

+550
-21
lines changed

Diff for: README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Node App Setup Guide
22

3-
This guide will help you set up and run the Node.js application along with PHPMyAdmin using Docker.
3+
This guide will help you set up and run the Node.js application along with PHPMyAdmin using Docker with the added convenience of a development container.
44

55
## Prerequisites
66

77
- Git
8-
- Docker
9-
- Node.js
8+
- [Docker](https://www.docker.com/)
9+
- [Node.js](https://nodejs.org/en)
10+
- [Visual Studio Code (or any editor with DevContainer support)](https://code.visualstudio.com/)
1011

1112
## Installation
1213

@@ -32,7 +33,7 @@ cd node-express-api-bootstrap
3233
docker-compose up -d
3334
```
3435

35-
2. Open your web browser and go to [http://localhost:8080](http://localhost:8080) to access PHPMyAdmin.
36+
2. Open your web browser and go to [http://localhost:8081](http://localhost:8081) to access PHPMyAdmin.
3637

3738
3. Create a new database named `testdb` as specified in the `.env` file. If you made any changes to the database name, ensure to create it with the updated name.
3839

@@ -52,7 +53,7 @@ This command will automatically create the required tables.
5253

5354
6. Once the database synchronization is complete, you can start working on the application.
5455

55-
7. Access the Node.js application at [http://localhost/](http://localhost/).
56+
7. Access the Node.js application at [http://localhost:8080](http://localhost:8080).
5657

5758
## Enjoy!
5859

@@ -72,4 +73,4 @@ This repository provides a Node.js application for demonstrating JWT authenticat
7273
- Utilizes Sequelize ORM for database interactions
7374
- Supports MySQL for database storage
7475

75-
## [Connect to us](https://topmate.io/vitabletech)
76+
## [Connect to us](https://topmate.io/vitabletech)

Diff for: app/controllers/guestController.js

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import db from '../models/index.js';
2+
3+
const { guests: Guest } = db;
4+
5+
// Method to add new guests
6+
export const addGuest = (req, res) => {
7+
// Destructure required fields from the request body
8+
const { name, contact, email, address } = req.body;
9+
10+
// Check if the required fields are provided
11+
if (!name || !contact) {
12+
return res.status(400).json({ error: 'Name and contact are mandatory fields.' });
13+
}
14+
15+
// Create a new Guest object with provided data
16+
return Guest.create({
17+
name,
18+
contact,
19+
email, // email not a mandatory parameter
20+
address // It's okay if these are undefined
21+
})
22+
.then(savedGuest =>
23+
// Respond with the saved guest object
24+
res.status(200).json(savedGuest)
25+
)
26+
.catch(err => {
27+
console.error('Error saving guest:', err);
28+
return res.status(500).send('Error saving guest.');
29+
});
30+
};
31+
32+
// Method to delete a guest
33+
export const deleteGuest = (req, res) => {
34+
const guestId = req.query.guestid;;
35+
36+
Guest.destroy({
37+
where: { guestId }
38+
})
39+
.then(num => {
40+
if (num === 1) {
41+
res.send({ message: 'Guest was deleted successfully!' });
42+
} else {
43+
res.send({ message: `Cannot delete Guest with id=${guestId}. Maybe Guest was not found!` });
44+
}
45+
})
46+
.catch(() => {
47+
res.status(500).send({ message: `Could not delete Guest with id=${ guestId}` });
48+
});
49+
};
50+
51+
// Method to update details of an existing guest
52+
export const updateGuest = (req, res) => {
53+
const { name, contact, email, address, guestId } = req.body;
54+
const updateData = { name, contact };
55+
if (email !== undefined) {
56+
updateData.email = email;
57+
}
58+
if (address !== undefined) {
59+
updateData.address = address;
60+
}
61+
Guest.update(
62+
updateData,
63+
{ where: { guestId } }
64+
)
65+
.then(([affectedCount]) => {
66+
if (affectedCount > 0) {
67+
res.send({ message: 'Guest updated successfully!' });
68+
} else {
69+
res.send({ message: `Cannot update Guest with id=${guestId}. Maybe Guest was not found or req.body is empty!` });
70+
}
71+
})
72+
.catch((err) => {
73+
console.error('Error updating guest:', err);
74+
res.status(500).send({ message: `Error updating Guest with id=${ guestId}` });
75+
});
76+
};

Diff for: app/models/Guest.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export default (sequelize, Sequelize) => {
2+
const Guest = sequelize.define('guests', {
3+
guestId: {
4+
type: Sequelize.INTEGER,
5+
autoIncrement: true,
6+
primaryKey: true
7+
},
8+
name: {
9+
type: Sequelize.STRING
10+
11+
},
12+
contact: {
13+
type: Sequelize.STRING
14+
},
15+
email: {
16+
type: Sequelize.STRING
17+
},
18+
address: {
19+
type: Sequelize.STRING
20+
}
21+
});
22+
23+
return Guest;
24+
};

Diff for: app/models/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Sequelize from 'sequelize';
22
import config from '../config/db.config.js';
33
import createUserModel from './User.js';
44
import createRoleModel from './Role.js';
5+
import createGuestModel from './Guest.js';
56

67
const { DB, USER, PASSWORD, HOST, DIALECT, pool } = config;
78

@@ -16,6 +17,7 @@ const db = {
1617
sequelize,
1718
user: createUserModel(sequelize, Sequelize),
1819
role: createRoleModel(sequelize, Sequelize),
20+
guests: createGuestModel(sequelize, Sequelize),
1921
ROLES: ['user', 'admin']
2022
};
2123

Diff for: app/routes/Router.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import authRoutes from './auth.routes.js';
22
import userRoutes from './user.routes.js';
3+
import guestRoutes from './guest.routes.js';
34

45
const applyRoutes = (app) => {
56
// simple route
@@ -10,6 +11,7 @@ const applyRoutes = (app) => {
1011
// routes
1112
authRoutes(app);
1213
userRoutes(app);
14+
guestRoutes(app);
1315
};
1416

1517
export default applyRoutes;

Diff for: app/routes/guest.routes.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import express from 'express'; // Import express
2+
import { addGuest, deleteGuest, updateGuest } from '../controllers/guestController.js'; // Import addGuest function
3+
4+
export default function guestRoutes(app) {
5+
app.use((req, res, next) => {
6+
res.header(
7+
'Access-Control-Allow-Headers',
8+
'x-access-token, Origin, Content-Type, Accept'
9+
);
10+
next();
11+
});
12+
app.use(express.json());
13+
app.post('/addguest', addGuest);
14+
app.delete('/deleteguest', deleteGuest);
15+
app.put('/updateguest',updateGuest);
16+
17+
}

Diff for: coverage/lcov.info

+114-12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,74 @@ BRF:6
7676
BRH:0
7777
end_of_record
7878
TN:
79+
SF:app/controllers/guestController.js
80+
FN:6,(anonymous_0)
81+
FN:22,(anonymous_1)
82+
FN:26,(anonymous_2)
83+
FN:33,(anonymous_3)
84+
FN:39,(anonymous_4)
85+
FN:46,(anonymous_5)
86+
FN:52,(anonymous_6)
87+
FN:65,(anonymous_7)
88+
FN:72,(anonymous_8)
89+
FNF:9
90+
FNH:0
91+
FNDA:0,(anonymous_0)
92+
FNDA:0,(anonymous_1)
93+
FNDA:0,(anonymous_2)
94+
FNDA:0,(anonymous_3)
95+
FNDA:0,(anonymous_4)
96+
FNDA:0,(anonymous_5)
97+
FNDA:0,(anonymous_6)
98+
FNDA:0,(anonymous_7)
99+
FNDA:0,(anonymous_8)
100+
DA:3,0
101+
DA:6,0
102+
DA:8,0
103+
DA:11,0
104+
DA:12,0
105+
DA:16,0
106+
DA:24,0
107+
DA:27,0
108+
DA:28,0
109+
DA:33,0
110+
DA:34,0
111+
DA:36,0
112+
DA:40,0
113+
DA:41,0
114+
DA:43,0
115+
DA:47,0
116+
DA:52,0
117+
DA:53,0
118+
DA:54,0
119+
DA:55,0
120+
DA:56,0
121+
DA:58,0
122+
DA:59,0
123+
DA:61,0
124+
DA:66,0
125+
DA:67,0
126+
DA:69,0
127+
DA:73,0
128+
DA:74,0
129+
LF:29
130+
LH:0
131+
BRDA:11,0,0,0
132+
BRDA:11,0,1,0
133+
BRDA:11,1,0,0
134+
BRDA:11,1,1,0
135+
BRDA:40,2,0,0
136+
BRDA:40,2,1,0
137+
BRDA:55,3,0,0
138+
BRDA:55,3,1,0
139+
BRDA:58,4,0,0
140+
BRDA:58,4,1,0
141+
BRDA:66,5,0,0
142+
BRDA:66,5,1,0
143+
BRF:12
144+
BRH:0
145+
end_of_record
146+
TN:
79147
SF:app/controllers/userController.js
80148
FN:1,(anonymous_0)
81149
FN:5,(anonymous_1)
@@ -177,6 +245,19 @@ BRF:8
177245
BRH:0
178246
end_of_record
179247
TN:
248+
SF:app/models/Guest.js
249+
FN:1,(anonymous_0)
250+
FNF:1
251+
FNH:0
252+
FNDA:0,(anonymous_0)
253+
DA:2,0
254+
DA:23,0
255+
LF:2
256+
LH:0
257+
BRF:0
258+
BRH:0
259+
end_of_record
260+
TN:
180261
SF:app/models/Role.js
181262
FN:1,(anonymous_0)
182263
FNF:1
@@ -206,31 +287,32 @@ TN:
206287
SF:app/models/index.js
207288
FNF:0
208289
FNH:0
209-
DA:6,0
210-
DA:8,0
211-
DA:14,0
212-
DA:22,0
213-
DA:25,0
214-
DA:29,0
290+
DA:7,0
291+
DA:9,0
292+
DA:15,0
293+
DA:24,0
294+
DA:27,0
295+
DA:31,0
215296
LF:6
216297
LH:0
217298
BRF:0
218299
BRH:0
219300
end_of_record
220301
TN:
221302
SF:app/routes/Router.js
222-
FN:4,(anonymous_0)
223-
FN:6,(anonymous_1)
303+
FN:5,(anonymous_0)
304+
FN:7,(anonymous_1)
224305
FNF:2
225306
FNH:0
226307
FNDA:0,(anonymous_0)
227308
FNDA:0,(anonymous_1)
228-
DA:4,0
229-
DA:6,0
309+
DA:5,0
230310
DA:7,0
231-
DA:11,0
311+
DA:8,0
232312
DA:12,0
233-
LF:5
313+
DA:13,0
314+
DA:14,0
315+
LF:6
234316
LH:0
235317
BRF:0
236318
BRH:0
@@ -255,6 +337,26 @@ BRF:0
255337
BRH:0
256338
end_of_record
257339
TN:
340+
SF:app/routes/guest.routes.js
341+
FN:4,guestRoutes
342+
FN:5,(anonymous_1)
343+
FNF:2
344+
FNH:0
345+
FNDA:0,guestRoutes
346+
FNDA:0,(anonymous_1)
347+
DA:5,0
348+
DA:6,0
349+
DA:10,0
350+
DA:12,0
351+
DA:13,0
352+
DA:14,0
353+
DA:15,0
354+
LF:7
355+
LH:0
356+
BRF:0
357+
BRH:0
358+
end_of_record
359+
TN:
258360
SF:app/routes/index.js
259361
FN:5,(anonymous_0)
260362
FN:12,(anonymous_1)

Diff for: package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "node-js-jwt-auth",
3-
"version": "1.0.0",
4-
"description": "Node.js Demo for JWT Authentication",
2+
"name": "node-express-api-bootstrap",
3+
"version": "2.0.0",
4+
"description": "This repository provides a Node.js application for demonstrating JWT authentication and authorization. It includes features such as user registration, user login, and authorization process. The application is built using Express.js, JWT for authentication, Sequelize ORM for database interactions, and MySQL for database storage.",
55
"main": "app/server.js",
66
"type": "module",
77
"scripts": {

0 commit comments

Comments
 (0)