Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require("express"); //import pakietu express
- const router = express.Router();
- const mongoose = require("mongoose");
- const Product = require("../models/product");
- router.get("/", (req,res,next)=>{
- Product.find().exec()
- .then(docs => {
- res.status(200).json(docs);
- })
- .catch(err => req.status(500).json({error: err}));
- });
- router.post("/", (req,res,next)=>{
- const product = new Product({
- _id: new mongoose.Types.ObjectId(),
- name: req.body.name,
- price: req.body.price
- });
- product.save()
- .then(result => {res.status(200).json({
- message: "Dodanie nowego produktu",
- createdProduct: product
- });
- })
- .catch(err => req.status(500).json({error: err}));
- });
- router.get("/:productId", (req,res,next)=>{
- const id = req.params.productId;
- Product.findById(id).exec().then(doc => {
- res.status(200).json(doc)
- }).catch(err => req.status(500).json({error: err}));
- })
- router.patch("/:productId", (req,res,next)=>{
- const id = req.params.productId;
- Product.update({_id: id}), {$set:{
- name: req.body.name,
- price: req.body.price
- }}.exec()
- .then(res.status(200).json({
- message: "Szczegóły produktu o nr " + id
- }))
- .catch(err => req.status(500).json({error: err}));
- })
- router.delete("/:productId", (req,res,next)=>{
- const id = req.params.productId;
- Product.remove({_id: id}).exec()
- .then(result => res.status(200).json({
- message: "Usunięcie produktu o nr " + id
- }))
- .catch(err => req.status(500).json({error: err}));
- })
- module.exports = router;
- // dorobić orders!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- // get, post
- // parametryczny: get, delete
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement