Task: Updated the delete route and controller
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Sambo Chea 2021-09-14 10:51:24 +07:00
parent 5120752ecb
commit a00c5b0650
2 changed files with 37 additions and 2 deletions

View File

@ -41,7 +41,7 @@ curl http://localhost:3000/info
```typescript ```typescript
import { Request, Response } from "express" import { Request, Response } from "express"
import Controller from "../decorators/controller.decorator" import Controller from "../decorators/controller.decorator"
import { Get, Post } from "../decorators/handlers.decorator" import { Get, Post, Delete } from "../decorators/handlers.decorator"
const data: any[] = [] const data: any[] = []
@ -70,6 +70,14 @@ export default class HomeController {
body: body, body: body,
}) })
} }
@Delete("/:id")
public get(req: Request, res: Response) {
const id = req.params.id
res.json({
id: id,
})
}
} }
``` ```

View File

@ -1,6 +1,6 @@
import { Request, Response } from "express" import { Request, Response } from "express"
import Controller from "../decorators/controller.decorator" import Controller from "../decorators/controller.decorator"
import { Post, Get } from "../decorators/handlers.decorator" import { Post, Get, Delete } from "../decorators/handlers.decorator"
const persons: Array<any> = [ const persons: Array<any> = [
{ {
@ -33,4 +33,31 @@ export default class PersonController {
body: person, body: person,
}) })
} }
@Delete("/:id")
public deletePerson(req: Request, res: Response) {
const id = req.params.id
if (id == null) {
return res.status(400).json({
status: 400,
message: "Id is required",
})
}
const person = persons.find((person) => person.id === Number(id))
if (person == null) {
return res.status(404).json({
status: 404,
message: "Person not found",
})
}
persons.splice(persons.indexOf(person), 1)
res.json({
message: "Person deleted successfully",
body: person,
})
}
} }