-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(page-service): refactor service as a microservice (#363)
* feat(page-service): init nestjs microservice app * feat(page-service): import category model & dto * perf: start page-service with npm-script * feat(page-service): category service * perf: import category service and export it * feat: post model and dto define * feat: basic post service & aggregate method * perf: throw exception with rpc exception method * perf: get post by category and slug * fix: get transport data from an object * feat: optimize method into a detail name * feat: make post controller public * perf: add a custom fields * feat: page model define * feat: PageEvents defined * perf: add more page event * perf: get data event service * feat: post service public controller * perf: multi query search service * feat: merge tags and category * feat: delete category and create default * feat: make category service public * feat: register core module * feat: category public gateway api * feat: page public gateway api * fix: function params order * feat: post public gateway api * chore: import controllers * fix: change console Mog
- Loading branch information
Showing
27 changed files
with
1,976 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* @FilePath: /nx-core/apps/core/src/modules/category/category.controller.ts | ||
* @author: Wibus | ||
* @Date: 2022-09-24 15:53:29 | ||
* @LastEditors: Wibus | ||
* @LastEditTime: 2022-09-24 16:00:01 | ||
* Coding With IU | ||
*/ | ||
|
||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpCode, | ||
HttpException, | ||
Inject, | ||
Param, | ||
Patch, | ||
Post, | ||
Put, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { ApiBody, ApiOperation, ApiParam, ApiQuery } from '@nestjs/swagger'; | ||
import { timeout, catchError, throwError } from 'rxjs'; | ||
import { | ||
MultiCategoriesQueryDto, | ||
MultiQueryTagAndCategoryDto, | ||
SlugorIdDto, | ||
} from '~/apps/page-service/src/dto/category.dto'; | ||
import { | ||
CategoryModel, | ||
CategoryType, | ||
PartialCategoryModel, | ||
} from '~/apps/page-service/src/model/category.model'; | ||
import { Auth } from '~/shared/common/decorator/auth.decorator'; | ||
import { ApiName } from '~/shared/common/decorator/openapi.decorator'; | ||
import { CategoryEvents } from '~/shared/constants/event.constant'; | ||
import { ServicesEnum } from '~/shared/constants/services.constant'; | ||
import { MongoIdDto } from '~/shared/dto/id.dto'; | ||
|
||
@Controller('category') | ||
@ApiName | ||
export class CategoryController { | ||
constructor( | ||
@Inject(ServicesEnum.category) private readonly category: ClientProxy, | ||
) {} | ||
|
||
@Get('/') | ||
@ApiOperation({ summary: '多分类查询、分类列表' }) | ||
async getCategories(@Query() query: MultiCategoriesQueryDto) { | ||
return this.category | ||
.send({ cmd: CategoryEvents.CategoryGetAll }, query) | ||
.pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Get('/:query') | ||
@ApiOperation({ summary: '根据分类id或者标签名查询分类' }) | ||
@ApiParam({ | ||
name: 'query', | ||
type: 'string', | ||
required: true, | ||
description: | ||
'如果这个是标签,则query为标签名,如果是分类,则query为分类id或分类名', | ||
}) | ||
@ApiQuery({ | ||
// 查询参数 | ||
name: 'tag', // 参数名 | ||
type: 'boolean', // 参数类型 | ||
description: '选择分类 或 标签云查询', | ||
enum: ['true', 'false'], // 可选值 | ||
required: false, // 是否必填 | ||
}) | ||
async getCategoryByCategoryIdOrTag( | ||
@Param() { query }: SlugorIdDto, | ||
@Query() { tag }: MultiQueryTagAndCategoryDto, // 如果这个是标签,则tag为true,如果是分类,则tag为分类id | ||
) { | ||
return this.category | ||
.send({ cmd: CategoryEvents.CategoryGet }, { query, tag }) | ||
.pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Post('/') | ||
@ApiOperation({ summary: '创建分类' }) | ||
@Auth() | ||
@ApiBody({ type: CategoryModel }) | ||
async createCategory(@Body() query: CategoryModel) { | ||
return this.category | ||
.send({ cmd: CategoryEvents.CategoryCreate }, query) | ||
.pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Post('/merge') | ||
@ApiOperation({ summary: '合并分类或标签 (Beta)' }) | ||
@Auth() | ||
async merge(@Body() body: { type: CategoryType; from: string; to: string }) { | ||
return this.category.send({ cmd: CategoryEvents.CategoryMerge }, body).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Put('/:id') | ||
@ApiOperation({ summary: '更新分类' }) | ||
@Auth() | ||
@ApiParam({ name: 'id', description: '分类id' }) | ||
@ApiBody({ description: '分类信息', type: CategoryModel }) | ||
async update( | ||
@Param() { id }: MongoIdDto, | ||
@Body() { type, slug, name }: CategoryModel, | ||
) { | ||
const send = { | ||
_id: id, | ||
_data: { type, slug, name }, | ||
}; | ||
return this.category.send({ cmd: CategoryEvents.CategoryPatch }, send).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Patch('/:id') | ||
@ApiOperation({ summary: '更新分类' }) | ||
@Auth() | ||
@HttpCode(204) | ||
async patch(@Param() params: MongoIdDto, @Body() body: PartialCategoryModel) { | ||
return this.category | ||
.send( | ||
{ cmd: CategoryEvents.CategoryPatch }, | ||
{ _id: params.id, _data: body }, | ||
) | ||
.pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Delete('/:id') | ||
@ApiOperation({ summary: '删除分类' }) | ||
@Auth() | ||
@ApiParam({ name: 'id', description: '分类id' }) | ||
async deleteCategory(@Param() { id }: MongoIdDto) { | ||
return this.category.send({ cmd: CategoryEvents.CategoryDelete }, id).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* @FilePath: /nx-core/apps/core/src/modules/category/category.module.ts | ||
* @author: Wibus | ||
* @Date: 2022-09-24 15:42:05 | ||
* @LastEditors: Wibus | ||
* @LastEditTime: 2022-09-24 16:15:05 | ||
* Coding With IU | ||
*/ | ||
import { Module } from '@nestjs/common'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { ServicesEnum } from '~/shared/constants/services.constant'; | ||
import { CategoryController } from './category.controller'; | ||
|
||
@Module({ | ||
imports: [ | ||
ClientsModule.register([ | ||
{ | ||
name: ServicesEnum.category, | ||
transport: Transport.TCP, | ||
}, | ||
]), | ||
], | ||
controllers: [CategoryController], | ||
}) | ||
export class CategoryModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* @FilePath: /nx-core/apps/core/src/modules/page/page.controller.ts | ||
* @author: Wibus | ||
* @Date: 2022-09-24 16:01:23 | ||
* @LastEditors: Wibus | ||
* @LastEditTime: 2022-09-24 16:06:08 | ||
* Coding With IU | ||
*/ | ||
|
||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpException, | ||
Inject, | ||
Param, | ||
Patch, | ||
Post, | ||
Put, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { ApiOperation } from '@nestjs/swagger'; | ||
import { timeout, catchError, throwError } from 'rxjs'; | ||
import { PageModel } from '~/apps/page-service/src/model/page.model'; | ||
import { Auth } from '~/shared/common/decorator/auth.decorator'; | ||
import { Paginator } from '~/shared/common/decorator/http.decorator'; | ||
import { ApiName } from '~/shared/common/decorator/openapi.decorator'; | ||
import { PageEvents } from '~/shared/constants/event.constant'; | ||
import { ServicesEnum } from '~/shared/constants/services.constant'; | ||
import { MongoIdDto } from '~/shared/dto/id.dto'; | ||
import { PagerDto } from '~/shared/dto/pager.dto'; | ||
|
||
@Controller('page') | ||
@ApiName | ||
export class PageController { | ||
constructor(@Inject(ServicesEnum.page) private readonly page: ClientProxy) {} | ||
|
||
@Get('/') | ||
@Paginator | ||
@ApiOperation({ summary: '获取页面列表' }) | ||
async getPagesSummary(@Query() query: PagerDto) { | ||
return this.page.send({ cmd: PageEvents.PageGetAll }, query).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Get('/:id') | ||
@ApiOperation({ summary: '通过id获取页面详情' }) | ||
@Auth() | ||
async getPage(@Param() params) { | ||
return this.page | ||
.send({ cmd: PageEvents.PageGetByIdWithMaster }, params.id) | ||
.pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Get('/slug/:slug') | ||
@ApiOperation({ summary: '使用slug获取页面详情' }) | ||
async getPageBySlug(@Param('slug') slug: string) { | ||
return this.page.send({ cmd: PageEvents.PageGet }, slug).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Post('/create') | ||
@ApiOperation({ summary: '创建页面' }) | ||
@Auth() | ||
async create(@Body() body: PageModel) { | ||
return this.page.send({ cmd: PageEvents.PageCreate }, body).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Put('/:id') | ||
@Patch('/:id') | ||
@ApiOperation({ summary: '更新页面' }) | ||
@Auth() | ||
async modify(@Body() body: PageModel, @Param() params: MongoIdDto) { | ||
return this.page | ||
.send({ cmd: PageEvents.PagePatch }, { id: params.id, body }) | ||
.pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
|
||
@Delete('/:id') | ||
@ApiOperation({ summary: '删除页面' }) | ||
@Auth() | ||
async deletePage(@Param() params: MongoIdDto) { | ||
return this.page.send({ cmd: PageEvents.PageDelete }, params.id).pipe( | ||
timeout(1000), | ||
catchError((err) => { | ||
return throwError(() => new HttpException(err.message, err.status)); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* @FilePath: /nx-core/apps/core/src/modules/page/page.module.ts | ||
* @author: Wibus | ||
* @Date: 2022-09-24 15:41:58 | ||
* @LastEditors: Wibus | ||
* @LastEditTime: 2022-09-24 15:41:58 | ||
* Coding With IU | ||
*/ | ||
import { Module } from '@nestjs/common'; | ||
import { ClientsModule, Transport } from '@nestjs/microservices'; | ||
import { ServicesEnum } from '~/shared/constants/services.constant'; | ||
import { PageController } from './page.controller'; | ||
|
||
@Module({ | ||
imports: [ | ||
ClientsModule.register([ | ||
{ | ||
name: ServicesEnum.page, | ||
transport: Transport.TCP, | ||
}, | ||
]), | ||
], | ||
controllers: [PageController], | ||
}) | ||
export class PageModule {} |
Oops, something went wrong.