Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express')
  2. const mongoose = require('mongoose')
  3. const Action = require('../models/action')
  4. const Session = require('../models/session')
  5. const router = express.Router()
  6.  
  7. router.get('/', (req, res, next) =>
  8. {
  9.     Action.find()
  10.             .select('-__v')
  11.             .exec()
  12.             .then(docs =>
  13.                 {
  14.                     const response =
  15.                     {
  16.                         count: docs.length,
  17.                         actions: docs.map(action =>
  18.                         {
  19.                             return  {
  20.  
  21.                                         _id: action._id,
  22.                                         visitorId: action.visitorId,
  23.                                         sessionId: action.sessionId,
  24.                                         actionTime: action.actionTime,
  25.                                         actionType: action.actionType,
  26.  
  27.                                         request:
  28.                                         {
  29.                                             type: "GET",
  30.                                             url: req.protocol + '://' + req.get('host') + '/actions/' + action._id,
  31.                                         }
  32.                                     }
  33.                         })
  34.                     }
  35.                     res.status(200).json(response)
  36.                 })
  37.                 .catch(e =>
  38.                 {
  39.                     res.status(500).json({ error: e })
  40.                 })
  41. })
  42.  
  43. router.get('/:actionId', (req, res, next) =>
  44. {
  45.     const id = req.params.actionId
  46.     console.log(id)
  47.     Action.findById(id)
  48.     .select('-__v')
  49.     .exec()
  50.     .then(doc =>
  51.     {
  52.         console.log(doc)
  53.         if(doc)
  54.         {
  55.             res.status(200).json(
  56.                 {
  57.                     action: doc,
  58.                     request:
  59.                     {
  60.                         type: 'GET',
  61.                         url: req.protocol + '://' + req.get('host') + '/actions/'
  62.                     }
  63.                 })
  64.         }
  65.         else
  66.         {
  67.             res.status(404).json({ message: 'No valid entry found for provided ID' })
  68.         }
  69.  
  70.     }).catch(e =>
  71.     {
  72.         console.log(e)
  73.         res.status(500).json({ error: e })
  74.     })
  75. })
  76.  
  77. router.post('/', (req, res, next) =>
  78. {
  79.     const action = new Action(
  80.         {
  81.             _id: new mongoose.Types.ObjectId,
  82.             visitorId: req.body.visitorId,
  83.             sessionId: req.body.sessionId,
  84.             actionTime: req.body.actionTime,
  85.             actionType: req.body.actionType
  86.         })
  87.  
  88.     action.save().then(result =>
  89.     {
  90.         res.status(201).json(
  91.             {
  92.                 message: "Action created",
  93.                 createdAction:
  94.                 {
  95.                     _id: result._id,
  96.                     visitorId: result.visitorId,
  97.                     sessionId: result.sessionId,
  98.                     actionTime: result.actionTime,
  99.                     actionType: result.actionType,
  100.                     request:
  101.                     {
  102.                         type: 'POST',
  103.                         url: req.protocol + '://' + req.get('host') + '/actions/' + result._id
  104.                     }
  105.                 }
  106.             })
  107.     })
  108.     .catch(e =>
  109.     {
  110.         console.log(e)
  111.         res.status(500).json({ error: e })
  112.     })
  113. })
  114.  
  115. router.patch('/:actionId', (req, res, next) =>
  116. {
  117.     const id = req.params.actionId
  118.     const toUpdate = {}
  119.     req.body.map((doc) => toUpdate[doc.propertyName] = doc.value)
  120.  
  121.     Action.update({ _id: id }, { $set: toUpdate }).exec().then((result) =>
  122.     {
  123.         res.status(200).json(
  124.             {
  125.                 message: 'Action updated.',
  126.                 request:
  127.                 {
  128.                     type: 'GET',
  129.                     url: req.protocol + '://' + req.get('host') + req.originalUrl
  130.                 }
  131.             })
  132.     }).catch(e =>
  133.     {
  134.         res.status(500).json({ error: e })
  135.     })
  136. })
  137.  
  138. router.delete("/:actionId", (req, res, next) =>
  139. {
  140.     Action.remove({ _id: req.params.actionId })
  141.         .exec()
  142.         .then(() =>
  143.         {
  144.             res.status(200).json(
  145.                 {
  146.                     message: "Action deleted"
  147.                 })
  148.         })
  149.         .catch(e =>
  150.         {
  151.             res.status(500).json({ error: e })
  152.         })
  153. })
  154.  
  155. router.get('/visitor/:visitorId', (req, res, next) =>
  156. {
  157.     const id = req.params.visitorId
  158.     Action.find({visitorId: id})
  159.     .select('-__v')
  160.     .populate('sessionId')
  161.     .exec()
  162.     .then(doc =>
  163.     {
  164.         if(doc.length<=0)
  165.         {
  166.             res.status(404).json({ message: 'No valid entry found for provided ID' })
  167.         }
  168.         else
  169.         {
  170.             res.status(200).json(
  171.                 {  
  172.                     length: doc.length,
  173.                     actions: doc,
  174.                     request:
  175.                     {
  176.                         type: 'GET',
  177.                         url: req.protocol + '://' + req.get('host') + '/actions/visitor/' + id
  178.                     }
  179.                 })
  180.         }
  181.  
  182.     }).catch(e =>
  183.     {
  184.         res.status(500).json({ error: e })
  185.     })
  186. })
  187.  
  188. module.exports = router
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement