Skip to content
Snippets Groups Projects
Commit a07f3eb7 authored by Raed's avatar Raed
Browse files

Initial commit

parents
Branches master
No related tags found
No related merge requests found
from create_aws_instance import main # this comes from a compiled binary
if __name__ == "__main__":
main()
File added
botocore
boto3
/**
* Object containing the arithmetic operations
*/
function Compute() {
/**
* @method add
* @desc Calculate the sum of two numbers
* @param {Number} a First member of the operation
* @param {Number} b Second member of the operation
* @returns {Number} Sum of a and b
*/
// TODO: modify with the correct operator
// We intentionally kept this mistake: * operator instead of + operator
this.add = (a, b) => Number(a) * Number(b);
/**
* @method sub
* @desc Calculate the difference between two numbers
* @param {Number} a First member of the operation
* @param {Number} b Second member of the operation
* @returns {Number} Difference between a and b (a - b)
*/
this.sub = (a, b) => Number(a) - Number(b);
}
module.exports = {Compute}
\ No newline at end of file
const mongoose = require('mongoose');
const operationSchema = new mongoose.Schema({
a: {type: Number},
b: {type: Number},
operator: {type: String, trim: true},
result: {type: Number},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
module.exports = mongoose.model('Operation', operationSchema);
\ No newline at end of file
This diff is collapsed.
{
"name": "tuto_genie_logiciel_preparation",
"version": "1.0.0",
"description": "C'est un dépôt privé pour préparer le tuto du cours Génie Logiciel avant de les donner aux étudiants",
"main": "app.js",
"scripts": {
"watch": "nodemon --ignore **/aws_script/** ./app.js",
"test": "jest ./tests",
"docs:md": "mkdir -p documentation && documentation build compute.js -f md -o documentation/doc.md",
"docs:html": "documentation build compute.js -f html -o public/docs"
},
"repository": {
"type": "git",
"url": "ssh://git@ssh.hesge.ch:10572/cours-ihm/tuto_genie_logiciel_preparation.git"
},
"keywords": [],
"author": "Raed Abdennadher",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"documentation": "^12.1.2",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.2.0",
"jest": "^24.9.0",
"moment": "^2.24.0",
"mongoose": "^5.7.7",
"pug": "^2.0.4"
},
"devDependencies": {
"nodemon": "^1.19.4"
}
}
.opr-btns {
text-align: center;
}
.error {
color: red;
}
.container-table tr {
vertical-align: middle;
}
.calculator-td{
width: 300px;
}
.history-table {
width: 100%;
border-collapse: collapse;
}
.history-table tr:nth-child(even) {
background: rgb(224, 224, 224)
}
.history-table tr:nth-child(odd) {
background: #FFF
}
.history-line {
border-bottom:1pt solid black;
}
.history-container {
overflow-y: auto;
padding: 5px 5px;
margin: 5px 0;
height: 200px;
width: 500px;
border: 1px solid;
}
.datetime-td {
width: 35%;
}
footer {
position: fixed;
left: 20px;
bottom: 20px;
width: 100%;
color: white;
}
\ No newline at end of file
$( document ).ready(function() {
$('.btn').on('click', function(){
$('#operator').val($(this).val());
});
});
\ No newline at end of file
This diff is collapsed.
const express = require('express');
const { body, validationResult } = require('express-validator');
const { Compute } = require('../compute');
const router = express.Router();
const Operation = require('../models/Operation');
router.get('/', async (req, res) => {
const resultBodyContent = {
operations: await getOperationsList(),
moment: require('moment')
}
res.render('main-content', resultBodyContent);
});
router.post('/', [
body('a').isInt().withMessage('"a" must be a number'),
body('b').isInt().withMessage('"b" must be a number'),
body('operator').isIn(["+", "-", "x"]).withMessage('Operator must be "+", "-" or "x"'),
], async (req, res) => {
// preprare return body
const resultBodyContent = {
moment: require('moment'),
errors: []
}
const errors = validationResult(req);
if (errors.isEmpty()) {
const a = req.body.a;
const b = req.body.b;
const operator = req.body.operator;
const compute = new Compute();
var result = 0;
switch (operator) {
case "+":
result = compute.add(a, b);
break;
case "-":
result = compute.sub(a, b);
break;
default:
break;
}
const operation = new Operation({
a: a,
b: b,
operator: operator,
result: result
})
resultBodyContent.result = a + " " + operator + " " + b + " = " + result;
await operation.save()
.then(() => {})
.catch(() => {
resultBodyContent.errors.push("Can't save data in database!");
});
} else {
resultBodyContent.errors.push(...errors.array());
resultBodyContent.data = req.body;
}
resultBodyContent.operations = await getOperationsList();
res.render('main-content', resultBodyContent);
});
async function getOperationsList() {
return await Operation.find().sort({"created_at": -1});
}
module.exports = router;
\ No newline at end of file
const {Compute} = require('../compute');
const operation = new Compute();
// test Compute.add function
test.each([[1, 1, 2], [-1, 2, 1], [-2, -1, -3]])(
'%i + %i equals %i', (a, b, expected) => {
expect(operation.add(a, b)).toBe(expected);
}
);
// test Compute.sub function
test.each([[1, 1, 0], [-1, 1, -2], [5, 3, 2]])(
'%i - %i equals %i', (a, b, expected) => {
expect(operation.sub(a, b)).toBe(expected);
}
);
\ No newline at end of file
- let title="Calculator"
doctype html
html
head
title=title
link(rel="stylesheet", href="css/app.css")
script(src="js/jquery-3.4.1.min.js")
script(src="js/form.js")
body
h1 #{title}
block content
footer
strong
a(href="docs") documentation
\ No newline at end of file
extends layout
block content
-data = data || {}
-operations = operations || {}
table.container-table
tr
td.calculator-td
if errors
ul
for error in errors
li.error= error.msg
form(action="." method="POST")
table
tr
td
label(for="a") a:
td
input(
type="number"
id="a"
name="a"
value=data.a
)
tr
td
label(for="b") b:
td
input(
type="number"
id="b"
name="b"
value=data.b
)
tr
td(colspan=2 class="opr-btns")
input(type="submit" value="+" class="btn")
input(type="submit" value="-" class="btn")
input(type="hidden" name="operator" id="operator")
h3= result
td.history-td
div
strong History (from mongo database)
div.history-container
if operations.length
table.history-table
each operation in operations
tr
td.datetime-td !{moment(operation.created_at).format('MMMM Do YYYY, h:mm:ss a')}
td #{operation.a} #{operation.operator} #{operation.b} = #{operation.result}
else
| No history yet
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment