-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
79 lines (67 loc) · 2.33 KB
/
server.ts
File metadata and controls
79 lines (67 loc) · 2.33 KB
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
import express, { Request, Response } from 'express';
import cors from 'cors';
import { Product } from './src/models/Product';
import { InMemoryProductRepository } from './src/repositories/ProductRepository';
import { ProductService } from './src/services/ProductService';
import { DiscountedPrice, RegularPrice } from './src/models/PriceStrategy';
const app = express();
const PORT = 3001;
// middleware
app.use(cors());
app.use(express.json());
const productRepository = new InMemoryProductRepository();
const productService = new ProductService(productRepository);
// Routes
// List All Products
app.get("/products", (req: Request, res: Response) => {
res.json(productService.getProducts().map(product => ({
id: product.getId(),
name: product.getName(),
price: product.getFinalPrice()
})));
})
// Add a new product
app.post("/products", (req: Request, res: Response) => {
const { id, name, price, discount } = req.body;
let priceStrategy = new RegularPrice();
if(discount) {
priceStrategy = new DiscountedPrice(discount);
}
try{
const product = new Product(id, name, price, priceStrategy);
productService.addProduct(product);
res.status(201).send({message: "Product added successfully."});
} catch (error) {
if (error instanceof Error) {
res.status(400).send({ message: error.message });
} else {
res.status(400).send({ message: 'An unexpected error occurred.' });
}
}
})
// Get Product Details
app.get("/products/:id", (req: Request, res: Response) => {
const product = productService.getProductById(req.params.id);
if(product) {
res.json({
id: product.getId(),
name: product.getName(),
price: product.getFinalPrice()
});
} else {
res.status(404).send({message: "Product not found."});
}
})
// Delete a product
app.delete("/products/:id", (req: Request, res: Response) => {
const product = productService.getProductById(req.params.id);
if(product) {
productService.deleteProduct(req.params.id);
res.send({message: "Product deleted successfully."});
} else {
res.status(404).send({message: "Product not found."});
}
})
app.listen(PORT, () => {
console.log(`Server is running at http://localhost:${PORT}`);
})