Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment II #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
26 changes: 26 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Node.js CI

on:
push:
branches: [ block* ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Use Node.js 12
uses: actions/setup-node@v1
with:
node-version: 12
- name: branch name
run: echo running on branch ${GITHUB_REF##*/}
- name: run ci
run: cd $(echo "${GITHUB_REF##*/}") && npm ci
- name: build
run: cd $(echo "${GITHUB_REF##*/}") && npm run build --if-present
- name: run test
run: cd $(echo "${GITHUB_REF##*/}") && npm test

68 changes: 68 additions & 0 deletions block-BNaacn/app/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

var express = require('express');
var mongoose = require("mongoose");
var Product = require('./model/product')

var app = express();

mongoose.connect("mongodb://127.0.0.1:27017/sample",
{useNewUrlParser:true,useUnifiedTopology:true},(err)=>{
console.log(err ? err : 'connected to database')
})


app.get('/',(req,res)=>{
res.send('Welcome')
});

app.use(express.json())


app.post('/products',(req,res)=>{
// console.log(req.body);
Product.create(req.body,(err,product) => {
console.log(err,product);
res.json(product);
})


})
app.get('/products',(req,res)=>{
Product.find({},(err,products)=>{
console.log(err,products);
res.json({products:products})
})
})
// app.get('/products',(req,res)=>{
// Product.find({"title":"Google Pixel"},(err,products)=>{
// // console.log(err,products);
// res.json({products})
// })
// });
app.get('/products/:id',(req,res)=>{
var id = req.params.id;
Product.findById(id,(err,products)=>{
console.log(err);
res.json(products)
})
});
app.get('/products/:id',(req,res)=>{
var id = req.params.id;
Product.findByIdAndUpdate(id,req.body,(err,updatedproduct)=>{
console.log(err);
res.json(updatedproduct)
})
})
app.delete('/products/:id',(req,res)=>{
Product.findByIdAndDelete(req.params.id,(err,deletedProduct)=>{
res.send(`${deletedProduct.title} was deleted`);
})
})
app.use((req,res,next)=>{
res.send('Page not found')

});
app.listen(4000,()=>{
console.log('server is listening on port 4k');
})

17 changes: 17 additions & 0 deletions block-BNaacn/app/model/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var userLoginInfo = new Schema({
name : String,
email : {type :String, lowercase :true},
age: {type : Number, default : 0},
favorites : [String],
username : {type : String, required : true},
password : {type : String, minLenth : 5, maxLength : 15},
createdAt : {type : Date, default : new Date()}
},{timestamps : true});

var User = mongoose.model('User',userLoginInfo);

module.exports = User;
61 changes: 61 additions & 0 deletions block-BNaacp/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
writeCode

#### Perform users CRUD operation using mongoose from an express application

- create an express application named sample
- connect to mongodb database using mongoose.connect() in `app.js`
- create a user schema in models directory
- name
- email
- sports
- create user model based on schema
- export it from model
- import it into app.js
- on POST request on `/users` route create a user

Q. Insert a user into database using Model.create() OR model.save() function

- insert user `{name: '', email: '', sports: ['cricket', 'khokho']}`
- check into your local mongo database for inserted data

Q. Query all the users from the database

- use Model.find

Q. query a single document(user) using mongoose

- on GET request on '/users/:id' route, fetch a user
- use Model.find({\_id: 'some-id'}) // use \_id of inserted document in database
- use Model.findOne({\_id: 'some-id'})
- use Model.findById(id)

Mention the difference between them in comments, if any ?

Q. Update a user

- on PUT request on '/users/:id', update a user
- use Model.update
- use Model.updateOne
- use Model.findByIdAndUpdate(id)

Q. delete a user using Model.findByIdAndDelete()

- on DELETE request on '/users/:id'

For example:-

```js
// import User model at top
const User = require("./models/user");

// delete route for deleting a user using id
app.delete("/users", (req, res) => {
var userId = "some id from database";
User.findByIdAndDelete(id, (err, user) => {
if (err) return next(err);
res.send("user deleted");
});
});
```

Similarily, handle all above routes taking help from delete route.
Loading