-
Notifications
You must be signed in to change notification settings - Fork 230
/
crud.controller.ts
executable file
·58 lines (48 loc) · 1.52 KB
/
crud.controller.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
import {
Body,
Controller,
Get,
Param,
Post,
Put,
Delete,
NotFoundException,
InternalServerErrorException,
ParseIntPipe,
} from '@nestjs/common';
import type { Sampletable1 } from '#entity/sampledb1';
import { CreateDto, UpdateDto } from '../dto';
import { CrudService } from '../providers';
/**
* route /test/crud/*
*/
@Controller('crud')
export class CrudController {
constructor(private crud: CrudService) {}
@Get(':id') // GET http://localhost:3000/test/crud/:id
public async read(@Param('id', ParseIntPipe) id: number): Promise<Sampletable1> {
const result = await this.crud.read(id);
if (!result) {
throw new NotFoundException('NotFoundData');
}
return result;
}
@Post() // POST http://localhost:3000/test/crud
public async create(@Body() body: CreateDto): Promise<{ id: number }> {
const result = await this.crud.create(body);
if (!result.id) {
throw new InternalServerErrorException('NotCreatedData');
}
return { id: result.id };
}
@Put(':id') // PUT http://localhost:3000/test/crud/:id
public async update(@Param('id', ParseIntPipe) id: number, @Body() body: UpdateDto): Promise<{ success: boolean }> {
const result = await this.crud.update(id, body);
return { success: !!result.affected };
}
@Delete(':id') // DELETE http://localhost:3000/test/crud/:id
public async remove(@Param('id', ParseIntPipe) id: number): Promise<{ success: boolean }> {
const result = await this.crud.remove(id);
return { success: !!result.affected };
}
}