From fef12c9389be362ecf82070a209b2cee0c924f59 Mon Sep 17 00:00:00 2001 From: varun Date: Mon, 10 Apr 2023 02:36:14 -0400 Subject: [PATCH] added capacity calculator --- src/controllers/tanksController.js | 146 ++++++++++++++++++++++++++++- src/routes/tanksRoute.js | 36 ++++++- 2 files changed, 180 insertions(+), 2 deletions(-) diff --git a/src/controllers/tanksController.js b/src/controllers/tanksController.js index 4c7a7fcf..0d290510 100644 --- a/src/controllers/tanksController.js +++ b/src/controllers/tanksController.js @@ -510,4 +510,148 @@ exports.consumption = async (req, reply) => { } catch (err) { throw boom.boomify(err); } -}; \ No newline at end of file +}; + +exports.calculateCapacity = async (req, reply) => { + try { + const shape = req.body.shape + if(shape==="rectangle"){ + const { length, width, height } = req.body + + // Ensure all parameters are valid numbers + if (isNaN(length) || isNaN(width) || isNaN(height)) { + reply.code(400).send('Invalid input parameters') + return + } + + // Calculate the capacity of the water tank in liters + const capacity = length * width * height * 1000 + + reply.send({ status_code: 200, capacity: capacity}); + + + return { message: 'success' }; + + } + if(shape==="horizontalcylinder"){ + console.log("hii1") + const { length,diameter } = req.body + + // Ensure all parameters are valid numbers + if (isNaN(length) || isNaN(diameter)) { + reply.code(400).send('Invalid input parameters') + return + } + + // Calculate the capacity of the water tank in liters + const radius = diameter / 2 + const volume = Math.PI * Math.pow(radius, 2) * length + const capacity = volume * 1000 + + reply.send({ status_code: 200, capacity: capacity}); + + + return { message: 'success' }; + + } + if(shape==="verticalcylinder"){ + console.log("hii2") + const { height,diameter } = req.body + + // Ensure all parameters are valid numbers + if (isNaN(height) || isNaN(diameter)) { + reply.code(400).send('Invalid input parameters') + return + } + + // Calculate the capacity of the water tank in liters + const radius = diameter / 2 + const volume = Math.PI * Math.pow(radius, 2) * height + const capacity = volume * 1000 + + reply.send({ status_code: 200, capacity: capacity}); + + + return { message: 'success' }; + +} + if(shape==="horizontaloval"){ + console.log("hii3") + const { length, width, height } = req.body + + // Ensure all parameters are valid numbers + if (isNaN(length) || isNaN(width) || isNaN(height)) { + reply.code(400).send('Invalid input parameters') + return + } + + // Calculate the capacity of the water tank in liters + const radius = height / 2 + const a = width - height + const area = Math.PI * radius * radius + 2 * radius * a + const volume = area * length + const capacity = volume * 1000 + + // Format the result with two decimal places and comma-separated thousands + const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + + reply.send({ status_code: 200, capacity: formattedCapacity}); + + + return { message: 'success' }; + +} + +if(shape=== "verticaloval"){ + console.log("hii4") + const { length, width, height } = req.body + + // Ensure all parameters are valid numbers + if (isNaN(length) || isNaN(width) || isNaN(height)) { + reply.code(400).send('Invalid input parameters') + return + } + + // Calculate the capacity of the water tank in liters + const radius = width / 2 + const a = height - width + const area = Math.PI * radius * radius + 2 * radius * a + const volume = area * length + const capacity = volume * 1000 + + // Format the result with two decimal places and comma-separated thousands + const formattedCapacity = capacity.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,') + + reply.send({ status_code: 200, capacity: formattedCapacity}); + + + return { message: 'success' }; + +} +if(shape==="horizontalellipse"){ + const { length, width, height } = req.body + + // Ensure all parameters are valid numbers + if (isNaN(length) || isNaN(width) || isNaN(height)) { + reply.code(400).send('Invalid input parameters') + return + } + + // Calculate the capacity of the water tank in liters + const radius1 = length / 2 + const radius2 = width / 2 + const volume = Math.PI * radius1 * radius2 * height + const capacity = volume * 1000 + reply.send({ status_code: 200, capacity: capacity}); + + + return { message: 'success' }; + +} + + } + + catch (err) { + throw boom.boomify(err); + } +}; diff --git a/src/routes/tanksRoute.js b/src/routes/tanksRoute.js index e96e30ec..07a292d0 100644 --- a/src/routes/tanksRoute.js +++ b/src/routes/tanksRoute.js @@ -234,6 +234,7 @@ module.exports = function (fastify, opts, next) { percentage: { type: "string",default: "100" }, startTime:{ type: "string" }, stopTime:{ type: "string" }, + }, }, security: [ @@ -280,7 +281,40 @@ module.exports = function (fastify, opts, next) { handler: tanksController.consumption, }); - + fastify.route({ + method: "PUT", + url: "/api/calculateCapacity", + schema: { + tags: ["Tank"], + summary: "This is for calculating the capacity", + + + body: { + type: "object", + // required: ['phone'], + properties: { + shape: { type: "string", default: null }, + length: { type: "string", default: null }, + width: { type: "string", default: null }, + height: { type: "string" }, + diameter:{ type: "string" }, + }, + }, + security: [ + { + basicAuth: [], + }, + ], + }, + // preHandler: [ + // fastify.auth([fastify.operatorAuthenticate]), + // validationHandler.validatePhoneFormat, + // ], + preHandler: fastify.auth([fastify.authenticate]), + handler: tanksController.calculateCapacity, + }); + + next(); }