Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
79 changes: 77 additions & 2 deletions routes/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
Expand All @@ -160,6 +185,56 @@ router.post("/areaOfRectangle", (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
}
});
}
});


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;
70 changes: 68 additions & 2 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -153,4 +153,70 @@ 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();
}
});
});

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();
}
});
});

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();
}
});
});
});