1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const daoFamilia = require("../DAO/FamiliasDAO");
class FamiliaController {
/* ROTAS DE FETCH:
* Todas as rotas de fetch lidam com informações pertinentes a vários usuários
*/
//Fetch especie
static fetchFamilias(query, callback) {
console.log(callback);
const orderQuery = FamiliaController.constructOrderQuery(query);
const whereQuery = FamiliaController.constructWhereQuery(query);
return daoFamilia.fetchFamilias(orderQuery, whereQuery, callback);
}
static getFamiliaByID(req, res, next) {
daoFamilia.findByID(req.params.id, (error, familia) => {
if (error) {
res.json(error);
res.status(400);
} else {
res.json(familia);
res.status(200);
}
});
}
//jjjj
static addFamilia(req, res, next) {
daoFamilia.addFamilia(req.body, (error, familia) => {
if (error) {
res.json(error);
res.status(400);
} else {
res.json(familia);
res.status(200);
}
});
}
static constructOrderQuery(query) {
/**
* Construct Order Query:
*
* isAscending: should the ordering be ascending or descending?
* field: order by specified field. possible values:
* name: nome_cientifico
*/
let orderQuery = {};
//Decide isAscending's value. Default is ASC, if false then DESC
orderQuery.isAscending = query.isAscending === "false" ? "DESC" : "ASC";
switch (query.sort) {
case "nome":
orderQuery.field = "nome";
break;
default:
//Default Order Query
orderQuery.field = "id_familia";
}
return orderQuery;
}
static constructWhereQuery(query) {
/**
* Construct Where Query:
*
* Possible query parameters:
* contains: searches for the string in any one of the table's fields
*/
let whereQuery = {};
if (query.contains !== undefined) {
whereQuery.contains = query.contains;
}
console.log(whereQuery);
return whereQuery;
}
}
module.exports = FamiliaController;