Skip to content

Commit 9fb0547

Browse files
committed
Create API for creating person and generating id
1 parent da35da5 commit 9fb0547

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

controllers/controllers.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { v4 } from "https://deno.land/std/uuid/mod.ts";
2+
13
import Person from "../models/person.model.ts";
24

35
const people: Person[] = [
@@ -11,12 +13,28 @@ export const getPeople = ({ response }: { response: any }) => {
1113
};
1214
};
1315

14-
export const addPerson = ({ response }: { response: any }) => {
15-
response.status = 200;
16-
response.body = {
17-
"success": true,
18-
"data": people,
19-
};
16+
export const addPerson = async (
17+
{ request, response }: { request: any; response: any },
18+
) => {
19+
const body = await request.body();
20+
21+
if (!request.hasBody) {
22+
response.status = 400;
23+
response.body = {
24+
"success": false,
25+
"msg": "No data",
26+
};
27+
} else {
28+
const person: Person = body.value;
29+
person.id = v4.generate();
30+
people.push(person);
31+
32+
response.status = 201;
33+
response.body = {
34+
"success": true,
35+
"data": person,
36+
};
37+
}
2038
};
2139

2240
export const getPerson = (

0 commit comments

Comments
 (0)