Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
63 lines (55 loc) · 1.46 KB
/
Copy pathserver.ts
File metadata and controls
63 lines (55 loc) · 1.46 KB
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
import express, { Request, Response } from "express";
import { PrismaClient } from "@prisma/client";
const app = express();
const prisma = new PrismaClient();
// get all joke
//eg: http://127.0.0.1:3000/
// http://127.0.0.1:3000/?joke_id=ckph6po9l0000hse5sdrtby89
app.get("/", async (req: Request, res: Response) => {
const jokes = await prisma.joke.findMany({
include: { creator: true },
});
res.json({
jokes: jokes,
});
});
// create a joke
app.post("/", async (req: Request, res: Response) => {
// use the below when we call by api fetch
// const text = req.body.text;
// const userId = req.body.userId;
const joke = await prisma.joke.create({
data: {
text: "Joke number to be deleted",
userId: "ckph6oqz700007ge5ia5d2klp",
},
});
res.json({ joke });
});
// get a single joke
//eg: http://127.0.0.1:3000/ckph6po9l0000hse5sdrtby89
app.get("/:joke_id", async (req: Request, res: Response) => {
const { joke_id } = req.params;
const joke = await prisma.joke.findFirst({
where: {
id: joke_id,
},
include: { creator: true },
});
res.json(joke);
});
// delete a single joke
app.delete("/:joke_id", async (req: Request, res: Response) => {
const { joke_id } = req.params;
const joke = await prisma.joke.delete({
where: {
id: joke_id,
},
});
res.json({
message: `joke with id=${joke.id} is deleted!`,
});
});
app.listen(3000, () => {
console.log(`Server running at 127.0.0.1`);
});