From 312e6be9b86d8750d72cc894a7870a3d26d48abc Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 23 Oct 2019 22:09:15 +0530 Subject: [PATCH 1/3] Added area of square --- routes/route.js | 29 +++++++++++++++++++++++++++-- test/tests.js | 26 ++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/routes/route.js b/routes/route.js index eacbb2d..e5a0347 100644 --- a/routes/route.js +++ b/routes/route.js @@ -139,13 +139,38 @@ router.post("/areaOfRectangle", (req, res) => { try{ const { param1, param2 } = req.body; - let result = parseFloat(param2, 10) * parseFloat(param1, 10); + let result = parseFloat(param1, 10) * parseFloat(param2, 10); + console.log(result) + res.json({ + result, + meta: { + success:true, + message: `Calculated area of rectangle with sides ${param1, param2}`, + code: 200 + } + }); + } catch (err) { + res.json({ + meta: { + success: false, + message: err.message, + code: 400 + } + }); + } +}); + +router.post("/areaOfSquare", (req, res) => { + try{ + const { param1 } = req.body; + + let result = Math.pow(parseFloat(param1, 10),2); res.json({ result, meta: { success:true, - message: `Calculated area of rectangle with sides ${param1}`, + message: `Calculated area of square with side ${param1}`, code: 200 } }); diff --git a/test/tests.js b/test/tests.js index f77d81a..4e64ed3 100644 --- a/test/tests.js +++ b/test/tests.js @@ -135,8 +135,8 @@ describe("----------START TEST FOR app.js----------", () => { it("Checks the POST /math/areaOfRectangle", done => { chai .request(app) - .post("/math/power") - .send({ param1: 3, param2: 2 }) + .post("/math/areaOfRectangle") + .send({ param1: 2, param2: 3}) .end((err, res) => { if (err) { done(err); @@ -153,4 +153,26 @@ describe("----------START TEST FOR app.js----------", () => { } }); }); + + it("Checks the POST /math/areaOfSquare", done => { + chai + .request(app) + .post("/math/areaOfSquare") + .send({ param1: 1 }) + .end((err, res) => { + if (err) { + done(err); + process.exit(1); + } else { + res.body.result.should.be.a("number"); + res.body.meta.success.should.be.a("boolean"); + res.body.meta.message.should.be.a("string"); + res.body.meta.code.should.be.a("number"); + + res.body.result.should.equal(1); + + done(); + } + }); + }); }); From a6ff93f0ec24ebefa41ee7d13e3d4e344b63e3bf Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 23 Oct 2019 22:15:55 +0530 Subject: [PATCH 2/3] Added area of triangle --- routes/route.js | 25 +++++++++++++++++++++++++ test/tests.js | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/routes/route.js b/routes/route.js index e5a0347..1e0300c 100644 --- a/routes/route.js +++ b/routes/route.js @@ -185,6 +185,31 @@ router.post("/areaOfSquare", (req, res) => { } }); +router.post("/areaOfTriangle", (req, res) => { + try{ + const { param1, param2 } = req.body; + + let result = 0.5* parseFloat(param1, 10) * parseFloat(param2, 10); + console.log(result) + res.json({ + result, + meta: { + success:true, + message: `Calculated area of rectangle with sides ${param1, param2}`, + code: 200 + } + }); + } catch (err) { + res.json({ + meta: { + success: false, + message: err.message, + code: 400 + } + }); + } +}); + module.exports = router; diff --git a/test/tests.js b/test/tests.js index 4e64ed3..d6800f2 100644 --- a/test/tests.js +++ b/test/tests.js @@ -175,4 +175,26 @@ describe("----------START TEST FOR app.js----------", () => { } }); }); + + it("Checks the POST /math/areaOfTriangle", done => { + chai + .request(app) + .post("/math/areaOfTriangle") + .send({ param1: 1, param2 : 2}) + .end((err, res) => { + if (err) { + done(err); + process.exit(1); + } else { + res.body.result.should.be.a("number"); + res.body.meta.success.should.be.a("boolean"); + res.body.meta.message.should.be.a("string"); + res.body.meta.code.should.be.a("number"); + + res.body.result.should.equal(1); + + done(); + } + }); + }); }); From b66e1cb07c53b5f2681d714717fab9f105c65f23 Mon Sep 17 00:00:00 2001 From: Aditya Date: Wed, 23 Oct 2019 22:28:45 +0530 Subject: [PATCH 3/3] Added area of Ellipse --- app.js | 2 +- routes/route.js | 25 +++++++++++++++++++++++++ test/tests.js | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index 9843e61..eb28a42 100644 --- a/app.js +++ b/app.js @@ -2,7 +2,7 @@ const express = require('express'); const bodyParser = require("body-parser"); const app = express(); -const PORT = 5000; +const PORT = 6000; app.use(bodyParser.json()); app.use('/math', require('./routes/route')); diff --git a/routes/route.js b/routes/route.js index 1e0300c..6cc4ce8 100644 --- a/routes/route.js +++ b/routes/route.js @@ -211,5 +211,30 @@ router.post("/areaOfTriangle", (req, res) => { }); +router.post("/areaOfEllipse", (req, res) => { + try{ + const { param1, param2 } = req.body; + + let result = Math.PI * parseFloat(param1, 10) * parseFloat(param2, 10); + console.log(result) + res.json({ + result, + meta: { + success:true, + message: `Calculated area of rectangle with sides ${param1, param2}`, + code: 200 + } + }); + } catch (err) { + res.json({ + meta: { + success: false, + message: err.message, + code: 400 + } + }); + } +}); + module.exports = router; diff --git a/test/tests.js b/test/tests.js index d6800f2..9973ef9 100644 --- a/test/tests.js +++ b/test/tests.js @@ -197,4 +197,26 @@ describe("----------START TEST FOR app.js----------", () => { } }); }); + + it("Checks the POST /math/areaOfEllipse", done => { + chai + .request(app) + .post("/math/areaOfEllipse") + .send({ param1: 1, param2 : 2}) + .end((err, res) => { + if (err) { + done(err); + process.exit(1); + } else { + res.body.result.should.be.a("number"); + res.body.meta.success.should.be.a("boolean"); + res.body.meta.message.should.be.a("string"); + res.body.meta.code.should.be.a("number"); + + res.body.result.should.equal(Math.PI * 2); + + done(); + } + }); + }); });