Skip to content

Commit b0636b6

Browse files
committed
first commit
0 parents  commit b0636b6

29 files changed

+1660
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.git
2+
node_modules/

.sequelizerc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
"config": path.resolve('./config', 'config.json'),
5+
"models-path": path.resolve('./models'),
6+
"seeders-path": path.resolve('./seeders'),
7+
"migrations-path": path.resolve('./migrations')
8+
};

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Node.js, Express.js, Sequelize.js and PostgreSQL RESTful API
2+
3+
This source code is part of [Node.js, Express.js, Sequelize.js and PostgreSQL RESTful API]() tutorial.
4+
5+
To run locally:
6+
7+
* Make sure you have install and run PostgreSQL server
8+
* Create database with the name same as in config file
9+
* Run `npm install` or `yarn install`
10+
* Run `sequelize db:migrate`
11+
* Run `nodemon` or `npm start`

app.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
var createError = require('http-errors');
2+
var express = require('express');
3+
var path = require('path');
4+
var cookieParser = require('cookie-parser');
5+
var logger = require('morgan');
6+
7+
var indexRouter = require('./routes/index');
8+
var usersRouter = require('./routes/users');
9+
10+
var app = express();
11+
12+
// view engine setup
13+
app.set('views', path.join(__dirname, 'views'));
14+
app.set('view engine', 'ejs');
15+
16+
app.use(logger('dev'));
17+
app.use(express.json());
18+
app.use(express.urlencoded({ extended: false }));
19+
app.use(cookieParser());
20+
app.use(express.static(path.join(__dirname, 'public')));
21+
22+
app.use('/', indexRouter);
23+
app.use('/users', usersRouter);
24+
25+
// catch 404 and forward to error handler
26+
app.use(function(req, res, next) {
27+
res.status(404).send({ error: 'Not found' })
28+
});
29+
30+
// error handler
31+
app.use(function(err, req, res, next) {
32+
// set locals, only providing error in development
33+
res.locals.message = err.message;
34+
res.locals.error = req.app.get('env') === 'development' ? err : {};
35+
res.status(err.status || 500).send({ error: err })
36+
});
37+
38+
module.exports = app;

bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('node-sequelize:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

config/config.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"development": {
3+
"username": "djamware",
4+
"password": "dj@mw@r3",
5+
"database": "node_sequelize",
6+
"host": "127.0.0.1",
7+
"dialect": "postgres"
8+
},
9+
"test": {
10+
"username": "root",
11+
"password": "dj@mw@r3",
12+
"database": "node_sequelize",
13+
"host": "127.0.0.1",
14+
"dialect": "postgres"
15+
},
16+
"production": {
17+
"username": "root",
18+
"password": "dj@mw@r3",
19+
"database": "node_sequelize",
20+
"host": "127.0.0.1",
21+
"dialect": "postgres"
22+
}
23+
}

