Skip to content
Snippets Groups Projects
Commit eeacc237 authored by Sandeep Jha's avatar Sandeep Jha
Browse files

first commit

parent 8d6eab4d
Branches master
No related tags found
No related merge requests found
Showing
with 229 additions and 3 deletions
.test.js 0 → 100644
const request = require('supertest')
const mongoose =require('mongoose')
const app = require('./app.js')
beforeAll(async () => {
const url = "mongodb://127.0.0.1:27017/test";
await mongoose.connect(url)
})
describe('post/api/create user',()=>{
test('it should create a user',async()=>{
const data = {
firstName:"max1",
lastName:"simon",
city:"Noida"
}
const response = await request(app).post("/api/v1/create").send(data)
expect(response.status).toBe(200)
expect(response.body.User._id).toBeTruthy();
expect(response.body.User.firstName).toBe(data.firstName);
})
})
describe('get/api/user',()=>{
test('it should getAll users',async()=>{
const response = await request(app).get("/api/v1/users")
expect(response.status).toBe(200)
})
})
describe('put/api/updateuser',()=>{
test('it should update user',async()=>{
const id = "64ab939f89e4714e4b8ad2cc"
const data = {
firstName:"max2"
}
const response = await request(app).put(`/api/v1/update/${id}`).send(data)
expect(response.status).toBe(200)
expect(response.body._id).toBeTruthy();
expect(response.body.firstName).toBe(data.firstName)
})
})
# project3 # assignment3
...@@ -15,14 +15,14 @@ Already a pro? Just edit this README.md and make it your own. Want to make it ea ...@@ -15,14 +15,14 @@ Already a pro? Just edit this README.md and make it your own. Want to make it ea
``` ```
cd existing_repo cd existing_repo
git remote add origin https://gitlab.niveussolutions.com/sandeep.jha/project3.git git remote add origin https://gitlab.niveussolutions.com/sandeep.jha/assignment3.git
git branch -M master git branch -M master
git push -uf origin master git push -uf origin master
``` ```
## Integrate with your tools ## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.niveussolutions.com/sandeep.jha/project3/-/settings/integrations) - [ ] [Set up project integrations](https://gitlab.niveussolutions.com/sandeep.jha/assignment3/-/settings/integrations)
## Collaborate with your team ## Collaborate with your team
......
app.js 0 → 100644
const express =require('express')
const app =express()
const route = require('./routes/userRoute')
app.use(express.json())
app.use('/api/v1',route)
module.exports =app
const user = require('../models/userSchema')
const logger =require('../utils/logger')
//create user
const createUser= async(req,res)=>{
try {
const {firstName,lastName,city}=req.body
if(!firstName||!lastName||!city){
return res.status(422).json('Fields are required')
}else{
const data ={firstName:firstName,lastName:lastName,city:city}
const User = await user.create(data)
return res.status(200).json({message:'User created Successfully',User})
}
} catch (error) {
//console.log(error)
logger.error(error)
}
}
//get Users
const getAllUser = async(req,res)=>{
try {
const Users = await user.find({})
return res.status(200).json({Users})
} catch (error) {
logger.error(error)
}
}
//update user
const updateUser = async(req,res)=>{
try {
const id = req.params.id
const userId = await user.findById(id)
if(!userId){
return res.status(404).json('user not found')
}else{
const updatedUser = await user.findByIdAndUpdate(id,req.body,{new:true,useFindAndModify:false})
return res.status(200).json(updatedUser)
}
} catch (error) {
logger.error(error)
}
}
module.exports ={createUser,getAllUser,updateUser}
\ No newline at end of file
const mongoose =require('mongoose')
const logger = require('../utils/logger')
mongoose.connect('mongodb://127.0.0.1:27017/test')
.then(() =>
//console.log('connected db')
logger.info('connected to db')
).catch((err)=>
logger.error('error while connecting to db',err)
)
\ No newline at end of file
index.js 0 → 100644
const express = require('express')
const app = require('./app.js')
const PORT =process.env.PORT||8080
const logger = require('./utils/logger.js')
const db = require('./helpers/db.js')
app.listen(PORT,()=>{
logger.info('Server is running')
})
\ No newline at end of file
const mongoose =require('mongoose')
const userSchema = new mongoose.Schema({
firstName:{
type:String,
required:true,
trim:true
},
lastName:{
type:String,
required:true,
trim:true,
},
city:{
type:String,
required:true,
trim:true
}
})
const user = mongoose.model('listusers',userSchema)
module.exports =user
../browserslist/cli.js
\ No newline at end of file
../esprima/bin/esparse.js
\ No newline at end of file
../esprima/bin/esvalidate.js
\ No newline at end of file
../import-local/fixtures/cli.js
\ No newline at end of file
../jest/bin/jest.js
\ No newline at end of file
../js-yaml/bin/js-yaml.js
\ No newline at end of file
../jsesc/bin/jsesc
\ No newline at end of file
../json5/lib/cli.js
\ No newline at end of file
../mime/cli.js
\ No newline at end of file
../which/bin/node-which
\ No newline at end of file
../@babel/parser/bin/babel-parser.js
\ No newline at end of file
../resolve/bin/resolve
\ No newline at end of file
../@nicolo-ribaudo/semver-v6/bin/semver.js
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment