Skip to content

Commit 2dc8055

Browse files
committed
ATOMIC DEISGN BITCHEEEEEEEEEEEEE
1 parent f29eb9b commit 2dc8055

File tree

14 files changed

+200
-1
lines changed

14 files changed

+200
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Action that returns a HTTP 200's response
3+
*/
4+
5+
const Action = (err, data, res) => {
6+
if (err) {
7+
res.writeHead(403, {'Content-Type': 'application/json'});
8+
return res.end(JSON.stringify({error: err}));
9+
}
10+
11+
res.writeHead(200, {'Content-Type': 'application/json'});
12+
return res.end(JSON.stringify(data));
13+
};
14+
15+
module.exports = Action;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Action that creates an user
3+
*/
4+
5+
'use strict';
6+
7+
const callback = require('./action-callback-http-200');
8+
9+
module.exports = (Organism) => {
10+
return (req, res) => {
11+
let queryData = '';
12+
13+
req.on('data', (data) => {
14+
queryData += data;
15+
});
16+
17+
req.on('end', () => {
18+
const obj = querystring.parse(queryData);
19+
20+
Organism.create(obj, (err, user) => callback(err, user, res));
21+
});
22+
};
23+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Action that finds an user
3+
*/
4+
5+
const getQuery = require('./action-get-query-http');
6+
const callback = require('./action-callback-http-200');
7+
8+
module.exports = (Organism) => {
9+
return (req, res) => {
10+
const query = getQuery(req.url);
11+
12+
Organism.find(query, (err, users) => callback(err, users, res));
13+
};
14+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Action that finds the first user
3+
*/
4+
5+
const getQuery = require('./action-get-query-http');
6+
const callback = require('./action-callback-http-200');
7+
8+
module.exports = (Organism) => {
9+
return (req, res) => {
10+
const query = getQuery(req.url);
11+
12+
Organism.findOne(query, (err, user) => callback(err, user, res));
13+
};
14+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Action that gets querystring through URL
3+
*/
4+
5+
const Action = (_url) => {
6+
const url_parts = url.parse(_url);
7+
return querystring.parse(url_parts.query);
8+
};
9+
10+
module.exports = Action;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Action that removes an user
3+
*/
4+
5+
const getQuery = require('./action-get-query-http');
6+
const callback = require('./action-callback-http-200');
7+
8+
module.exports = (Organism) => {
9+
return (req, res) => {
10+
const query = getQuery(req.url);
11+
12+
Organism.remove(query, (err, user) => callback(err, user, res));
13+
};
14+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Action that updates an user
3+
*/
4+
5+
'use strict';
6+
7+
const getQuery = require('./action-get-query-http');
8+
const callback = require('./action-callback-http-200');
9+
10+
module.exports = (Organism) => {
11+
return (req, res) => {
12+
let queryData = '';
13+
14+
req.on('data', (data) => {
15+
queryData += data;
16+
});
17+
18+
req.on('end', () => {
19+
const query = getQuery(req);
20+
const mod = querystring.parse(queryData);
21+
22+
Organism.update(query, mod, {multi: true}, (err, data) => callback(err, data, res));
23+
});
24+
};
25+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Atom field name
3+
*/
4+
5+
const Atom = {
6+
type: String
7+
, get: require('../quarks/quark-toUpper')
8+
, set: require('../quarks/quark-toLower')
9+
, validate: require('../quarks/quark-validate-string-lengthGTE3')
10+
, required: true
11+
, index: true
12+
};
13+
14+
console.log(Atom);
15+
16+
module.exports = Atom;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* User's Schema/Molecule
3+
*/
4+
5+
const mongoose = require('mongoose');
6+
const Schema = mongoose.Schema;
7+
8+
const Molecule = {
9+
name: require('../atoms/atom-name')
10+
};
11+
12+
module.exports = new Schema(Molecule);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* User's Organism/Model
3+
*/
4+
5+
'use strict';
6+
7+
const mongoose = require('mongoose');
8+
const Molecule = require('../molecules/molecule-user');
9+
const Organism = mongoose.model('User', Molecule);
10+
const create = require('../actions/action-create')(Organism);
11+
const update = require('../actions/action-update')(Organism);
12+
const find = require('../actions/action-find')(Organism);
13+
const findOne = require('../actions/action-findOne')(Organism);
14+
const remove = require('../actions/action-remove')(Organism);
15+
16+
const CRUD = {
17+
create
18+
, find
19+
, findOne
20+
, update
21+
, remove
22+
};
23+
24+
module.exports = CRUD;

0 commit comments

Comments
 (0)