Skip to content

Commit fdec47c

Browse files
authored
Merge pull request #3 from lucasDSBR/criando-lists-e-rotas
Criando Listis e Rotas
2 parents 9a2e5f2 + c4f7b82 commit fdec47c

File tree

15 files changed

+386
-107
lines changed

15 files changed

+386
-107
lines changed

Server/Server.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ export class Server {
1010
application: restify.Server;
1111
initializeDb(): mongoose.MongooseThenable {
1212
(<any>mongoose).Promise = global.Promise
13-
return mongoose.connect(environment.db.url, {
14-
useMongoClient: true
15-
})
13+
return mongoose.connect(environment.db.url)
1614
}
1715
initRoutes(routers: Router[]): Promise<any>{
1816
return new Promise((resolve, reject) =>{

common/model-router.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Router } from './router';
2+
import * as mongoose from 'mongoose';
3+
import { NotFoundError } from 'restify-errors';
4+
5+
export abstract class ModelRouter<D extends mongoose.Document> extends Router {
6+
constructor(
7+
protected model: mongoose.Model<D>
8+
){
9+
super()
10+
}
11+
12+
validateId = (req, res, next) => {
13+
if(!mongoose.Types.ObjectId.isValid(req.params.id)){
14+
next(new NotFoundError('Document not found'))
15+
}else{
16+
next()
17+
}
18+
}
19+
20+
findAll = (req, resp, next) =>{
21+
this.model.find().then(this.renderAll(resp, next))
22+
.catch(next)
23+
};
24+
25+
findById = (req, resp, next) =>{
26+
this.model.findById(req.params.id)
27+
.then(this.render(resp, next))
28+
.catch(next)
29+
}
30+
31+
save = (req, resp, next) => {
32+
let document = new this.model(req.body)
33+
document.save().then(this.render(resp, next))
34+
.catch(next)
35+
}
36+
37+
replace = (req, resp, next) => {
38+
const options = {runValidators: true, overwrite: true}
39+
this.model.update({_id:req.params.id}, req.body, options)
40+
.exec().then((result: any) => {
41+
console.log(result)
42+
if(result.n){
43+
resp.send(204);
44+
}else{
45+
throw new NotFoundError('Documento não encontrado');
46+
}
47+
}).then(this.render(resp, next))
48+
.catch(next)
49+
}
50+
51+
update = (req, resp, next) =>{
52+
const options = {runValidators: true, new: true}
53+
54+
this.model.findByIdAndUpdate(req.params.id, req.body, options)
55+
.then(this.render(resp, next))
56+
.catch(next)
57+
}
58+
59+
delete = (req, resp, next) =>{
60+
this.model.remove({_id:req.params.id})
61+
.exec().then((cmdResult: any)=>{
62+
if(cmdResult.result.n){
63+
resp.send(204)
64+
}else{
65+
throw new NotFoundError('Documento não encontrado');
66+
}
67+
return next()
68+
})
69+
.catch(next)
70+
}
71+
}

common/router.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,18 @@ export abstract class Router extends EventEmitter {
1515
return next()
1616
}
1717
}
18+
19+
renderAll(response: restify.Response, next: restify.Next){
20+
return (documents: any[])=>{
21+
if(documents){
22+
documents.forEach(document=>{
23+
this.emit('beforeRender', document)
24+
})
25+
response.json(documents)
26+
}else{
27+
response.json([])
28+
}
29+
}
30+
}
1831
}
1932

dist/Server/Server.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ const error_handler_1 = require("./error.handler");
99
class Server {
1010
initializeDb() {
1111
mongoose.Promise = global.Promise;
12-
return mongoose.connect(environment_1.environment.db.url, {
13-
useMongoClient: true
14-
});
12+
return mongoose.connect(environment_1.environment.db.url);
1513
}
1614
initRoutes(routers) {
1715
return new Promise((resolve, reject) => {

dist/common/model-router.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.ModelRouter = void 0;
4+
const router_1 = require("./router");
5+
const mongoose = require("mongoose");
6+
const restify_errors_1 = require("restify-errors");
7+
class ModelRouter extends router_1.Router {
8+
constructor(model) {
9+
super();
10+
this.model = model;
11+
this.validateId = (req, res, next) => {
12+
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
13+
next(new restify_errors_1.NotFoundError('Document not found'));
14+
}
15+
else {
16+
next();
17+
}
18+
};
19+
this.findAll = (req, resp, next) => {
20+
this.model.find().then(this.renderAll(resp, next))
21+
.catch(next);
22+
};
23+
this.findById = (req, resp, next) => {
24+
this.model.findById(req.params.id)
25+
.then(this.render(resp, next))
26+
.catch(next);
27+
};
28+
this.save = (req, resp, next) => {
29+
let document = new this.model(req.body);
30+
document.save().then(this.render(resp, next))
31+
.catch(next);
32+
};
33+
this.replace = (req, resp, next) => {
34+
const options = { runValidators: true, overwrite: true };
35+
this.model.update({ _id: req.params.id }, req.body, options)
36+
.exec().then((result) => {
37+
console.log(result);
38+
if (result.n) {
39+
resp.send(204);
40+
}
41+
else {
42+
throw new restify_errors_1.NotFoundError('Documento não encontrado');
43+
}
44+
}).then(this.render(resp, next))
45+
.catch(next);
46+
};
47+
this.update = (req, resp, next) => {
48+
const options = { runValidators: true, new: true };
49+
this.model.findByIdAndUpdate(req.params.id, req.body, options)
50+
.then(this.render(resp, next))
51+
.catch(next);
52+
};
53+
this.delete = (req, resp, next) => {
54+
this.model.remove({ _id: req.params.id })
55+
.exec().then((cmdResult) => {
56+
if (cmdResult.result.n) {
57+
resp.send(204);
58+
}
59+
else {
60+
throw new restify_errors_1.NotFoundError('Documento não encontrado');
61+
}
62+
return next();
63+
})
64+
.catch(next);
65+
};
66+
}
67+
}
68+
exports.ModelRouter = ModelRouter;

dist/common/router.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,18 @@ class Router extends events_1.EventEmitter {
1616
return next();
1717
};
1818
}
19+
renderAll(response, next) {
20+
return (documents) => {
21+
if (documents) {
22+
documents.forEach(document => {
23+
this.emit('beforeRender', document);
24+
});
25+
response.json(documents);
26+
}
27+
else {
28+
response.json([]);
29+
}
30+
};
31+
}
1932
}
2033
exports.Router = Router;

dist/lists/lists.model.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.Lists = void 0;
4+
const mongoose = require("mongoose");
5+
const UserlistSchema = new mongoose.Schema({
6+
name: {
7+
type: String,
8+
required: true
9+
},
10+
vai: {
11+
type: Boolean,
12+
required: true
13+
},
14+
volta: {
15+
type: Boolean,
16+
required: true
17+
},
18+
situacao: {
19+
type: Boolean,
20+
required: true
21+
}
22+
});
23+
const listsSchema = new mongoose.Schema({
24+
name: {
25+
type: String,
26+
required: true
27+
},
28+
dataIda: {
29+
type: Date,
30+
required: true
31+
},
32+
dataVolta: {
33+
type: Date,
34+
required: true
35+
},
36+
users: {
37+
type: [UserlistSchema],
38+
required: false,
39+
select: false,
40+
default: []
41+
},
42+
situacao: {
43+
type: Boolean,
44+
required: true
45+
}
46+
});
47+
exports.Lists = mongoose.model('Lists', listsSchema);

dist/lists/lists.router.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.listsRouter = void 0;
4+
const lists_model_1 = require("./lists.model");
5+
const restify_errors_1 = require("restify-errors");
6+
const model_router_1 = require("../common/model-router");
7+
class ListsRouter extends model_router_1.ModelRouter {
8+
constructor() {
9+
super(lists_model_1.Lists);
10+
this.findUserslist = (req, resp, next) => {
11+
lists_model_1.Lists.findById(req.params.id, "+users")
12+
.then(rest => {
13+
if (!rest) {
14+
throw new restify_errors_1.NotFoundError('List not found');
15+
}
16+
else {
17+
resp.json(rest.users);
18+
return next();
19+
}
20+
}).catch;
21+
};
22+
}
23+
applyRoutes(application) {
24+
//Configurações das rotas transferidas para => "./common/model-router.ts"
25+
application.get('/lists', this.findAll);
26+
application.get('/lists/:id', [this.validateId, this.findById]);
27+
application.post('/lists', this.save);
28+
application.put('/lists/:id', [this.validateId, this.replace]);
29+
application.patch('/lists/:id', [this.validateId, this.update]);
30+
application.del('/lists/:id', [this.validateId, this.update]);
31+
}
32+
}
33+
exports.listsRouter = new ListsRouter();

dist/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
const Server_1 = require("./server/Server");
44
const users_router_1 = require("./users/users.router");
5+
const lists_router_1 = require("./lists/lists.router");
56
const server = new Server_1.Server();
6-
server.bootstrap([users_router_1.usersRouter]).then(server => {
7+
server.bootstrap([users_router_1.usersRouter, lists_router_1.listsRouter]).then(server => {
78
console.log("Server is listening on:", server.application.address());
89
}).catch(error => {
910
console.log('Server failed to start:', error);

dist/reviews/reviews.model.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const mongoose = require("mongoose");
4+
const reviewSchema = mongoose.Schema({
5+
data: {
6+
type: Date,
7+
required: true
8+
},
9+
});

0 commit comments

Comments
 (0)