Skip to content

Commit

Permalink
refactor(page-service): refactor service as a microservice (#363)
Browse files Browse the repository at this point in the history
* 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
wibus-wee authored Sep 24, 2022
1 parent d11dbfc commit c5342ea
Show file tree
Hide file tree
Showing 27 changed files with 1,976 additions and 52 deletions.
6 changes: 6 additions & 0 deletions apps/core/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { AllExceptionsFilter } from '~/shared/common/filters/any-exception.filte
import { RolesGuard } from '~/shared/common/guard/roles.guard';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CategoryModule } from './modules/category/category.module';
import { PageModule } from './modules/page/page.module';
import { PostModule } from './modules/post/post.module';
import { UserModule } from './modules/user/user.module';

@Module({
Expand All @@ -17,6 +20,9 @@ import { UserModule } from './modules/user/user.module';
DatabaseModule,
ConfigModule,
UserModule,
PostModule,
PageModule,
CategoryModule,
],
controllers: [AppController],
providers: [
Expand Down
16 changes: 9 additions & 7 deletions apps/core/src/global/index.global.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-useless-escape */
/* eslint-disable import/order */
import { Logger } from '@nestjs/common';
import { chalk, $ } from 'zx-cjs';
Expand All @@ -15,14 +16,15 @@ import './dayjs.global';
import { isDev } from './env.global';
import { join } from 'path';

function consoleNEXT() {
function consoleMog() {
console.log(`
_ _________ ________
/ | / / ____/ |/ /_ __/
/ |/ / __/ | / / /
/ /| / /___ / | / /
/_/ |_/_____//_/|_|/_/
__ __
| \/ | ___ __ _
| |\/| |/ _ \ / _ \ |
| | | | (_) | (_| |
|_| |_|\___/ \__, |
|___/
`);
Expand Down Expand Up @@ -62,7 +64,7 @@ export function registerGlobal() {

export function register() {
registerGlobal();
consoleNEXT();
consoleMog();
mkdirs();
registerStdLogger();
}
172 changes: 172 additions & 0 deletions apps/core/src/modules/category/category.controller.ts
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));
}),
);
}
}
25 changes: 25 additions & 0 deletions apps/core/src/modules/category/category.module.ts
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 {}
115 changes: 115 additions & 0 deletions apps/core/src/modules/page/page.controller.ts
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));
}),
);
}
}
25 changes: 25 additions & 0 deletions apps/core/src/modules/page/page.module.ts
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 {}
Loading

0 comments on commit c5342ea

Please sign in to comment.