Skip to content

Commit

Permalink
Added Tank Details for Reading route (vinitshahdeo#181)
Browse files Browse the repository at this point in the history
* added more fields in readingSchema

* updated readingRoutes to find using tankId

* corrected case of tankId

* updated reading routes according to reading schema

* added jsdoc compatible comments

Co-authored-by: Arpit Jain <[email protected]>
  • Loading branch information
akt114 and ArpitKotecha authored Apr 17, 2020
1 parent f448795 commit 7e3a2bc
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api/models/Reading.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ const readingSchema = new mongoose.Schema({
required: true,
type: mongoose.Types.ObjectId
},
tankCapacity: {
capacity: {
required: true,
type: Number
},
tankLocation: {
location: {
type: pointSchema,
required: true
},
tankName: {
name: {
required: true,
type: String
}
Expand Down
43 changes: 33 additions & 10 deletions api/routes/readingRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ const express = require("express");
const sanitizer = require("sanitizer");
const router = express.Router();
const Reading = require("../models/Reading");
//Add a new reading
/**
* Add a new reading.
* @returns {data} New tank reading
* @throws {Internal Error} When reading can't be added or error occurs.
*/
router.post("/new", (req, res) => {
//Sanitize the input to prevent malicious data
/**
*Sanitize the input to prevent malicious data
*/
const reading = { value: sanitizer.sanitize(req.body.value) };
Reading.create(reading)
.then(result => {
Expand All @@ -16,7 +22,11 @@ router.post("/new", (req, res) => {
res.status(500).json({ mssg: "Internal Error" });
});
});
//Read all readings
/**
* Read all readings.
* @returns {Reading} Tank Details
* @throws {Internal Error} When error occurs.
*/
router.get("/all", (req, res) => {
Reading.find({})
.then(result => {
Expand All @@ -26,38 +36,51 @@ router.get("/all", (req, res) => {
res.status(500).json({ mssg: "Internal Error" });
});
});
//Read particular reading

/**
* Read a particular reading.
* @returns {data} Tank reading
* @throws {Internal Error} When reading can't be found or error occurs.
*/
router.get("/:readId", (req, res) => {
const readId = sanitizer.sanitize(req.params.readId);
Reading.findById(readId)
Reading.findOne({tankId:readId})
.then(result => {
res.status(200).json({ mssg: "Ok", data: result });
})
.catch(err => {
res.status(500).json({ mssg: "Internal Error" });
});
});
//Update particular reading
/**
* Update a particular reading.
* @returns {data} Updated Tank reading
* @throws {Internal Error} When reading can't be updated or error occurs.
*/
router.put("/:readId", (req, res) => {
const readId = sanitizer.sanitize(req.params.readId);
const value = sanitizer.sanitize(req.body.value);
Reading.findByIdAndUpdate(readId, { $set: { value } }, { new: true })
Reading.findByIdAndUpdate({tankId:readId}, { $set: { value } }, { new: true })
.then(result => {
res.status(200).json({ mssg: "Ok", data: result });
})
.catch(err => {
res.status(500).json({ mssg: "Internal Error" });
});
});
//Delete particular reading
/**
* Delete a particular reading.
* @returns {data} Deleted Tank reading
* @throws {Internal Error} When reading can't be deleted or error occurs.
*/
router.delete("/:readId", (req, res) => {
const readId = sanitizer.sanitize(req.params.readId);
Reading.findByIdAndDelete(readId)
Reading.findByIdAndDelete({tankId:readId)
.then(result => {
res.status(200).json({ mssg: "Ok", data: result });
})
.catch(err => {
res.status(500).json({ mssg: "Internal Error" });
});
});
module.exports = router;
module.exports = router;
168 changes: 168 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e3a2bc

Please sign in to comment.