-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.controller.ts
91 lines (83 loc) · 2.12 KB
/
media.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {
Controller,
Get,
Query,
Post,
Put,
Delete,
Param,
UseInterceptors,
UploadedFiles,
Logger,
UploadedFile,
Body
} from "@nestjs/common";
import { Tags } from "nest-swagger";
import { MediaService } from "./media.service";
import {
CreateMediaDto,
MediaRes,
EditMediaDto,
UploadRes,
UploadMultipleRes,
MediaFile
} from "../dto";
import { NullableParseIntPipe, ResultList, KeyValue } from "nestx-common";
import { Media } from "../interfaces";
import { FilesInterceptor, FileInterceptor } from "@nestjs/platform-express";
@Tags("cms")
@Controller("media")
export class MediaController {
constructor(private readonly service: MediaService) {}
@Get("search")
async search(
@Query("keyword") keyword?: string,
@Query("value") value?: string
): Promise<KeyValue[]> {
return this.service.search(keyword, value);
}
@Post("upload")
@UseInterceptors(FileInterceptor("file"))
async uploadFile(@UploadedFile() file: MediaFile): Promise<UploadRes> {
return {
ok: true,
file: file.path
};
}
@Post("uploads")
@UseInterceptors(FilesInterceptor("files"))
async uploadFiles(
@UploadedFiles() files?: MediaFile[]
): Promise<UploadMultipleRes> {
const fileNames = (files || []).map(item => item.path);
return {
ok: true,
files: fileNames
};
}
@Post()
async create(@Body() entry: CreateMediaDto): Promise<MediaRes> {
return this.service.create(entry);
}
@Put()
async update(@Body() entry: EditMediaDto): Promise<MediaRes> {
return this.service.update(entry);
}
@Get("query")
async query(
@Query("keyword") keyword?: string,
@Query("page", new NullableParseIntPipe()) page: number = 1,
@Query("size", new NullableParseIntPipe()) size: number = 10,
@Query("sort") sort?: string
): Promise<ResultList<MediaRes>> {
return this.service.querySearch(keyword, page, size, sort);
}
@Delete(":id")
async remove(@Param("id") id: string): Promise<boolean> {
return this.service.remove(id);
}
@Get(":id")
async findOne(@Param("id") id: string): Promise<Media> {
return this.service.findById(id);
}
}