controllers/classroom.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
const Classroom = require('../models').Classroom;
2+
const Student = require('../models').Student;
3+
4+
module.exports = {
5+
list(req, res) {
6+
return Classroom
7+
.findAll({
8+
include: [{
9+
model: Student,
10+
as: 'students'
11+
}],
12+
order: [
13+
['createdAt', 'DESC'],
14+
[{ model: Student, as: 'students' }, 'createdAt', 'DESC'],
15+
],
16+
})
17+
.then((classrooms) => res.status(200).send(classrooms))
18+
.catch((error) => { res.status(400).send(error); });
19+
},
20+
21+
getById(req, res) {
22+
return Classroom
23+
.findById(req.params.id, {
24+
include: [{
25+
model: Student,
26+
as: 'students'
27+
}],
28+
})
29+
.then((classroom) => {
30+
if (!classroom) {
31+
return res.status(404).send({
32+
message: 'Classroom Not Found',
33+
});
34+
}
35+
return res.status(200).send(classroom);
36+
})
37+
.catch((error) => res.status(400).send(error));
38+
},
39+
40+
add(req, res) {
41+
return Classroom
42+
.create({
43+
class_name: req.body.class_name,
44+
})
45+
.then((classroom) => res.status(201).send(classroom))
46+
.catch((error) => res.status(400).send(error));
47+
},
48+
49+
addWithStudents(req, res) {
50+
return Classroom
51+
.create({
52+
class_name: req.body.class_name,
53+
students: req.body.students,
54+
}, {
55+
include: [{
56+
model: Student,
57+
as: 'students'
58+
}]
59+
})
60+
.then((classroom) => res.status(201).send(classroom))
61+
.catch((error) => res.status(400).send(error));
62+
},
63+
64+
update(req, res) {
65+
return Classroom
66+
.findById(req.params.id, {
67+
include: [{
68+
model: Student,
69+
as: 'students'
70+
}],
71+
})
72+
.then(classroom => {
73+
if (!classroom) {
74+
return res.status(404).send({
75+
message: 'Classroom Not Found',
76+
});
77+
}
78+
return classroom
79+
.update({
80+
class_name: req.body.class_name || classroom.class_name,
81+
})
82+
.then(() => res.status(200).send(classroom))
83+
.catch((error) => res.status(400).send(error));
84+
})
85+
.catch((error) => res.status(400).send(error));
86+
},
87+
88+
delete(req, res) {
89+
return Classroom
90+
.findById(req.params.id)
91+
.then(classroom => {
92+
if (!classroom) {
93+
return res.status(400).send({
94+
message: 'Classroom Not Found',
95+
});
96+
}
97+
return classroom
98+
.destroy()
99+
.then(() => res.status(204).send())
100+
.catch((error) => res.status(400).send(error));
101+
})
102+
.catch((error) => res.status(400).send(error));
103+
},
104+
};

controllers/course.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const Course = require('../models').Course;
2+
const Student = require('../models').Student;
3+
const Lecturer = require('../models').Lecturer;
4+
5+
module.exports = {
6+
list(req, res) {
7+
return Course
8+
.findAll({
9+
include: [{
10+
model: Student,
11+
as: 'students'
12+
},{
13+
model: Lecturer,
14+
as: 'lecturer'
15+
}],
16+
order: [
17+
['createdAt', 'DESC'],
18+
[{ model: Student, as: 'students' }, 'createdAt', 'DESC'],
19+
],
20+
})
21+
.then((courses) => res.status(200).send(courses))
22+
.catch((error) => { res.status(400).send(error); });
23+
},
24+
25+
getById(req, res) {
26+
return Course
27+
.findById(req.params.id, {
28+
include: [{
29+
model: Course,
30+
as: 'course'
31+
}],
32+
})
33+
.then((course) => {
34+
if (!course) {
35+
return res.status(404).send({
36+
message: 'Course Not Found',
37+
});
38+
}
39+
return res.status(200).send(course);
40+
})
41+
.catch((error) => res.status(400).send(error));
42+
},
43+
44+
add(req, res) {
45+
return Course
46+
.create({
47+
course_name: req.body.course_name,
48+
})
49+
.then((course) => res.status(201).send(course))
50+
.catch((error) => res.status(400).send(error));
51+
},
52+
53+
update(req, res) {
54+
return Course
55+
.findById(req.params.id, {
56+
include: [{
57+
model: Course,
58+
as: 'course'
59+
}],
60+
})
61+
.then(course => {
62+
if (!course) {
63+
return res.status(404).send({
64+
message: 'Course Not Found',
65+
});
66+
}
67+
return course
68+
.update({
69+
course_name: req.body.course_name || classroom.course_name,
70+
})
71+
.then(() => res.status(200).send(course))
72+
.catch((error) => res.status(400).send(error));
73+
})
74+
.catch((error) => res.status(400).send(error));
75+
},
76+
77+
delete(req, res) {
78+
return Course
79+
.findById(req.params.id)
80+
.then(course => {
81+
if (!course) {
82+
return res.status(400).send({
83+
message: 'Course Not Found',
84+
});
85+
}
86+
return course
87+
.destroy()
88+
.then(() => res.status(204).send())
89+
.catch((error) => res.status(400).send(error));
90+
})
91+
.catch((error) => res.status(400).send(error));
92+
},
93+
};

controllers/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const classroom = require('./classroom');
2+
const student = require('./student');
3+
const lecturer = require('./lecturer');
4+
const course = require('./course');
5+
6+
module.exports = {
7+
classroom,
8+
student,
9+
lecturer,
10+
course,
11+
};

0 commit comments

Comments
 (0)