diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js new file mode 100644 index 00000000..f01235c2 --- /dev/null +++ b/src/controllers/tanksController.js @@ -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); + } + + }; \ No newline at end of file diff --git a/src/handlers/tanksHandler.js b/src/handlers/tanksHandler.js new file mode 100644 index 00000000..e69de29b diff --git a/src/index.js b/src/index.js index 87c47564..61692213 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const userController = require("./controllers/userController"); +const tanksController = require("./controllers/tanksController"); const cors = require("cors"); const swagger = require("./config/swagger"); const rawBody = require('raw-body') @@ -268,7 +269,6 @@ fastify.get('/testtemp', (req, reply) => { }); - //fastify-auth plugin is required so we can define routes in seperate files and verify jwt supplied in preHandlers for each request. const multer = require("fastify-multer"); fastify.register(require("fastify-auth")); @@ -279,6 +279,9 @@ const { Schema } = require("mongoose"); // fastify.register(dbConnection); fastify.register(require("./routes/usersRoute")); + +fastify.register(require("./routes/tanksRoute")); + // Testing route allows for retrieving a user by phone so one can see what is the phone verification code sent for a given user's phone // Also allows deletion of a user with a given phone number fastify.register(require("./routes/forTestingRoute")); diff --git a/src/models/tanks.js b/src/models/tanks.js new file mode 100644 index 00000000..ba09d4dd --- /dev/null +++ b/src/models/tanks.js @@ -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); \ No newline at end of file diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js new file mode 100644 index 00000000..9d6d2230 --- /dev/null +++ b/src/routes/tanksRoute.js @@ -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(); +} + +