-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7f921a1
Showing
21 changed files
with
11,388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/node_modules | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Proyecto Final - Introducción a **Node.js** - Back-end :computer: | ||
|
||
## Institución: [Ada ITW](https://adaitw.org/) | ||
### Dicatado por: Ian Javier Rivas | ||
![logo_ada](ada-logo-fucsia.png) | ||
|
||
API creada para gestionar productos y suscriptores de un Ecommerce. Permite hacer CRUD de productos y suscriptores. | ||
Los productos se puede obtener de manera conjunta, por id y por su nombre, a su vez, se pueden agregar, actualizar y eliminar. | ||
|
||
## Tenologias utilizadas :hammer::wrench: | ||
|
||
* Node.js | ||
* Express | ||
* JavaScript | ||
* Base de Datos MongoDB con Mongoose | ||
* Git (Software de control de versiones) | ||
* Otras librerias: Dotenv, Cors, Morgan | ||
|
||
## Despliegue :rocket: | ||
|
||
* Server de Railway | ||
* Base de datos con MongoDB Atlas | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
export default { | ||
PORT: process.env.PORT || 3000, | ||
DB: process.env.MONGO_URI || 'mongodb://localhost/Ecommerce', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import Producto from '../models/producto.js'; | ||
|
||
export async function crearProducto(req, res) { | ||
try { | ||
const producto = new Producto(req.body); | ||
await producto.save(); | ||
res.status(201).json({ mensaje: 'Producto creado' }); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(500).json({ mensaje: 'Error al crear el producto' }); | ||
} | ||
} | ||
|
||
export async function obtenerProductos(req, res) { | ||
try { | ||
const productos = await Producto.find(); | ||
res.status(200).json(productos); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(500).json({ mensaje: 'Error al obtener el producto' }); | ||
} | ||
} | ||
|
||
export async function obtenerProductoPorNombre(req, res) { | ||
try { | ||
const productos = await Producto.find({ nombre: req.query.n}); | ||
res.status(200).json(productos); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(500).json({ mensaje: 'Error al obtener el producto' }); | ||
} | ||
} | ||
|
||
export async function obtenerProductoPorId(req, res) { | ||
try { | ||
const producto = await Producto.findById(req.params.id); | ||
if (producto === null) { | ||
res.status(404).json({ mensaje: 'Producto no encontrado' }); | ||
} | ||
res.status(200).json(producto); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(404).json({ mensaje: 'Producto no encontrado' }); | ||
} | ||
} | ||
|
||
export async function eliminarProducto(req, res) { | ||
try { | ||
const producto = await Producto.findByIdAndDelete(req.params.id); | ||
if (producto === null) { | ||
res.status(404).json({ mensaje: 'Producto no encontrado' }); | ||
} | ||
res.status(200).json({ mensaje: 'Producto eliminado' }); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(404).json({ mensaje: 'Producto no encontrado' }); | ||
} | ||
} | ||
|
||
export async function actualizarProducto(req, res) { | ||
try { | ||
const producto = await Producto.findByIdAndUpdate(req.params.id, req.body, { | ||
new: true, | ||
}); | ||
if (producto === null) { | ||
throw new Error('Producto no encontrado'); | ||
} | ||
res.status(200).json({ mensaje: 'Producto actualizado', producto }); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(404).json({ mensaje: 'Producto no encontrado' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import Suscriptor from '../models/suscriptor.js'; | ||
|
||
export async function crearSuscriptor(req, res) { | ||
try { | ||
const suscriptor = new Suscriptor(req.body); | ||
await suscriptor.save(); | ||
res.status(201).json({ mensaje: 'Suscriptor creado' }); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(500).json({ mensaje: 'Error al crear el suscriptor' }); | ||
} | ||
} | ||
|
||
export async function obtenerSuscriptores(req, res) { | ||
try { | ||
const suscriptores = await Suscriptor.find(); | ||
res.status(200).json(suscriptores); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(500).json({ mensaje: 'Error al obtener el suscriptor' }); | ||
} | ||
} | ||
|
||
export async function eliminarSuscriptor(req, res) { | ||
try { | ||
const suscriptor = await Suscriptor.findByIdAndDelete(req.params.id); | ||
if (suscriptor === null) { | ||
res.status(404).json({ mensaje: 'Suscriptor no encontrado' }); | ||
} | ||
res.status(200).json({ mensaje: 'Suscriptor eliminado' }); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(404).json({ mensaje: 'Suscriptor no encontrado' }); | ||
} | ||
} | ||
|
||
export async function actualizarSuscriptor(req, res) { | ||
try { | ||
const suscriptor = await Suscriptor.findByIdAndUpdate( | ||
req.params.id, | ||
req.body, | ||
{ new: true } | ||
); | ||
if (suscriptor === null) { | ||
res.status(404).json({ mensaje: 'Suscriptor no encontrado' }); | ||
} | ||
res.status(200).json(suscriptor); | ||
} catch (error) { | ||
console.log(error.mensaje); | ||
res.status(404).json({ mensaje: 'Suscriptor no encontrado' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import express from 'express'; | ||
const app = express(); | ||
import morgan from 'morgan'; | ||
import cors from 'cors'; | ||
import config from './config/index.js'; | ||
import productosRoutes from './routes/productos.routes.js'; | ||
import suscriptoresRoutes from './routes/suscriptores.routes.js'; | ||
import './mongodb.js'; | ||
import path from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
const __filename = fileURLToPath(import.meta.url); | ||
const __dirname = path.dirname(__filename); | ||
|
||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
// Middleware | ||
app.use(cors()); | ||
app.use(morgan('dev')); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
|
||
// Routes | ||
app.get('/', (req, res) => { | ||
res.send('Bienvenido a mi API'); | ||
}); | ||
|
||
app.get('/status', (req, res) => { | ||
res.send('OK'); | ||
}); | ||
|
||
app.use('/productos', productosRoutes); | ||
app.use('/suscriptores', suscriptoresRoutes); | ||
|
||
app.all('/*', (req, res) => { | ||
res.status(404).send('Página no encontrada'); | ||
}); | ||
|
||
// Server | ||
app.listen(config.PORT, () => { | ||
console.log(`Servidor en el puerto: ${config.PORT}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Schema, model } from 'mongoose'; | ||
|
||
const productoSchema = new Schema({ | ||
titulo: { | ||
type: String, | ||
required: true, | ||
}, | ||
precio: { type: Number, required: true }, | ||
descripcion: { type: String, required: true }, | ||
categoria: { type: String, required: true }, | ||
imagen: { type: String, required: true }, | ||
}); | ||
|
||
export default model('Producto', productoSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Schema, model } from 'mongoose'; | ||
|
||
const suscriptorSchema = new Schema({ | ||
email: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
|
||
export default model('Suscriptor', suscriptorSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import mongoose from 'mongoose'; | ||
import config from './config/index.js'; | ||
|
||
mongoose | ||
.connect(config.DB) | ||
.then(() => console.log('Base de datos conectada')) | ||
.catch((err) => console.log(err)); |
Oops, something went wrong.