-
Notifications
You must be signed in to change notification settings - Fork 2
/
cat.controller.ts
118 lines (109 loc) · 2.9 KB
/
cat.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
UseInterceptors,
UploadedFile,
Body,
Controller,
Delete,
Get,
Param,
Post,
BadRequestException,
Logger,
UnprocessableEntityException,
BadGatewayException,
Query,
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
AzureStorageFileInterceptor,
UploadedFileMetadata,
AzureStorageService,
} from '@nestjs/azure-storage';
import { AzureTableContinuationToken } from '@nestjs/azure-database';
import { CatService } from './cat.service';
@Controller('cats')
export class CatController {
constructor(
private readonly catService: CatService,
private azureStorage: AzureStorageService,
) {}
@Post('upload')
@UseInterceptors(FileInterceptor('file'))
async UploadedCatUsingService(
@UploadedFile()
file: UploadedFileMetadata,
) {
Logger.log(
`File "${file.originalname}" was uploaded using Azure Service`,
'CatController',
);
try {
const storageUrl = await this.azureStorage.upload(file);
return this.addCat(storageUrl);
} catch (error) {
throw new BadGatewayException(error);
}
}
@Post('upload/interceptor')
@UseInterceptors(AzureStorageFileInterceptor('file'))
uploadCat(
@UploadedFile()
file: UploadedFileMetadata,
) {
Logger.log(
`File "${file.originalname}" was uploaded using Azure Storage Interceptor`,
'CatController',
);
return this.addCat(file.storageUrl);
}
@Get()
getAllCats(
@Query('orderBy')
orderBy: string,
@Query('continuationToken')
continuationToken: string
) {
const azureContinuationToken: AzureTableContinuationToken = continuationToken ? JSON.parse(continuationToken) : undefined;
if (orderBy === 'rating') {
return this.catService.findAllByRating(azureContinuationToken);
}
return this.catService.findAll(azureContinuationToken);
}
@Post(':id/paw')
async incrementRating(
@Body()
data: { rating: number },
@Param('id') id,
) {
const { rating } = data;
if (rating && (rating < 1 || rating > 10)) {
throw new BadRequestException('rating must be a number between 1 and 10');
}
try {
return await this.catService.incrementRating(id, rating || 1);
} catch (error) {
throw new UnprocessableEntityException(error);
}
}
@Delete(':id')
async deleteCat(@Param('id') id) {
try {
return await this.catService.delete(id);
} catch (error) {
throw new UnprocessableEntityException(error);
}
}
private async addCat(storageUrl: string | boolean) {
if (storageUrl !== false) {
const url = storageUrl as string;
try {
Logger.log(`Created new cat with storage URL: ${url}`, 'CatController');
return await this.catService.create(url);
} catch (error) {
throw new UnprocessableEntityException(error);
}
} else {
throw new BadGatewayException();
}
}
}