This repository has been archived by the owner on Jul 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 213
/
storefront.ts
87 lines (77 loc) · 2.25 KB
/
storefront.ts
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
/**
* Storefront Router
*
* Router class that defines API REST endpoints used for buying & selling Kitty Items.
* Endpoints call storefront service with request data.
*
*/
import express, {Request, Response, Router} from "express"
import {body} from "express-validator"
import {validateRequest} from "../middlewares/validate-request"
import {StorefrontService} from "../services/storefront"
function initStorefrontRouter(storefrontService: StorefrontService): Router {
const router = express.Router()
router.post(
"/market/buy",
[body("account").exists(), body("itemID").isInt()],
validateRequest,
async (req: Request, res: Response) => {
const {account, itemID} = req.body
const tx = await storefrontService.buy(account, itemID)
return res.send({
transactionId: tx,
})
}
)
router.post("/market/setup", async (req: Request, res: Response) => {
const tx = await storefrontService.setupAccount()
return res.send({
transactionId: tx,
})
})
router.post(
"/market/sell",
[body("itemID").isInt(), body("price").isDecimal()],
validateRequest,
async (req: Request, res: Response) => {
const {itemID, price} = req.body
const tx = await storefrontService.sell(itemID, price)
return res.send({
transactionId: tx,
})
}
)
router.get(
"/market/collection/:account",
async (req: Request, res: Response) => {
const items = await storefrontService.getItems(req.params.account)
return res.send({
items,
})
}
)
router.get(
"/market/collection/:account/:item",
async (req: Request, res: Response) => {
const item = await storefrontService.getItem(
req.params.account,
parseInt(req.params.item)
)
return res.send({
item,
})
}
)
router.get("/market/latest", async (req: Request, res: Response) => {
const latestListings = await storefrontService.findMostRecentSales(
req.query
)
return res.send(latestListings)
})
router.get("/market/:id", async (req: Request, res: Response) => {
const listing = await storefrontService.findListing(req.params.id)
return res.send(listing)
})
return router
}
export default initStorefrontRouter