-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
43 lines (36 loc) · 1.17 KB
/
api.js
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
import { Application } from 'https://deno.land/x/abc@v1.0.0-rc10/mod.ts';
import { v4 } from 'https://deno.land/std/uuid/mod.ts';
let clients = [
{ id: '1', firstName: 'bart', lastName: 'zalewski' },
{ id: '2', firstName: 'long', lastName: 'shong' },
{ id: '3', firstName: 'joe', lastName: 'harrison' },
];
const getClients = (ctx) => ctx.json(clients);
const getClient = (ctx) => {
const { id } = ctx.params;
const client = clients.find((c) => c.id === id);
client ? ctx.json(client) : ctx.string('No client with that ID.');
};
const addClient = async (ctx) => {
const { firstName, lastName } = await ctx.body();
const id = v4.generate();
const client = { id, firstName, lastName };
clients.push(client);
return ctx.json(client);
};
const removeClient = (ctx) => {
const { id } = ctx.params;
const client = clients.find((c) => c.id === id);
if (client) {
clients = clients.filter((c) => c.id !== id);
return ctx.json(client);
}
return ctx.string('No client with that ID.');
};
const app = new Application();
app
.get('/clients', getClients)
.get('/clients/:id', getClient)
.post('/clients', addClient)
.delete('/clients/:id', removeClient)
.start({ port: 5000 });