|
|
|
const Tank = require("../models/tanks");
|
|
|
|
const User = require("../models/User");
|
|
|
|
const boom = require("boom");
|
|
|
|
const fastify = require("fastify")({
|
|
|
|
logger: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.addTanks = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
|
|
|
|
//const username = req.params.username;
|
|
|
|
|
|
|
|
console.log(req.params);
|
|
|
|
const username = loginObject.user.username;
|
|
|
|
//console.log(loginObject.user.username)
|
|
|
|
// const userInfo = await User.findOne({ username: username.toString() });
|
|
|
|
// const updateData = req.body;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// console.log("This is the reply in the handler after the validations", reply);
|
|
|
|
tankData = {
|
|
|
|
userName: username,
|
|
|
|
tankName: req.body.tankName,
|
|
|
|
blockName: req.body.blockName,
|
|
|
|
capacity: req.body.capacity,
|
|
|
|
typeOfWater: req.body.typeOfWater,
|
|
|
|
type: req.body.type,
|
|
|
|
tankLocation:req.body.tankLocation,
|
|
|
|
};
|
|
|
|
|
|
|
|
var tank_name = req.body.tankName
|
|
|
|
var i_tank = await Tank.findOne({ tankName: tank_name,userName:username})
|
|
|
|
if(i_tank){
|
|
|
|
throw new Error('tankname already exists');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
var tank = new Tank(tankData);
|
|
|
|
|
|
|
|
checkFormEncoding = isUserFormUrlEncoded(req);
|
|
|
|
if (checkFormEncoding.isUserFormUrlEncoded) {
|
|
|
|
usertobeInserted = checkFormEncoding.tank;
|
|
|
|
console.log("thsi true url string");
|
|
|
|
tank.tankName = usertobeInserted.tankName;
|
|
|
|
tank.blockName = usertobeInserted.blockName;
|
|
|
|
tank.capacity = usertobeInserted.capacity;
|
|
|
|
tank.typeOfWater = usertobeInserted.typeOfWater;
|
|
|
|
tank.typer = usertobeInserted.type;
|
|
|
|
tank.tankLocation = usertobeInserted.tankLocation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const insertedTank = await tank.save();
|
|
|
|
|
|
|
|
return insertedTank;
|
|
|
|
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//update selected tank
|
|
|
|
exports.updateTanksInfo = async (req, reply) => {
|
|
|
|
|
|
|
|
try {
|
|
|
|
const username = loginObject.user.username;
|
|
|
|
const tankName = req.params.tankName;
|
|
|
|
const tank = req.body;
|
|
|
|
const { ...updateData } = tank;
|
|
|
|
const update = await Tank.findOneAndUpdate({ tankName: tankName,userName:username, }, updateData, { new: true });
|
|
|
|
console.log(update.username)
|
|
|
|
//return update;
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: update });
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//delete selected tank
|
|
|
|
exports.deleteTanksInfo = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
const username = loginObject.user.username;
|
|
|
|
const tankName = req.params.tankName;
|
|
|
|
const tank = await Tank.findOneAndDelete({ tankName: tankName,userName:username });
|
|
|
|
|
|
|
|
reply.send({ status_code: 200, data: tank});
|
|
|
|
// return tank;
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//get tanks data by passing username
|
|
|
|
exports.getTank = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
await Tank.find({userName: req.query.userName})
|
|
|
|
.exec()
|
|
|
|
.then((docs) => {
|
|
|
|
reply.send({ status_code: 200, data: docs, count: docs.length });
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
reply.send({ error: err });
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw boom.boomify(err);
|
|
|
|
}
|
|
|
|
};
|