Skip to content

Commit

Permalink
feat(apps): fully friends microservice (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
wibus-wee authored Jan 8, 2023
1 parent fc7bd2a commit 5b9a41e
Show file tree
Hide file tree
Showing 20 changed files with 949 additions and 166 deletions.
2 changes: 2 additions & 0 deletions apps/core/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { PageModule } from './modules/page/page.module';
import { PostModule } from './modules/post/post.module';
import { UserModule } from './modules/user/user.module';
import { CommentsModule } from './modules/comments/comments.module';
import { FriendsModule } from './modules/friends/friends.module';

@Module({
imports: [
Expand All @@ -30,6 +31,7 @@ import { CommentsModule } from './modules/comments/comments.module';
CategoryModule,
AggregateModule,
CommentsModule,
FriendsModule,
],
controllers: [AppController],
providers: [
Expand Down
78 changes: 0 additions & 78 deletions apps/core/src/modules/friends/friends.basic.model.ts

This file was deleted.

46 changes: 0 additions & 46 deletions apps/core/src/modules/friends/friends.basic.service.ts

This file was deleted.

113 changes: 93 additions & 20 deletions apps/core/src/modules/friends/friends.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,133 @@ import {
Controller,
Delete,
Get,
Inject,
Param,
Patch,
Post,
Put,
Query,
} from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { ApiOperation } from '@nestjs/swagger';
import {
FriendsModel,
FriendStatus,
} from '~/apps/friends-service/src/friends.model';
import { Auth } from '~/shared/common/decorator/auth.decorator';
import { ApiName } from '~/shared/common/decorator/openapi.decorator';
import { IsMaster } from '~/shared/common/decorator/role.decorator';
import { FriendsBasicModel, FriendStatus } from './friends.basic.model';
import { FriendsBasicService } from './friends.basic.service';
import { FriendsEvents } from '~/shared/constants/event.constant';
import { ServicesEnum } from '~/shared/constants/services.constant';
import { transportReqToMicroservice } from '~/shared/microservice.transporter';

@Controller('friends')
@ApiName
export class FriendsController {
constructor(private readonly friendsBasicService: FriendsBasicService) {}
constructor(
@Inject(ServicesEnum.friends) private readonly friends: ClientProxy,
) {}

@Get('/')
@ApiOperation({ summary: '获取所有友链' })
async getAllFriends(@IsMaster() isMaster: boolean) {
return await this.friendsBasicService.getAllFriends(isMaster);
@ApiOperation({ summary: '获取所有友链(By group)' })
async getAllFriends(@Query('group') group?: string | undefined) {
console.log('group', group);
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendsGetList,
group ? group : {},
);
}

@Get('/all')
@ApiOperation({ summary: '获取所有友链(Master)' })
@Auth()
async getAllFriendsMaster(@Query('status') status: FriendStatus) {
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendsGetAllByMaster,
status,
);
}

@Get('/:id')
@ApiOperation({ summary: '获取友链详情' })
async getFriend(@Param('id') id: string) {
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendGet,
id,
);
}

@Post('/')
@ApiOperation({ summary: '创建友链' })
async createFriend(
@Body() friend: FriendsBasicModel,
@Body() data: FriendsModel,
@IsMaster() isMaster: boolean,
) {
return await this.friendsBasicService.createFriend(friend, isMaster);
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendCreate,
{ data, isMaster },
);
}

@Put('/:id')
@ApiOperation({ summary: '更新友链' })
async updateFriend(
@Param('id') id: string,
@Body() friend: FriendsBasicModel,
@Body() friend: FriendsModel & { token?: string },
@IsMaster() isMaster: boolean,
) {
return await this.friendsBasicService.updateFriend(id, friend, isMaster);
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendUpdateByMasterOrToken,
{ id, data: friend, isMaster, token: friend.token },
);
}

@Patch('/:id')
@ApiOperation({ summary: '更新友链状态' })
async updateFriendStatus(
@Delete('/:id')
@ApiOperation({ summary: '删除友链' })
async deleteFriend(
@Param('id') id: string,
@Query('status') status: FriendStatus,
@Body('token') token: string,
@IsMaster() isMaster: boolean,
) {
return await this.friendsBasicService.updateFriendStatus(id, status);
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendDeleteByMasterOrToken,
{ id, isMaster, token },
);
}

@Delete('/:id')
@ApiOperation({ summary: '删除友链' })
async deleteFriend(@Param('id') id: string) {
return await this.friendsBasicService.deleteFriend(id);
@Get('/alive')
@ApiOperation({ summary: '检查友链是否存活' })
async checkAliver() {
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendsCheckAlive,
{},
);
}

@Get('/feeds')
@ApiOperation({ summary: '获取友链 Feed 更新' })
async getFeed() {
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendsAnalyseFeed,
{},
10000,
);
}

