diff --git a/nodepop/controllers/AnuncioController.js b/nodepop/apiServices/anuncios/controller.js similarity index 93% rename from nodepop/controllers/AnuncioController.js rename to nodepop/apiServices/anuncios/controller.js index c3e61a1..81f0b43 100755 --- a/nodepop/controllers/AnuncioController.js +++ b/nodepop/apiServices/anuncios/controller.js @@ -1,6 +1,6 @@ "use strict"; -let Anuncio = require("../models/Anuncio"); +let Anuncio = require("./model"); // var anuncioController = {}; diff --git a/nodepop/models/Anuncio.js b/nodepop/apiServices/anuncios/model.js similarity index 100% rename from nodepop/models/Anuncio.js rename to nodepop/apiServices/anuncios/model.js diff --git a/nodepop/routes/apiv1/anuncios.js b/nodepop/apiServices/anuncios/routes.js similarity index 83% rename from nodepop/routes/apiv1/anuncios.js rename to nodepop/apiServices/anuncios/routes.js index 3e61c43..9932c2a 100755 --- a/nodepop/routes/apiv1/anuncios.js +++ b/nodepop/apiServices/anuncios/routes.js @@ -1,17 +1,17 @@ -'use strict'; +"use strict"; -const express = require('express'); +const express = require("express"); const router = express.Router(); -const anuncioController = require('../../controllers/AnuncioController'); -const Anuncio = require('../../models/Anuncio'); +const anuncioController = require("../../apiServices/anuncios/controller"); +const Anuncio = require("../../apiServices/anuncios/model"); /** * GET /anuncios * Devuelve una lista de anuncios * http://localhost:3000/apiv1/anuncios */ -router.get('/', async (req, res, next) => { +router.get("/", async (req, res, next) => { try { const skip = parseInt(req.query.start); const limit = parseInt(req.query.limit); @@ -34,7 +34,7 @@ router.get('/', async (req, res, next) => { // const cursor = db.collection('anuncios').find({ tags: { $in: ['work','stylelife']}}); if (tags !== "") { const extTags = tags.split(" "); - filter.tags = { '$in': extTags }; + filter.tags = { "$in": extTags }; } if (venta) { @@ -49,12 +49,12 @@ router.get('/', async (req, res, next) => { res.status(422).json({success: false, error:"El primero parámetro del rango de precio debe ser menor"}); return; } - filter.precio = { '$gte': extPrecio[0], '$lte': extPrecio[1] }; + filter.precio = { "$gte": extPrecio[0], "$lte": extPrecio[1] }; } else if (patPreMayor.test(precio)) { - filter.precio = { '$gte': extPrecio[0] }; + filter.precio = { "$gte": extPrecio[0] }; console.log("arma precio mayor -->", filter.precio); } else if (patPreMenor.test(precio)) { - filter.precio = { '$lte': parseInt(extPrecio[1]) }; + filter.precio = { "$lte": parseInt(extPrecio[1]) }; console.log("arma precio menor -->", filter.precio); } else if (patPreIgual.test(precio)) { filter.precio = parseInt(extPrecio[0]); @@ -63,7 +63,7 @@ router.get('/', async (req, res, next) => { } if (nombre) { - filter.nombre = {$regex: new RegExp('^' + nombre + '.*', 'i')}; + filter.nombre = {$regex: new RegExp("^" + nombre + ".*", "i")}; } const anuncios = await anuncioController.listaAnuncios({filter:filter, skip, limit, fields, sort}); @@ -78,7 +78,7 @@ router.get('/', async (req, res, next) => { * Obtiene el listado de tags existentes * http://localhost:3000/apiv1/anuncios/tags */ -router.get('/tags', async (req, res, next) => { +router.get("/tags", async (req, res, next) => { try { const tagList = await anuncioController.listaTags(); res.json({ success: true, results: tagList }); @@ -92,7 +92,7 @@ router.get('/tags', async (req, res, next) => { * Obtiene un agente mediante el :id * http://localhost:3000/apiv1/anuncios/:id */ -router.get('/:id', async(req, res, next) => { +router.get("/:id", async(req, res, next) => { try { const _id = req.params.id; const anuncio = await anuncioController.consultarUnAnuncio(_id); @@ -110,7 +110,7 @@ router.get('/:id', async(req, res, next) => { * Crea un anuncio * http://localhost:3000/apiv1/anuncios */ -router.post('/', async (req, res, next) => { +router.post("/", async (req, res, next) => { try { const data = req.body; const anuncio = new Anuncio(data); @@ -126,7 +126,7 @@ router.post('/', async (req, res, next) => { * Actualiza un anuncio * http://localhost:3000/apiv1/anuncios/:id */ -router.put('/:id', async(req, res, next) => { +router.put("/:id", async(req, res, next) => { try { const _id = req.params.id; const data = req.body; diff --git a/nodepop/app.js b/nodepop/app.js index 05f999a..2096777 100755 --- a/nodepop/app.js +++ b/nodepop/app.js @@ -4,6 +4,7 @@ var path = require("path"); var cookieParser = require("cookie-parser"); var logger = require("morgan"); const { error404Handler,generalErrorHandler } = require("./middleware"); +const routes = require("./routes"); var app = express(); @@ -23,13 +24,14 @@ app.use(express.static(path.join(__dirname, "public"))); * Conexión a la base de datos */ require("./services/mongoDB"); -require("./models/Anuncio"); -require("./controllers/AnuncioController"); +// require("./apiServices/anuncios/model"); +// require("./apiServices/anuncios/controller"); /** * Rutas de mi API */ -app.use("/apiv1/anuncios", require("./routes/apiv1/anuncios")); +// app.use("/apiv1/anuncios", require("./routes/apiv1/routes")); +app.use("/apiv1/", routes); // Variables globales para vistas app.locals.title = "NodePop"; @@ -37,8 +39,7 @@ app.locals.title = "NodePop"; /** * Rutas de mi aplicación web */ -app.use("/", require("./routes/index")); -app.use("/users", require("./routes/users")); +app.use("/",require("./routes/index")); app.use(error404Handler); // catch 404 and forward to error handler app.use(generalErrorHandler); // error handler diff --git a/nodepop/routes/index.js b/nodepop/routes/index.js index 31837c2..fe99fc7 100755 --- a/nodepop/routes/index.js +++ b/nodepop/routes/index.js @@ -1,11 +1,12 @@ -'use strict'; +"use strict"; -const express = require('express'); +const express = require("express"); const router = express.Router(); -const anuncioController = require('../controllers/AnuncioController'); +const anuncioController = require("../apiServices/anuncios/controller"); +const anuncios = require("../apiServices/anuncios/routes"); /* GET home page. */ -router.get('/', async function(req, res, next) { +router.get("/", async function(req, res, next) { try { const skip = parseInt(req.query.start); const limit = parseInt(req.query.limit); @@ -21,11 +22,15 @@ router.get('/', async function(req, res, next) { } res.locals.anuncios = await anuncioController.listaAnuncios({filter:filter, skip, limit, sort}); - res.render('index'); + res.render("index"); } catch (err) { next(err); } }); +router.use("/anuncios", anuncios); + +// router.use("/users", users); + module.exports = router; \ No newline at end of file diff --git a/nodepop/views/index.html b/nodepop/views/index.html index c2a0154..bb5d45a 100755 --- a/nodepop/views/index.html +++ b/nodepop/views/index.html @@ -1,94 +1,120 @@ - - <%= title %> - - - - - -
-
- - NodePop - -
- - Documentación + + + + <%= title %> + + + + + + + +
+
+ + + NodePop + + +
+ + + + Documentación + + +
+ +
+
+

