Skip to content

Commit 8364332

Browse files
committed
COMMIIIITA PORRA
1 parent f43e9bc commit 8364332

File tree

11 files changed

+300
-1
lines changed

11 files changed

+300
-1
lines changed

nodejs/class-8/mongoose-middleware/docs_parallel/models.log

Whitespace-only changes.

nodejs/class-8/mongoose-user/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const http = require('http');
66
const url = require('url');
7-
const Controller = require('./controller');
7+
const Controller = require('../controller');
88

99
var server = http.createServer(function (req, res) {
1010
// var msg = "";
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Main stuff
3+
*/
4+
5+
const http = require('http');
6+
const url = require('url');
7+
const PokemonController = require('./controllers/Pokemon');
8+
const MyPokemonController = require('./controllers/MyPokemon');
9+
10+
var server = http.createServer(function (req, res) {
11+
var url_parts = url.parse(req.url);
12+
13+
switch (url_parts.pathname) {
14+
case '/api/pokemons/create' :
15+
PokemonController.create(req, res);
16+
break;
17+
case '/api/mypokemons/create' :
18+
MyPokemonController.create(req, res);
19+
break;
20+
case '/api/pokemons/find' :
21+
PokemonController.find(req, res);
22+
break;
23+
default :
24+
res.end('Path not found');
25+
break;
26+
}
27+
});
28+
29+
server.listen(8080, () => {
30+
console.log('Server running in localhost:8080');
31+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* User's Controller
3+
*/
4+
5+
const PokemonModel = require('../models/MyPokemons');
6+
7+
const CRUD = {
8+
create: PokemonModel.create
9+
, find: PokemonModel.find
10+
, findOne: PokemonModel.findOne
11+
, update: PokemonModel.update
12+
, remove: PokemonModel.remove
13+
};
14+
15+
module.exports = CRUD;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* User's Controller
3+
*/
4+
5+
const PokemonModel = require('../models/Pokemon');
6+
7+
const CRUD = {
8+
create: PokemonModel.create
9+
, find: PokemonModel.find
10+
, findOne: PokemonModel.findOne
11+
, update: PokemonModel.update
12+
, remove: PokemonModel.remove
13+
};
14+
15+
module.exports = CRUD;
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## Exercises
2+
3+
### 1 - Insira 5 pokemons novos, na coleção pokemons, escolha 3 e os adicione em um array e uma nova coleção chamada meus-pokemons, utilizando o ObjectId. Adicione o required em campos que ache obrigatório no Schema do Pokemon.
4+
5+
Inserting 5 pokemon:
6+
7+
```
8+
const create = (req, res) => {
9+
req.on('end', () => {
10+
var pokemons = [
11+
{
12+
name : "Digimon",
13+
type : "Digitype"
14+
},
15+
{
16+
name : "Pkemon",
17+
type : "Pkemontype"
18+
},
19+
{
20+
name : "Chrismon",
21+
type : "Christype"
22+
},
23+
{
24+
name : "Chairmon",
25+
type : "Chairtype"
26+
},
27+
{
28+
name : "Cumon",
29+
type : "Cutype"
30+
}
31+
];
32+
33+
Pokemon.create(pokemons, (err, user) => callback(err, user, res));
34+
});
35+
};
36+
37+
```
38+
39+
Picking 3 of them by its _id:
40+
41+
```
42+
[
43+
{
44+
"_id": "57c771f97005287706a5d283",
45+
},
46+
{
47+
"_id": "57c771f97005287706a5d284",
48+
},
49+
{
50+
"_id": "57c771f97005287706a5d285",
51+
}
52+
]
53+
```
54+
55+
### 2 - Crie um Schema de exemplo com validação para os campos (utilizar Arquitetura Atômica, ou seja cada campo sendo um Schema separado):
56+
57+
### 3 - Dê 3 exemplos diferentes, de cada, utilizando as funções:
58+
59+
- findAndModify
60+
- findOneAndUpdate
61+
- findOneAndRemove
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* User's Model
3+
*/
4+
5+
'use strict';
6+
7+
const mongoose = require('mongoose');
8+
const querystring = require('querystring');
9+
const Schema = require('../schemas/MyPokemons');
10+
const url = require('url');
11+
12+
const Pokemon = mongoose.model('MyPokemon', Schema);
13+
14+
const callback = (err, data, res) => {
15+
if (err) {
16+
res.writeHead(403, {'Content-Type': 'application/json'});
17+
return res.end(JSON.stringify({error: err}));
18+
}
19+
20+
res.writeHead(200, {'Content-Type': 'application/json'});
21+
return res.end(JSON.stringify(data));
22+
};
23+
24+
const getQuery = (_url) => {
25+
const url_parts = url.parse(_url);
26+
return querystring.parse(url_parts.query);
27+
};
28+
29+
const create = (req, res) => {
30+
let queryData = '';
31+
32+
req.on('data', (data) => {
33+
queryData += data;
34+
});
35+
36+
req.on('end', () => {
37+
var pokemons = [
38+
{
39+
"pokemon": "57c771f97005287706a5d283",
40+
},
41+
{
42+
"pokemon": "57c771f97005287706a5d284",
43+
},
44+
{
45+
"pokemon": "57c771f97005287706a5d285",
46+
}
47+
];
48+
49+
Pokemon.create(pokemons, (err, user) => callback(err, user, res));
50+
});
51+
};
52+
53+
const CRUD = {
54+
create
55+
};
56+
57+
module.exports = CRUD;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* User's Model
3+
*/
4+
5+
'use strict';
6+
7+
const mongoose = require('mongoose');
8+
const querystring = require('querystring');
9+
const Schema = require('../schemas/Pokemon');
10+
const url = require('url');
11+
12+
const Pokemon = mongoose.model('Pokemon', Schema);
13+
14+
const callback = (err, data, res) => {
15+
if (err) {
16+
res.writeHead(403, {'Content-Type': 'application/json'});
17+
return res.end(JSON.stringify({error: err}));
18+
}
19+
20+
res.writeHead(200, {'Content-Type': 'application/json'});
21+
return res.end(JSON.stringify(data));
22+
};
23+
24+
const getQuery = (_url) => {
25+
const url_parts = url.parse(_url);
26+
return querystring.parse(url_parts.query);
27+
};
28+
29+
const create = (req, res) => {
30+
let queryData = '';
31+
32+
req.on('data', (data) => {
33+
queryData += data;
34+
});
35+
36+
req.on('end', () => {
37+
// const obj = querystring.parse(queryData);
38+
39+
var pokemons = [
40+
{
41+
name : "Digimon",
42+
type : "Digitype"
43+
},
44+
{
45+
name : "Pkemon",
46+
type : "Pkemontype"
47+
},
48+
{
49+
name : "Chrismon",
50+
type : "Christype"
51+
},
52+
{
53+
name : "Chairmon",
54+
type : "Chairtype"
55+
},
56+
{
57+
name : "Cumon",
58+
type : "Cutype"
59+
}
60+
];
61+
62+
Pokemon.create(pokemons, (err, user) => callback(err, user, res));
63+
});
64+
};
65+
66+
const find = (req, res) => {
67+
const query = getQuery(req.url);
68+
Pokemon.find(query, (err, pokemons) => callback(err, pokemons, res));
69+
};
70+
71+
const CRUD = {
72+
create
73+
, find
74+
};
75+
76+
module.exports = CRUD;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* User's Schema
3+
*/
4+
5+
'use strict';
6+
7+
const mongoose = require('mongoose');
8+
mongoose.connect('mongodb://localhost/pokemons');
9+
10+
const Schema = mongoose.Schema;
11+
12+
const _schema = {
13+
my_pokemons: [{
14+
pokemon: Schema.Types.ObjectId,
15+
ref: 'pokemons'
16+
}]
17+
};
18+
19+
const pokemonSchema = new Schema(_schema);
20+
21+
module.exports = pokemonSchema;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* User's Schema
3+
*/
4+
5+
'use strict';
6+
7+
const mongoose = require('mongoose');
8+
mongoose.connect('mongodb://localhost/pokemons');
9+
10+
const Schema = mongoose.Schema;
11+
12+
const _schema = {
13+
name: {type : String}
14+
, type : {type : String}
15+
};
16+
17+
const pokemonSchema = new Schema(_schema);
18+
19+
module.exports = pokemonSchema;

0 commit comments

Comments
 (0)