Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18,238 changes: 1,284 additions & 16,954 deletions package-lock.json

Large diffs are not rendered by default.

35 changes: 12 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
{
"name": "escuelajs-reto-09",
"name": "CRUD_Mongo",
"version": "1.0.0",
"description": "Reto 9 Octubre 26: Curso de Backend con Node",
"description": "",
"main": "index.js",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongodb": "^3.6.6"
},
"devDependencies": {
"jest": "^26.6.3",
"nodemon": "^1.19.4",
"supertest": "^6.1.3"
},
"scripts": {
"dev": "DEBUG=app:* nodemon src/index.js",
"start": "NODE_ENV=production node src/index.js",
"test:e2e": "jest --forceExit --config ./e2e/jest-e2e.json"
},
"repository": {
"type": "git",
"url": "git+https://github.com/platzi/escuelajs-reto-09.git"
"start": "node source/app.js",
"dev": "nodemon ./source"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/platzi/escuelajs-reto-09/issues"
"dependencies": {
"ejs": "^3.1.6",
"express": "^4.17.1",
"mongoose": "^5.12.9",
"morgan": "^1.10.0"
},
"homepage": "https://github.com/platzi/escuelajs-reto-09#readme"
"devDependencies": {
"nodemon": "^2.0.7"
}
}
32 changes: 32 additions & 0 deletions source/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const path = require("path");
const express = require("express");
const app = express();
const morgan = require("morgan");
const mongoose = require("mongoose");


