georgeB96

API WITH MONGOOSE INSERT

Nov 3rd, 2021
778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //declarations and imports
  2. const express = require('express')
  3. const bodyParser = require('body-parser')
  4. const app = express()
  5. const port = 3000
  6. // const Dog = require('./dog.js')
  7. const mongoose = require('mongoose')
  8. const {Schema} = mongoose;
  9.  
  10. //schema or the set of rules for our class
  11. const dogSchema = new Schema({
  12.   age : Number, // Decimal128 // String
  13.   name: String,
  14.   breed: String,
  15.   isNeutred: Boolean
  16. })
  17. //constructor for our class
  18. const Dog = mongoose.model('Dog', dogSchema);
  19.  
  20. //defining one object using our new constructor
  21. const dog = new Dog({age:5, name: "Boris", breed: "Golden Retriever", isNeutred: false})
  22.  
  23.  
  24.  
  25.  
  26.  
  27. // const path = require('path');
  28.  
  29. //app plugins or libraries
  30. app.use(bodyParser.urlencoded({extended:false}))
  31.  
  32. // let dog1 = new Dog(5, "Roger", "Rotwiler", false);
  33. // let dog2 = new Dog(5, "Boris", "Rotwiler", true);
  34.  
  35. // let dogs = [dog1, dog2]
  36.  
  37. // console.log(dog1);
  38. app.get('/', (req, res) => {
  39.   res.send('George Blanaru')
  40. })
  41.  
  42. app.get('/message', (req, res) => {
  43.     res.send('Hi, this is a nice message');
  44. })
  45.  
  46. app.get('/othermessage', (req, res) => {
  47.     res.send('This is the second message');
  48. })
  49.  
  50. app.get('/showdog', (req, res) =>{
  51.     console.log('Someone is requiring a dog;')
  52.     res.send(dogs)
  53. })
  54.  
  55. app.post('/showdog', (req, res) =>{
  56.   //insert using post!
  57.     console.log('Someone is trying to post something');
  58.     res.send('Congrats, you posted something');
  59.     console.log(req.body);
  60.     // let name = req.body.name;
  61.     // let age = parseInt(req.body.age);
  62.     // let breed = req.body.breed;
  63.     let isNeutred = (req.body.isNeutred === 'true');
  64.     let dog = new Dog(parseInt(req.body.age),req.body.name,req.body.breed, isNeutred);
  65.     dogs.push(dog)
  66.     console.log(dog)
  67. })
  68.  
  69.  
  70.  
  71. app.delete('/deletedog/:name', (req, res) =>{
  72.   //"dogs" is the database
  73.   //newDogsArray is a holder for the updated Database
  74.   let newDogsArray =[];
  75.   //for loop in JavaScript, takes each element and assigns it to the variable "d"
  76.   //
  77.   dogs = dogs.forEach(d =>{
  78.     //checks to see if the name from the parameter matches the dogs name
  79.     if(!(d._name === req.params.name)){
  80.       //if it doesn't match, add it to the new array
  81.        newDogsArray.push(d)
  82.     }
  83.   })
  84.   console.log("Dog deleted, new database looks like this")
  85.   console.log(newDogsArray);
  86.   //update the "database / dogs"
  87.   dogs = newDogsArray;
  88.   //send a result to postman/user so it doesn't get stuck on loading
  89.   res.send("Delete in progress");
  90.  
  91.  
  92. })
  93.  
  94. app.put('/updatedog/', (req,res)=>{
  95.  
  96.   //request to use for UPDATE
  97.  
  98. })
  99. //insert a dog into the database
  100. app.post('/dog',(req,res)=>{
  101.   console.log("Inserting a dog in the database")
  102.   dog.save()
  103.   res.send("Dog saved")
  104. })
  105.  
  106.  
  107.  
  108. app.listen(port, () => {
  109.   mongoose.connect('mongodb+srv://admin:[email protected]/myFirstDatabase?retryWrites=true&w=majority').
  110.   catch(error => console.log(error));
  111.   console.log(`Example app listening at http://localhost:${port}`)
  112. })
Advertisement
Add Comment
Please, Sign In to add comment