parent
294839b817
commit
c229c947af
@ -0,0 +1,78 @@
|
|||||||
|
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 } = req.params;
|
||||||
|
// 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,
|
||||||
|
tankLocation:req.body.tankLocation,
|
||||||
|
};
|
||||||
|
|
||||||
|
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.tankLocation = usertobeInserted.tankLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
const insertedTank = await tank.save();
|
||||||
|
|
||||||
|
return insertedTank;
|
||||||
|
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
throw boom.boomify(err);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
exports.editTanksInfo = async (req, body) => {
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { tankId } = req.params;
|
||||||
|
const tankInfo = await Tank.findById(tankId);
|
||||||
|
const updateData = req.body;
|
||||||
|
console.log(updateData.tankName);
|
||||||
|
|
||||||
|
if (updateData.tankName) tankInfo.tankName = updateData.tankName;
|
||||||
|
if (updateData.blockName) tankInfo.blockName = updateData.blockName;
|
||||||
|
if (updateData.capacity) tankInfo.capacity = updateData.capacity;
|
||||||
|
if (updateData.typeOfWater) tankInfo.typeOfWater = updateData.typeOfWater;
|
||||||
|
if (updateData.tankLocation) tankInfo.tankLocation = updateData.tankLocation;
|
||||||
|
|
||||||
|
const tanks = await tankInfo.save();
|
||||||
|
return tanks;
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
throw boom.boomify(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
@ -0,0 +1,23 @@
|
|||||||
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const Schema = mongoose.Schema;
|
||||||
|
const ObjectId = Schema.Types.ObjectId;
|
||||||
|
|
||||||
|
// Store a random password reset code
|
||||||
|
const code = Math.floor(100000 + Math.random() * 900000);
|
||||||
|
const RoleSchema = new Schema({ name: String });
|
||||||
|
const tanksSchema = new mongoose.Schema({
|
||||||
|
|
||||||
|
userName: { type: String, default: null },
|
||||||
|
tankName: { type: String, default: null },
|
||||||
|
blockName: { type: String, default: null },
|
||||||
|
capacity: { type: Number, default: null },
|
||||||
|
typeOfWater: { type: String, default: null },
|
||||||
|
tankLocation: { type: String, default: null },
|
||||||
|
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model("Tank", tanksSchema);
|
@ -0,0 +1,41 @@
|
|||||||
|
const fastify = require("fastify");
|
||||||
|
const tanksController = require("../controllers/tanksController");
|
||||||
|
|
||||||
|
module.exports = function (fastify, opts, next) {
|
||||||
|
|
||||||
|
fastify.route({
|
||||||
|
method: "POST",
|
||||||
|
url: "/api/addTanks",
|
||||||
|
schema: {
|
||||||
|
tags: ["Tank"],
|
||||||
|
description: "This is for cretae New Tank",
|
||||||
|
summary: "This is for Create New Tank.",
|
||||||
|
body: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
tankName: { type: "string", default: null },
|
||||||
|
blockName: { type: "string", default: null },
|
||||||
|
capacity: { type: "number" },
|
||||||
|
typeOfWater: { type: "string", default: null },
|
||||||
|
tankLocation: { type: "string", default: null },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
security: [
|
||||||
|
{
|
||||||
|
basicAuth: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
},
|
||||||
|
preHandler: fastify.auth([fastify.authenticate]),
|
||||||
|
handler: tanksController.addTanks,
|
||||||
|
// onResponse: (request, reply) => {
|
||||||
|
// validationHandler.sendPhoneVerificationCode(request, reply);
|
||||||
|
// },
|
||||||
|
//onResponse: validationHandler.sendPhoneVerificationCode,
|
||||||
|
});
|
||||||
|
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in new issue