Skip to content

Commit 712c270

Browse files
committed
agregar productos al carrito
1 parent 398708d commit 712c270

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

app.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ var admin_route = require('./routes/admin');
1010
var producto_route = require('./routes/producto');
1111
var cupon_route = require('./routes/cupon');
1212
var categorias_route = require('./routes/categorias');
13+
var carrito_route = require('./routes/carrito');
14+
1315

1416
const bodyParser = require('body-parser');
1517

@@ -40,6 +42,7 @@ app.use('/api',admin_route);
4042
app.use('/api',producto_route);
4143
app.use('/api',cupon_route);
4244
app.use('/api',categorias_route);
45+
app.use('/api',carrito_route);
4346

4447

4548
module.exports = app;

controllers/CarritoController.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var Carrito = require('../models/carrito');
2+
3+
const agregar_carrito_cliente = async function (req, res) {
4+
if(req.user){
5+
let data = req.body;
6+
7+
let carrito_cliente = await Carrito.findOne({cliente: data.cliente, producto: data.producto});
8+
if(carrito_cliente){
9+
10+
}else if(!carrito_cliente){
11+
12+
}
13+
if(!carrito_cliente){
14+
let reg = await Carrito.create(data);
15+
res.status(200).send({data: reg});
16+
}else if(carrito_cliente){
17+
let nuevaCantidad = parseInt(data.cantidad) + parseInt(carrito_cliente.cantidad);
18+
let reg = await Carrito.findByIdAndUpdate({_id: carrito_cliente._id}, {cantidad: nuevaCantidad});
19+
res.status(200).send({data: reg});
20+
}
21+
22+
}else{
23+
res.status(500).send({message: 'UnauthorizedAccess'});
24+
}
25+
}
26+
27+
module.exports = {
28+
agregar_carrito_cliente
29+
}

models/carrito.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
var mongoose = require('mongoose');
4+
var schema = mongoose.Schema;
5+
6+
var CarritoSchema = schema({
7+
producto: {type: schema.ObjectId, ref: 'producto', required: true},
8+
cliente: {type: schema.ObjectId, ref: 'cliente', required: true},
9+
cantidad: {type: Number, require: true},
10+
createdAt: {type: Date, default: Date.now, require: true}
11+
});
12+
13+
module.exports = mongoose.model('carrito', CarritoSchema);

routes/carrito.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
var express = require('express');
4+
var carritoController = require('../controllers/CarritoController');
5+
6+
var api = express.Router();
7+
var auth = require('../middleware/authenticate');
8+
9+
api.post('/agregar_carrito_cliente', auth.auth, carritoController.agregar_carrito_cliente);
10+
11+
module.exports = api;

0 commit comments

Comments
 (0)