SHARE
TWEET

Untitled

a guest Jun 15th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Mongoose = require("mongoose");
  2. const Schema = Mongoose.Schema;
  3. const UserSchema = new Schema({
  4. name : {
  5.     type: String,
  6.     require: true
  7. },
  8. address : {
  9.     type: String,
  10.     require: false
  11. },
  12. password: {
  13.     type: String,
  14.     require: true
  15. }
  16. });
  17.  Mongoose.model('User', UserSchema);
  18.  Mongoose.connect("mongodb://localhost:27017/demo", function (err) {
  19. if (err) {
  20.     console.log(err);
  21.     process.exit(-1);
  22. }
  23. console.log("Connected");
  24. });
  25. module.exports = Mongoose;
  26.      
  27. var Mongoose = require("../DBSchema/DBConfig");
  28. var UserSchema = Mongoose.model('User');
  29. var Controller = function() {
  30. this.insertUser = function (data) {
  31.     return new Promise(function(resolve, reject) {
  32.         var User = new UserSchema({
  33.             name: data.name,
  34.             address: data.address,
  35.             password: data.password
  36.         });
  37.  
  38.         User.save().then(function () {
  39.             resolve({status: 200, message: "User inserted successfully"})
  40.         }).catch(function (err) {
  41.             reject({status: 500, message: "Error:- " + err})
  42.         });
  43.     });
  44. }
  45.  
  46. this.getAll = function () {
  47.     return new Promise(function (resolve, reject) {
  48.         UserSchema.find().exec().then(function (data) {
  49.             resolve({status: 200, Userdata: data})
  50.         }).catch(function (err) {
  51.             resolve({status: 500, message : "No data available"})
  52.         })
  53.     })
  54. }
  55.  
  56. this.getuser = function (id) {
  57.     return new Promise(function (resolve, reject) {
  58.         UserSchema.find({_id: id}).exec().then(function (data) {
  59.             resolve({status: 200, userSearched: data});
  60.         }).catch(function (err) {
  61.             reject({status: 404, message: "User NOT FOUND"});
  62.         })
  63.     })
  64. }
  65. this.updateUser = function (id, data) {
  66.     return new Promise(function (resolve, reject) {
  67.         UserSchema.update({_id: id}, data).then(function (data) {
  68.             resolve({status: 200, message: "User updated successfully"})
  69.         }).catch(function (err) {
  70.             reject({status: 500, message: "Error:- " + err})
  71.  
  72.         })
  73.     })
  74. }
  75.  
  76. this.remove = function (id) {
  77.     return new Promise(function (resolve, reject) {
  78.         UserSchema.remove({_id: id}).then(function () {
  79.             resolve({status: 200, message: "Successfully deleted"})
  80.         }).catch(function (err) {
  81.             resolve({status: 500, message: "Error : "+err})
  82.         })
  83.     })
  84.   }
  85. }
  86. module.exports = new Controller();
  87.      
  88. var Express = require('express');
  89. var router = Express.Router();
  90. var Controller = require('./Controller')
  91.  
  92. router.get('/',function (req,res) {
  93. Controller.getAll().then(function (data) {
  94.     res.status(data.status).send({data:data.Userdata})
  95. });
  96. });
  97.  
  98. router.get('/:id',function (req,res) {
  99. Controller.getuser(req.params.id).then(function (data) {
  100.     res.status(data.status).send({data:data.userSearched});
  101. });
  102. });
  103.  
  104. router.post('/',function(req,res){
  105. Controller.insertUser(req.body).then(function (data) {
  106.     res.status(data.status).send({message: data.message});
  107. }).catch(function (err) {
  108.     res.status(err.status).send({message: err.message});
  109. });
  110. });
  111.  
  112. router.put('/:id',function (req,res) {
  113. Controller.updateUser(req.params.id,req.body).then(function (data) {
  114.     res.status(data.status).send({message: data.message});
  115. });
  116. });
  117.  
  118. router.delete('/:id', function (req,res) {
  119. Controller.remove(req.params.id).then(function (data) {
  120.     res.status(data.status).send({message: data.message});
  121. });
  122. });
  123.  
  124. module.exports = router;
  125.      
  126. const Express = require("express");
  127. var Routes = Express.Router();
  128. var UserRoute = require('./UserController/User.Route');
  129.  
  130. Routes.use('/user/', UserRoute);
  131. module.exports = Routes;
  132.      
  133. const Express = require("express");
  134. const app = Express();
  135. const BodyParser = require("body-parser");
  136. const Routes = require("./Routes");
  137.  
  138. app.use(BodyParser.json());
  139. app.use("/", Routes);
  140.  
  141. app.listen(8083, "localhost", function(err) {
  142. if (err) {
  143. console.log(err);
  144. process.exit(-1);
  145. }
  146. console.log("Server listening on port 8083");
  147. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top