forked from NucleoidAI/Nucleoid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelloworld.ts
More file actions
38 lines (29 loc) · 858 Bytes
/
helloworld.ts
File metadata and controls
38 lines (29 loc) · 858 Bytes
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
import nucleoid from "./";
export class Item {
public name: string;
public barcode: string;
constructor(name: string, barcode: string) {
this.name = name;
this.barcode = barcode;
}
static find(predicate: (item: Item) => boolean): Item | undefined {
const items: Item[] = [];
return items.find(predicate);
}
}
nucleoid.register(Item);
const app = nucleoid.express({});
// 👍 Only needed a business logic and 💖
// "Create an item with given name and barcode,
// but the barcode must be unique"
app.post("/items", (req) => {
const { name, barcode } = req.body;
const existing = Item.find((i) => i.barcode === barcode);
if (existing) {
throw new Error("DUPLICATE_BARCODE");
}
return new Item(name, barcode);
});
app.listen(3000, () => {
console.log("🚀 Server listening on http://localhost:3000");
});