133 lines
3.9 KiB
TypeScript
133 lines
3.9 KiB
TypeScript
import {
|
|
Controller,
|
|
UseInterceptors,
|
|
ClassSerializerInterceptor,
|
|
Inject,
|
|
Get,
|
|
Post,
|
|
Body,
|
|
UploadedFiles,
|
|
Patch,
|
|
Param,
|
|
ParseIntPipe,
|
|
} from '@nestjs/common';
|
|
import { ClientProxy } from '@nestjs/microservices';
|
|
import { ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';
|
|
import { User } from '../../decorators/user.decorator';
|
|
import { UserInfo } from '@freeblox/shared';
|
|
import { CreateContentDto } from './dtos/create-content.dto';
|
|
import { AnyFilesInterceptor } from '@nestjs/platform-express';
|
|
import { CreateContentRevisionDto } from './dtos/create-content-revision.dto';
|
|
import { ContentResponseDto } from './dtos/content-response.dto';
|
|
import { ContentAssetType } from 'apps/catalog/src/enums/content-asset-type.enum';
|
|
import { lastValueFrom } from 'rxjs';
|
|
import { CategoryResponseDto } from './dtos/category-response.dto';
|
|
import { ContentAssetDto } from './dtos/content-asset.dto';
|
|
import { RequirePrivileges } from '../../decorators/require-privileges.decorator';
|
|
|
|
@Controller({
|
|
version: '1',
|
|
path: 'catalog',
|
|
})
|
|
@ApiTags('Catalog')
|
|
@UseInterceptors(ClassSerializerInterceptor)
|
|
export class CatalogController {
|
|
constructor(@Inject('catalog') private catalog: ClientProxy) {}
|
|
|
|
@Get('categories')
|
|
@ApiOkResponse({ type: CategoryResponseDto, isArray: true })
|
|
@ApiOperation({ summary: 'Get catalog categories' })
|
|
async getCatalogCategories() {
|
|
return this.catalog.send('catalog.items.categories', {});
|
|
}
|
|
|
|
@Post('content')
|
|
@ApiOperation({ summary: 'Create new content' })
|
|
@ApiOkResponse({ type: ContentResponseDto })
|
|
@RequirePrivileges('create:*', 'contentedit')
|
|
@UseInterceptors(AnyFilesInterceptor())
|
|
async createContent(
|
|
@User() user: UserInfo,
|
|
@Body() body: CreateContentDto,
|
|
@UploadedFiles() files: Express.Multer.File[],
|
|
) {
|
|
return this.catalog.send('catalog.items.create', { body, user, files });
|
|
}
|
|
|
|
@Get('content/:id')
|
|
@ApiOperation({ summary: 'Get content details' })
|
|
@ApiOkResponse({ type: ContentResponseDto })
|
|
async getContent(
|
|
@Param('id', new ParseIntPipe()) id: number,
|
|
@User() user: UserInfo,
|
|
) {
|
|
return this.catalog.send('catalog.items.byId', { id, user });
|
|
}
|
|
|
|
@Get('content/:id/thumbnail')
|
|
@ApiOperation({ summary: 'Get content thumbnail ID' })
|
|
@ApiOkResponse({ type: ContentAssetDto })
|
|
async getContentThumbnail(
|
|
@Param('id', new ParseIntPipe()) id: number,
|
|
@User() user: UserInfo,
|
|
) {
|
|
const [thumbnail] = await lastValueFrom(
|
|
this.catalog.send('catalog.assets.byId', {
|
|
id,
|
|
user,
|
|
type: [ContentAssetType.IMAGE],
|
|
typeName: ['thumbnail'],
|
|
}),
|
|
);
|
|
return thumbnail;
|
|
}
|
|
|
|
@Patch('content/:id')
|
|
@ApiOperation({ summary: 'Update content details' })
|
|
@ApiOkResponse({ type: ContentResponseDto })
|
|
@RequirePrivileges('create:*', 'contentedit')
|
|
async updateContent(
|
|
@Param('id', new ParseIntPipe()) id: number,
|
|
@User() user: UserInfo,
|
|
@Body() body: CreateContentDto,
|
|
) {
|
|
return this.catalog.send('catalog.items.update', { id, body, user });
|
|
}
|
|
|
|
@Post('content/:id/revision')
|
|
@ApiOperation({
|
|
summary: 'Create a new revision (upload new content for item)',
|
|
})
|
|
@RequirePrivileges('create:*', 'contentedit')
|
|
@UseInterceptors(AnyFilesInterceptor())
|
|
async createContentRevision(
|
|
@Param('id', new ParseIntPipe()) id: number,
|
|
@User() user: UserInfo,
|
|
@Body() body: CreateContentRevisionDto,
|
|
@UploadedFiles() files: Express.Multer.File[],
|
|
) {
|
|
return this.catalog.send('catalog.items.createRevision', {
|
|
id,
|
|
body,
|
|
user,
|
|
files,
|
|
});
|
|
}
|
|
|
|
@Patch('content/:id/publish')
|
|
@ApiOkResponse({ type: ContentResponseDto })
|
|
@ApiOperation({
|
|
summary: 'Publish content',
|
|
})
|
|
@RequirePrivileges('create:*', 'contentedit')
|
|
async publishContent(
|
|
@Param('id', new ParseIntPipe()) id: number,
|
|
@User() user: UserInfo,
|
|
) {
|
|
return this.catalog.send('catalog.items.publish', {
|
|
id,
|
|
user,
|
|
});
|
|
}
|
|
}
|