+ + + <%= title %> + + +

+

+ + + Tienes productos que quieres vender?, o buscas productos usados y de buena calidad? + + + Anuncia tus productos aquí. + + +

+ + + Crea un Anuncio aquí! + +
+
+
+
+
+

Lista de Anuncios

+
+ <% anuncios.forEach(anuncio=> {%> -
-
-

- - - <%= title %> - - -

-

- - - Tienes productos que quieres vender?, o buscas productos usados y de buena calidad? - - - Anuncia tus productos aquí. - - -

- - - Crea un Anuncio aquí! - - -
-
-
-
-
-

Lista de Anuncios

-
- <% anuncios.forEach(anuncio => {%> - -
-
- <% - let foto = "/images/anuncios/" + anuncio.foto; - let trim_url = foto.trim(); - %> - - <% - if (foto === "/images/anuncios/"){%> - PlaceholderThumbnail - <%}%> -
-

- <%= anuncio.nombre %> - <%if(anuncio.venta){%> - Venta - <%}%> - <%if(!anuncio.venta){%> - Compra - <%}%> -

-
$<%= anuncio.precio %>
-

Lorem ipsum dolor sit amet consectetur adipisicing elit. -

-
- -
+
+
+ <% let foto="/images/anuncios/" + anuncio.foto; let trim_url=foto.trim(); %> + + <% if (foto==="/images/anuncios/" ){%> + + Placeholder + Thumbnail + + <%}%> +
+

+ + <%= anuncio.nombre %> + + <%if(anuncio.venta){%> + Venta + <%}%> + <%if(!anuncio.venta){%> + Compra + <%}%> +

+
$<%= anuncio.precio %> +
+

Lorem ipsum dolor sit amet consectetur adipisicing elit. +

+
+
+
- <%})%> + <%})%> -
- - +
+ + + \ No newline at end of file