-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproductosWS.js
135 lines (120 loc) · 4.16 KB
/
productosWS.js
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const conexion=require('./conexion.js');
const sql=require('mssql');
//Consulta de todos los servicios
async function getProductos(){
try{
let pool=await sql.connect(conexion);
let salida=await pool.request().query('select * from Productos');
return salida.recordsets;
}catch(err){
console.log(err);
}
}
//Consulta un servicio especifico
async function getProducto(IDProducto){
try{
let pool=await sql.connect(conexion);
let salida=await pool.request()
.input('IDProducto',sql.Int,IDProducto)
.query('select * from Productos where IDProducto= @IDProducto');
return salida.recordsets;
}catch(err){
console.log(err);
}
}
//Insert de los productos
async function newProducto(producto){
try{
let pool=await sql.connect(conexion);
let newProducto=await pool.request()
// .input('IDServicio',sql.Int,servicio.IDServicio)
.input('NombreProducto',sql.VarChar,producto.NombreProducto)
.input('MarcaProducto',sql.VarChar,producto.MarcaProducto)
.input('DescripcionProducto',sql.VarChar,producto.DescripcionProducto)
.input('PrecioProducto',sql.Money,producto.PrecioProducto)
.input('ExistenciaProducto',sql.Int,producto.ExistenciaProducto)
.execute('pr_newProducto');
return newProducto.recordsets;
} catch (err) {
throw new Error ('Se presentó un error en el procedimiento almacenado agregar p');
}
}
//Update de los servicios
async function upProducto(producto){
try{
let pool=await sql.connect(conexion);
let upProducto=await pool.request()
.input('IDProducto',sql.Int,producto.IDProducto)
.input('NombreProducto',sql.VarChar,producto.NombreProducto)
.input('MarcaProducto',sql.VarChar,producto.MarcaProducto)
.input('DescripcionProducto',sql.VarChar,producto.DescripcionProducto)
.input('PrecioProducto',sql.Money,producto.PrecioProducto)
.input('ExistenciaProducto',sql.Int,producto.ExistenciaProducto)
.execute('pr_upProducto');
return upProducto.recordsets;
} catch (err) {
throw new Error ('Se presentó un error en el procedimiento almacenado actualizar s');
}
}
//Delete de los servicios
async function delProducto(IDProducto){
try{
let pool=await sql.connect(conexion);
let delProducto=await pool.request()
.input('IDProducto',sql.Int,IDProducto)
.execute('pr_delProducto');
return delProducto.recordsets;
} catch (err) {
throw new Error ('Se presentó un error en el procedimiento almacenado eliminar producto');
}
}
async function getIDProductos(){
try{
let pool=await sql.connect(conexion);
let salida=await pool.request().query('select IDProducto from Productos');
return salida.recordsets;
}catch(err){
console.log(err);
}
}
//NUEVOS METODOS EN APP
async function getProductosAsc(){
try{
let pool=await sql.connect(conexion);
let salida=await pool.request().query('select * from Productos ORDER BY PrecioProducto');
return salida.recordsets;
}catch(err){
console.log(err);
}
}
//NUEVO METODO EN APP
async function getProductosDesc(){
try{
let pool=await sql.connect(conexion);
let salida=await pool.request().query('select * from Productos ORDER BY PrecioProducto DESC');
return salida.recordsets;
}catch(err){
console.log(err);
}
}
//NUEVO METODO EN APP
async function getProductosAlfa(){
try{
let pool=await sql.connect(conexion);
let salida=await pool.request().query('select * from Productos ORDER BY NombreProducto');
return salida.recordsets;
}catch(err){
console.log(err);
}
}
module.exports={
getProductos:getProductos,
getProducto:getProducto,
newProducto:newProducto,
upProducto:upProducto,
delProducto:delProducto,
getIDProductos:getIDProductos,
getProductosDesc:getProductosDesc,
getProductosAsc:getProductosAsc,
getProductosAlfa:getProductosAlfa
}