forked from strivedi4u/hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
home.html
98 lines (89 loc) · 4.02 KB
/
home.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>My Cafe</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-route/1.7.9/angular-route.min.js"></script>
</head>
<body ng-controller="HomeController">
<h1>Welcome to My Cafe</h1>
<div>
<h2>Add Product</h2>
<form ng-submit="addProduct()">
<input type="text" ng-model="newProduct.name" placeholder="Product Name" required>
<input type="number" ng-model="newProduct.price" placeholder="Price" required>
<input type="number" ng-model="newProduct.quantity" placeholder="Quantity" required>
<button type="submit">Add</button>
</form>
</div>
<div ng-show="productsLoaded">
<h2>Products</h2>
<ul>
<li ng-repeat="product in products">
<ul>
<li>Name:-{{ product.name }}</li>
<li>Price:-{{ product.price }}</li>
<li>Quantity:-{{ product.quantity }}</li>
</ul>
<button ng-click="deleteProduct(product._id)">Delete</button>
<button ng-click="editProduct(product)">Edit</button>
</li>
</ul>
</div>
<div ng-show="editingProduct">
<h2>Edit Product</h2>
<form ng-submit="updateProduct()">
<input type="text" ng-model="editingProduct.name" required>
<input type="number" ng-model="editingProduct.price" required>
<input type="number" ng-model="editingProduct.quantity" required>
<button type="submit">Update</button>
</form>
</div>
<script>
angular.module('myApp', [])
.controller('HomeController', function($scope, $http) {
$scope.newProduct = {};
$scope.editingProduct = null;
$scope.products = [];
$scope.productsLoaded = false;
// Load products from the server
$http.get('http://localhost:3000/products')
.then(function(response) {
$scope.products = response.data;
$scope.productsLoaded = true;
})
.catch(function(error) {
console.error('Error loading products:', error);
});
$scope.addProduct = function() {
$http.post('http://localhost:3000/products', $scope.newProduct)
.then(function(response) {
$scope.products.push(response.data);
$scope.newProduct = {};
});
};
$scope.deleteProduct = function(productId) {
$http.delete('http://localhost:3000/products/' + productId)
.then(function(response) {
$scope.products = $scope.products.filter(function(product) {
return product._id !== productId;
});
});
};
$scope.editProduct = function(product) {
$scope.editingProduct = angular.copy(product);
};
$scope.updateProduct = function() {
$http.put('http://localhost:3000/products/' + $scope.editingProduct._id, $scope.editingProduct)
.then(function(response) {
var index = $scope.products.findIndex(function(product) {
return product._id === $scope.editingProduct._id;
});
$scope.products[index] = response.data;
$scope.editingProduct = null;
});
};
});
</script>
</body>
</html>