Skip to content

Commit b9fca89

Browse files
committed
POPULATING
1 parent 94cad1b commit b9fca89

File tree

12 files changed

+149
-0
lines changed

12 files changed

+149
-0
lines changed

nodejs/class-11/part-3/class-express-generator/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const paginate = require('express-paginate');
1313
const routes = require('./routes/index');
1414
const users = require('./routes/users');
1515
const UsersAPI = require('./modules/Users');
16+
const ClassAPI = require('./modules/Class');
1617

1718
const app = express();
1819

@@ -35,6 +36,7 @@ app.use('/users', users);
3536

3637
// JSON API
3738
app.use('/api/v1/users', UsersAPI);
39+
app.use('/api/v1/class', ClassAPI);
3840

3941
// catch 404 and forward to error handler
4042
app.use(function (req, res, next) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'use strict';
2+
3+
const Organism = require('./organisms/organism-class');
4+
const paginate = require('express-paginate');
5+
6+
const callback = (err, data, res)=> {
7+
if (err) return res.json(err);
8+
9+
return res.json(data);
10+
};
11+
12+
const Actions = {};
13+
14+
Actions.list = (req, res) => {
15+
const query = {};
16+
17+
// Organism.paginate(query, {page: req.query.page, limit: req.query.limit}, (err, data, pageCount, itemCount)=> {
18+
// var obj = {
19+
// has_next_page: data.page < data.pages ? true : false
20+
// , data: data
21+
// };
22+
// callback(err, obj, res);
23+
// });
24+
25+
Organism
26+
.find(query)
27+
.populate('students' , 'name email')
28+
.exec((err, data)=> {
29+
callback(err, data, res);
30+
})
31+
};
32+
33+
Actions.create = (req, res) => {
34+
const query = {};
35+
const body = req.body;
36+
const new_class = new Organism(body);
37+
38+
new_class.save((err, data)=> {
39+
callback(err, data, res);
40+
});
41+
};
42+
43+
module.exports = Actions;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Atom field name
3+
*/
4+
5+
const Atom = {
6+
type: String
7+
, set: require('../quarks/quark-toLower')
8+
, validate: require('../quarks/quark-validate-string-lengthGTE3')
9+
, required: true
10+
, index: true
11+
};
12+
13+
module.exports = Atom;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Atom field user
3+
*/
4+
5+
const mongoose = require('mongoose');
6+
const Schema = mongoose.Schema;
7+
8+
const Atom = {
9+
type: Schema.Types.ObjectId
10+
, ref: 'User'
11+
, required: true
12+
};
13+
14+
module.exports = Atom;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const router = require('./routerExpress');
4+
const Actions = require('./actions');
5+
const Routes = require('./routes')(Actions);
6+
const Router = require('./routesExpress')(Routes, router);
7+
8+
module.exports = Router;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
const Schema = mongoose.Schema;
5+
const paginate = require('mongoose-paginate');
6+
7+
const Molecule = {
8+
name: require('../atoms/atom-name')
9+
, students: [require('../atoms/atom-user')]
10+
};
11+
12+
const MoleculeSchema = new Schema(Molecule);
13+
MoleculeSchema.plugin(paginate);
14+
15+
module.exports = MoleculeSchema;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
const mongoose = require('mongoose');
4+
const classSchema = require('../molecule/molecule-class');
5+
6+
const Organism = mongoose.model('Class', classSchema);
7+
8+
module.exports = Organism;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Quark toLower
3+
*
4+
* Every single field that calls it will display itself
5+
* in lowercase letters.
6+
*/
7+
8+
module.exports = (v) => v.toLowerCase();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Will validate a string to get its length
3+
* greater than 3 characters.
4+
*/
5+
6+
module.exports = (v) => v.length > 3;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = require('express').Router();

0 commit comments

Comments
 (0)