@Get('/:id/check')
@ApiOperation({ summary: '自动检查是否互链' })
async autoCheck(@Param('id') id: string) {
return transportReqToMicroservice(
this.friends,
FriendsEvents.FriendAnalyseAutoCheck,
id,
);
}
}
24 changes: 20 additions & 4 deletions apps/core/src/modules/friends/friends.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { Module } from '@nestjs/common';
import { FriendsBasicService } from './friends.basic.service';
import { ClientsModule, Transport } from '@nestjs/microservices';
import {
ServicesEnum,
ServicePorts,
} from '~/shared/constants/services.constant';
import { getEnv } from '~/shared/utils/rag-env';
import { FriendsController } from './friends.controller';

@Module({
imports: [],
imports: [
ClientsModule.register([
{
name: ServicesEnum.friends,
transport: Transport.TCP,
options: {
port: getEnv(ServicesEnum.friends)?.port || ServicePorts.friends,
host: getEnv(ServicesEnum.friends)?.host || undefined,
},
},
]),
],
controllers: [FriendsController],
providers: [FriendsBasicService],
exports: [FriendsBasicService],
providers: [],
exports: [],
})
export class FriendsModule {}
71 changes: 71 additions & 0 deletions apps/friends-service/src/friends-service.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { FriendsEvents } from '~/shared/constants/event.constant';
import { FriendsService } from './friends-service.service';
import { FriendsModel, FriendStatus } from './friends.model';

@Controller()
export class FriendsServiceController {
constructor(private readonly friendsService: FriendsService) {}

@MessagePattern({ cmd: FriendsEvents.FriendsGetList })
async getFriendsList(group: string | undefined | Object) {
const theGroup = group ? (group === Object ? undefined : group) : undefined;
return await this.friendsService.getList(String(theGroup));
}

@MessagePattern({ cmd: FriendsEvents.FriendsGetAllByMaster })
async getAllFriends(status: FriendStatus) {
return await this.friendsService.getAllByMaster(status);
}

@MessagePattern({ cmd: FriendsEvents.FriendGet })
async getFriend(id: string) {
return await this.friendsService.get(id);
}

@MessagePattern({ cmd: FriendsEvents.FriendCreate })
async createFriend(data: { data: FriendsModel; isMaster: boolean }) {
return await this.friendsService.create(data);
}

@MessagePattern({ cmd: FriendsEvents.FriendUpdateByMasterOrToken })
async updateFriend(input: {
id: string;
data: FriendsModel;
isMaster: boolean;
token?: string;
}) {
return await this.friendsService.update(
input.id,
input.data,
input.isMaster,
input.token,
);
}

@MessagePattern({ cmd: FriendsEvents.FriendDeleteByMasterOrToken })
async deleteFriend(input: { id: string; isMaster: boolean; token?: string }) {
return await this.friendsService.delete(
input.id,
input.isMaster,
input.token,
);
}

@MessagePattern({ cmd: FriendsEvents.FriendsAnalyseFeed })
async analyseFeed() {
await this.friendsService.analyseFeed();
return true;
}

@MessagePattern({ cmd: FriendsEvents.FriendAnalyseAutoCheck })
async analyseAutoCheck(id: string) {
return await this.friendsService.analyseAutoCheck(id);
}

@MessagePattern({ cmd: FriendsEvents.FriendsCheckAlive })
async checkAlive() {
return await this.friendsService.checkAliver();
}
}
Loading

0 comments on commit 5b9a41e

Please sign in to comment.