Projekti kood

This commit is contained in:
Arti Zirk 2017-06-07 14:55:51 +03:00
parent 3898aecb9b
commit 8aee3b6c3f
3 changed files with 87 additions and 7 deletions

View File

@ -1,16 +1,87 @@
<!doctype html> <!doctype html>
<html lang="et"> <html lang="et" ng-app="app">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
</head> </head>
<body ng-controller="C1 as vm"> <body ng-controller="C1 as vm">
<div ng-repeat="item in vm.cart">
{{ item.name }}
<a href="" style="text-decoration: none" ng-click="vm.changeCount(item, -1)">&nbsp;-&nbsp;</a>
<input size="1" ng-model="item.count" ng-change="vm.setCount(item)" ng-reflect-model="1">
<a href="" style="text-decoration: none" ng-click="vm.changeCount(item, +1)">&nbsp;+&nbsp;</a>
</div>
<br>
Kokku: {{ vm.total }} EUR
<br><br>
<button ng-click=vm.submitOrder();>Saada tellimus</button>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script> <script>
(function () {
'use strict';
angular.module('app', []);
angular.module('app').controller('C1', function ($http) {
var vm = this;
vm.cart = [
{
"_id":0,
"name":"Banaanid (1 EUR/KG)",
"price":1,
"count":0
},
{
"_id":1,
"name":"Õunad (2 EUR/KG)",
"price":2,
"count":0
}
];
vm.total = 0;
vm.calcTotal = calcTotal;
vm.changeCount = changeCount;
vm.setCount = setCount;
vm.submitOrder = submitOrder;
calcTotal();
function calcTotal(){
var total = 0;
for (let item of vm.cart) {
total += item.price*item.count;
}
vm.total = total;
}
function changeCount(item, amount) {
var count = parseInt(item.count, 10) + amount;
if (count >= 0) {
item.count = count;
}
calcTotal();
}
function setCount(item, amount) {
// How to apply changes?
var count = parseInt(item.count, 10);
if (count == NaN) {
count = 0;
}
item.count = count;
}
function submitOrder(){
$http.post("/api/cart/order", vm.cart)
.then(resp => console.log("Cart submitted", resp))
.catch(error => console.log("Submittion failed", error))
}
});
// kood tuleb siia })();
</script> </script>
</body> </body>

View File

@ -2,7 +2,7 @@
"name": "i399exam", "name": "i399exam",
"version": "1.0.0", "version": "1.0.0",
"scripts": { "scripts": {
"serve": "nodemon server.js" "start": "nodemon server.js"
}, },
"devDependencies": { "devDependencies": {
"body-parser": "^1.17.1", "body-parser": "^1.17.1",

View File

@ -6,10 +6,19 @@ const app = express();
app.use(bodyParser.json()); app.use(bodyParser.json());
app.use(express.static('./')); app.use(express.static('./'));
app.get('/...', f); app.get('/api/cart', getCart);
app.post('/api/cart/order', handleOrder);
app.listen(3000, () => console.log('running at: http://localhost:3000')); app.listen(3000, () => console.log('running at: http://localhost:3000'));
function f(request, response) { function getCart(req, resp) {
response.end('ok'); resp.set('Content-Type', 'application/json');
resp.end("");
}
function handleOrder(req, resp) {
resp.set('Content-Type', 'application/json');
var order = req.body;
console.log(order);
resp.end("");
} }