-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
68 lines (63 loc) · 1.87 KB
/
app.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
import cors from "cors"
import express, { Request, Response } from "express"
import { AppDataSource } from "./DataSource"
import { Todo } from "./Todo"
const app = express()
app.use(express.json())
app.use(cors())
AppDataSource.initialize()
.then(() => {
app.listen(3000, () => {
console.log("Server connected: Port 3000")
const todoStudentRepo = AppDataSource.getRepository(Todo)
app.get("/students", async (req: Request, res: Response) => {
const allTodos = await todoStudentRepo.find()
return res.json({
status: "OK",
data: allTodos,
})
})
app.patch("/students/:id", async (req: Request, res: Response) => {
const isStudentEdit = req.body.edit
const id = parseInt(req.params.id)
const foundStudent = await todoStudentRepo.findOneBy({ id })
let result
if (foundStudent) {
foundStudent.edit = isStudentEdit
result = await todoStudentRepo.save(foundStudent)
}
return res.json({
status: "Ok",
data: result,
})
})
app.delete("/students/:id", async (req: Request, res: Response) => {
const id = parseInt(req.params.id)
const result = await todoStudentRepo.delete({ id })
return res.json({
status: "OK",
data: result,
})
})
app.post("/students", async (req: Request, res: Response) => {
const data: {
newFullName: string
newClass: string
newBirth: string
} = req.body
const newStudent = {
...data,
edit: false,
}
console.log(newStudent)
const result = await todoStudentRepo.save(newStudent)
return res.json({
status: "OK",
data: result,
})
})
})
})
.catch((err) => {
console.log("Error", err)
})