Skip to content

Commit

Permalink
refactor: separate in component Anuncio into apiServices
Browse files Browse the repository at this point in the history
  • Loading branch information
rpalaciosg committed Aug 23, 2021
1 parent 197c719 commit 8d0ecdc
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

let Anuncio = require("../models/Anuncio");
let Anuncio = require("./model");

// var anuncioController = {};

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand All @@ -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]);
Expand All @@ -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});
Expand All @@ -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 });
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions nodepop/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -23,22 +24,22 @@ 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";

/**
* 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

Expand Down
15 changes: 10 additions & 5 deletions nodepop/routes/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
198 changes: 112 additions & 86 deletions nodepop/views/index.html
Original file line number Diff line number Diff line change
@@ -1,94 +1,120 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<!-- <link rel='stylesheet' href='/stylesheets/style.css' /> -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm bg-dark">
<h5 class="my-0 mr-md-auto font-weight-normal">
<a class="py-2" style="text-decoration: none;" href="#">
<font style="vertical-align: inherit;"><font style="vertical-align: inherit;"><strong>NodePop</strong></font></font>
</a>
</h5>
<nav class="my-2 my-md-0 mr-md-3">
<a class="p-2 text-dark" href="https://github.com/rpalaciosg/kc_backend_nodejs_web7" target="blank"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Código fuente </font></font></a>
</nav>
<a class="btn btn-outline-secondary" href="https://documenter.getpostman.com/view/3162339/SVn3sFCr" target="blank"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Documentación</font></font></a>

<head>
<title>
<%= title %>
</title>
<!-- <link rel='stylesheet' href='/stylesheets/style.css' /> -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
</head>

<body>
<div
class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom shadow-sm bg-dark">
<h5 class="my-0 mr-md-auto font-weight-normal">
<a class="py-2" style="text-decoration: none;" href="#">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;"><strong>NodePop</strong></font>
</font>
</a>
</h5>
<nav class="my-2 my-md-0 mr-md-3">
<a class="p-2 text-dark" href="https://github.com/rpalaciosg/kc_backend_nodejs_web7" target="blank">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">Código fuente </font>
</font>
</a>
</nav>
<a class="btn btn-outline-secondary" href="https://documenter.getpostman.com/view/3162339/SVn3sFCr" target="blank">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">Documentación</font>
</font>
</a>
</div>

<div class=" jumbotron position-relative overflow-hidden p-3 p-md-5 m-md-3 text-center bg-light">
<div class="col-md-5 p-lg-5 mx-auto my-5">
<h1 class="display-4 font-weight-normal">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">
<%= title %>
</font>
</font>
</h1>
<p class="lead font-weight-normal">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">
Tienes productos que quieres vender?, o buscas productos usados y de buena calidad?
</font>
<font style="vertical-align: inherit;">
Anuncia tus productos aquí.
</font>
</font>
</p>
<a class="btn btn-outline-info" href="#">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">Crea un Anuncio aquí!</font>
</font>
</a>
</div>
<div class="product-device shadow-sm d-none d-md-block"></div>
<div class="product-device product-device-2 shadow-sm d-none d-md-block"></div>
</div>
<div class="container">
<h2>Lista de Anuncios</h2>
<div class="d-flex flex-row flex-wrap">
<% anuncios.forEach(anuncio=> {%>

<div class=" jumbotron position-relative overflow-hidden p-3 p-md-5 m-md-3 text-center bg-light">
<div class="col-md-5 p-lg-5 mx-auto my-5">
<h1 class="display-4 font-weight-normal">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">
<%= title %>
</font>
</font>
</h1>
<p class="lead font-weight-normal">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">
Tienes productos que quieres vender?, o buscas productos usados y de buena calidad?
</font>
<font style="vertical-align: inherit;">
Anuncia tus productos aquí.
</font>
</font>
</p>
<a class="btn btn-outline-info" href="#">
<font style="vertical-align: inherit;">
<font style="vertical-align: inherit;">Crea un Anuncio aquí!</font>
</font>
</a>
</div>
<div class="product-device shadow-sm d-none d-md-block"></div>
<div class="product-device product-device-2 shadow-sm d-none d-md-block"></div>
</div>
<div class="container">
<h2>Lista de Anuncios</h2>
<div class="d-flex flex-row flex-wrap">
<% anuncios.forEach(anuncio => {%>

<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100 shadow-sm rounded">
<%
let foto = "/images/anuncios/" + anuncio.foto;
let trim_url = foto.trim();
%>
<a href="#"><img class="card-img-top" src="<%=trim_url%>" alt=""></a>
<%
if (foto === "/images/anuncios/"){%>
<svg class="bd-placeholder-img card-img-top" width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img" aria-label="Marcador de posición: miniatura"><title>Placeholder</title><rect width="100%" height="100%" fill="#55595c"></rect><text x="40%" y="50%" fill="#eceeef" dy=".3em">Thumbnail</text></svg>
<%}%>
<div class="card-body">
<h4 class="card-title">
<a href="#"> <%= anuncio.nombre %> </a>
<%if(anuncio.venta){%>
<span class="badge badge-success" style="font-size: 12px;">Venta</span>
<%}%>
<%if(!anuncio.venta){%>
<span class="badge badge-warning" style="font-size: 12px;">Compra</span>
<%}%>
</h4>
<h5>$<%= anuncio.precio %></h5>
<p class="card-text">Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</div>
<div class="card-footer">
<small class="text-muted"></small>
<% anuncio.tags.forEach(tag => {%>
<a href="#" class="badge outline badge-pill badge-secondary"><%=tag%></a>
<%})%>
</div>
</div>
<div class="col-lg-4 col-md-6 mb-4">
<div class="card h-100 shadow-sm rounded">
<% let foto="/images/anuncios/" + anuncio.foto; let trim_url=foto.trim(); %>
<a href="#"><img class="card-img-top" src="<%=trim_url%>" alt=""></a>
<% if (foto==="/images/anuncios/" ){%>
<svg class="bd-placeholder-img card-img-top" width="100%" height="100%"
xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid slice" focusable="false" role="img"
aria-label="Marcador de posición: miniatura">
<title>Placeholder</title>
<rect width="100%" height="100%" fill="#55595c"></rect><text x="40%" y="50%" fill="#eceeef"
dy=".3em">Thumbnail</text>
</svg>
<%}%>
<div class="card-body">
<h4 class="card-title">
<a href="#">
<%= anuncio.nombre %>
</a>
<%if(anuncio.venta){%>
<span class="badge badge-success" style="font-size: 12px;">Venta</span>
<%}%>
<%if(!anuncio.venta){%>
<span class="badge badge-warning" style="font-size: 12px;">Compra</span>
<%}%>
</h4>
<h5>$<%= anuncio.precio %>
</h5>
<p class="card-text">Lorem ipsum dolor sit amet consectetur adipisicing elit.
</p>
</div>
<div class="card-footer">
<small class="text-muted"></small>
<% anuncio.tags.forEach(tag=> {%>
<a href="#" class="badge outline badge-pill badge-secondary">
<%=tag%>
</a>
<%})%>
</div>
</div>
</div>

<%})%>
<%})%>

</div>
</div>
</body>
</html>
</div>
</body>

</html>

0 comments on commit 8d0ecdc

Please sign in to comment.