Newer
Older
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
import { IDatabase, IServer } from "../interfaces";
import express from "express";
import cors from "cors";
import morgan from "morgan";
import bodyParser from "body-parser";
import { AllProductsRequest, Order, OrderRequest, ProductRequest, UserPatchRequest, UserRequest } from "../types";
export default class RestServer implements IServer {
db: IDatabase;
server: any;
constructor(db: IDatabase) {
this.db = db;
this.server = express();
}
start() {
const port = 3000;
this.server.use(cors());
this.server.use(morgan("tiny"));
this.server.use(bodyParser.json());
this.server.get("/", (req: express.Request, res: express.Response) => res.send("Hello, World!"));
this.server.get("/product/:product_id", async (req: express.Request, res: express.Response) => {
const { product_id } = req.params as ProductRequest;
const product = await this.db.queryProductById(product_id);
res.send(product)
}); // Gets a product by product id
this.server.get("/randomproduct", async (req: express.Request, res: express.Response) => {
const randProd = await this.db.queryRandomProduct();
res.send(randProd);
}); // I'm feeling lucky type
this.server.get("/products", async (req: express.Request, res: express.Response) => {
const { category_id } = req.query as AllProductsRequest;
const products = await this.db.queryAllProducts(category_id);
res.send(products);
}); // Gets all products, or by category
this.server.get("/categories", async (req: express.Request, res: express.Response) => {
const categories = await this.db.queryAllCategories();
res.send(categories);
}); // Gets all categories
this.server.get("/allorders", async (req: express.Request, res: express.Response) => {
const orders = await this.db.queryAllOrders();
res.send(orders);
}); // Gets all orders
this.server.get("/orders", async (req: express.Request, res: express.Response) => {
const { id } = req.query as UserRequest;
const orders = await this.db.queryOrdersByUser(id);
res.send(orders);
}); // Gets all of a single user's orders
this.server.get("/order/:id", async (req: express.Request, res: express.Response) => {
const { id } = req.params as OrderRequest;
const order = await this.db.queryOrderById(id);
res.send(order);
}); // Gets more details on a specific order by id
this.server.get("/user/:id", async (req: express.Request, res: express.Response) => {
const { id } = req.params as UserRequest;
const user = await this.db.queryUserById(id);
res.send(user);
}); // Gets details on a specific user by username
this.server.get("/users", async (_req: express.Request, res: express.Response) => {
const users = await this.db.queryAllUsers();
res.send(users);
});// Gets all users
this.server.post("/orders", async (req: express.Request, res: express.Response) => {
const order = req.body as Order;
const response = await this.db.insertOrder(order);
res.send(response);
}); // Creates a new order
this.server.patch("/user/:id", async (req: express.Request, res: express.Response) => {
const updates = req.body;
const userId = (req.params as UserRequest).id;
const patch: UserPatchRequest = {
...updates,
id: userId,
}
const response = await this.db.updateUser(patch);
res.send(response);
}); // Updates a user's email or password
this.server.listen(port, () => {
console.log(`App ready and listening on port ${port}`);
});
}
}