//Connecting to db
mongoose.connect("mongodb+srv://hacg18:harold18@crudnodemongo.ruwy5.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
.then(db => console.log("DB connected"))
.catch(err => console.log("Error"));


//Settings
app.set("port", process.env.PORT || 3000);
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");

//Imports routes
const indexRoutes = require("./routes/index");

//middlewares
app.use(morgan("dev"));
app.use(express.urlencoded({extended: false}));

//routes
app.use("/", indexRoutes);

//Starts the server
app.listen(app.get("port"), ()=>{
console.log(`Server on port ${app.get("port")}`);
})
11 changes: 11 additions & 0 deletions source/models/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require("mongoose");
const schema = mongoose.Schema;


const categorySchema = new schema({
name: String,
categoryID: Number
});


module.exports = mongoose.model("categories", categorySchema);
12 changes: 12 additions & 0 deletions source/models/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require("mongoose");
const schema = mongoose.Schema;


const productSchema = new schema({
categoryID: Number,
name: String,
price: Number,
description: String,
});

module.exports = mongoose.model("products", productSchema);
64 changes: 64 additions & 0 deletions source/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const express = require("express");
const router = express.Router();

const product = require("../models/product");
const category = require("../models/category");
const { findById } = require("../models/product");

router.get("/", async (req, res) => {
const products = await product.find();
const categories = await category.find();
res.render("index", {
products: products,
categories: categories
})
});

router.post("/add", async (req, res) => {
const Product = new product(req.body);
await Product.save();
res.redirect("/");
});

router.post("/addd", async (req, res) => {
const Category = new category(req.body);
await Category.save();
res.redirect("/");
});

router.get("/delete/:id", async (req, res) =>{
const {id} = req.params;
await product.remove({_id: id});
await category.remove({_id: id});
res.redirect("/");
});

router.get("/edit/:id", async (req, res) =>{
const {id} = req.params;
const productt = await product.findById(id);
res.render("edit", {
productt
});
});

router.get("/editt/:id", async (req, res) =>{
const {id} = req.params;
const categori = await category.findById(id);
res.render("editt", {
categori
});
});

router.post("/edit/:id", async (req, res) =>{
const {id} = req.params;
await product.updateMany({_id: id}, req.body);
res.redirect("/");
});

router.post("/editt/:id", async (req, res) =>{
const {id} = req.params;
await category.updateMany({_id: id}, req.body);
res.redirect("/");
});

module.exports = router;
31 changes: 31 additions & 0 deletions source/views/edit.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%- include ("partials/_header") %>

<div class="container">
<div class="row">
<div class="col-md-5 offset-md-3">
<div class="card">
<div class="card-body">
<form action="/edit/<%= productt._id%>" method="POST">
<div class="form-group">
<input type="number" name="categoryID" placeholder="insert a category ID" class="form-control" value="<%= productt.categoryID %>">
</div>
<div class="form-group">
<input type="text" name="name" placeholder="insert a product name" class="form-control" value="<%= productt.name %>">
</div>
<div class="form-group">
<input type="number" name="price" placeholder="insert a product price" class="form-control" value="<%= productt.price%>">
</div>
<div class="form-group">
<textarea name="description" cols="80" placeholder="insert a product description" class="form-control"><%= productt.description%></textarea>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>

<%- include ("partials/_footer") %>
23 changes: 23 additions & 0 deletions source/views/editt.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<%- include ("partials/_header") %>

<div class="container">
<div class="row">
<div class="col-md-5 offset-md-3">
<div class="card">
<div class="card-body">
<form action="/editt/<%= categori._id%>" method="POST">
<div class="form-group">
<input type="text" name="name" placeholder="insert a category name" class="form-control" value="<%= categori.name %>">
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>


<%- include ("partials/_footer") %>
120 changes: 120 additions & 0 deletions source/views/index.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<%- include ("partials/_header") %>

<div class="container">
<div class="row">
<!--Form-->
<div class="col-md-5">
<div class="card">
<div class="card-body">
<form action="/add" method="POST">
<div class="form-group">
<input type="number" name="categoryID" placeholder="insert a category ID" class="form-control">
</div>
<div class="form-group">
<input type="text" name="name" placeholder="insert a product name" class="form-control">
</div>
<div class="form-group">
<input type="number" name="price" placeholder="insert a product price" class="form-control">
</div>
<div class="form-group">
<textarea name="description" cols="80" placeholder="insert a product description" class="form-control"></textarea>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
<!--List-->
<div class="col-md-7">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ProductID</th>
<th>CategoryID</th>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<% for(var i=0; i < products.length; i++) { %>
<tr>
<td>
<%= i +1 %>
</td>
<td>
<%= products[i].categoryID %>
</td>
<td>
<%= products[i].name %>
</td>
<td>
<%= products[i].price %>
</td>
<td>
<%= products[i].description %>
</td>
<td>
<a href="/edit/<%= products[i]._id %>" class="btn btn-dark">Edit</a>
<a href="/delete/<%= products[i]._id %>" class="btn btn-danger">Delete</a>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
</div>

<div class="container">
<div class="row">
<!--Form-->
<div class="col-md-5">
<div class="card">
<div class="card-body">
<form action="/addd" method="POST">
<div class="form-group">
<input type="text" name="name" placeholder="insert a category name" class="form-control">
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
</div>
</div>
</div>
<!--List-->
<div class="col-md-7">
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>CategoryID</th>
<th>Name</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<% for(var i=0; i < categories.length; i++) { %>
<tr>
<td>
<%= i + 1 %>
</td>
<td>
<%= categories[i].name %>
</td>
<td>
<a href="/editt/<%= categories[i]._id %>" class="btn btn-dark">Edit</a>
<a href="/delete/<%= categories[i]._id %>" class="btn btn-danger">Delete</a>
</td>
</tr>
<% } %>
</tbody>
</table>
</div>
</div>
</div>

<%- include ("partials/_footer") %>
2 changes: 2 additions & 0 deletions source/views/partials/_footer.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
</body>
</html>
14 changes: 14 additions & 0 deletions source/views/partials/_header.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CRUD NodeJS MongoDB</title>
<!-- Bootstrap 5 cdn-->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-dark bg-dark mb-4">
<a href="/" class="navbar-brand">CRUD NodeJS-MongoDB</a>
</nav>
13 changes: 0 additions & 13 deletions src/app.js

This file was deleted.

Loading