feat: scaffold center and guild backend NestJS skeletons
This commit is contained in:
11
Fabric.Backend.Guild/src/app.module.ts
Normal file
11
Fabric.Backend.Guild/src/app.module.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { HealthController } from './common/health.controller';
|
||||
import { GuildsModule } from './guilds/guilds.module';
|
||||
import { ChannelsModule } from './channels/channels.module';
|
||||
import { MessagingModule } from './messaging/messaging.module';
|
||||
|
||||
@Module({
|
||||
imports: [GuildsModule, ChannelsModule, MessagingModule],
|
||||
controllers: [HealthController],
|
||||
})
|
||||
export class AppModule {}
|
||||
9
Fabric.Backend.Guild/src/channels/channels.controller.ts
Normal file
9
Fabric.Backend.Guild/src/channels/channels.controller.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Body, Controller, Post } from '@nestjs/common';
|
||||
|
||||
@Controller('channels')
|
||||
export class ChannelsController {
|
||||
@Post()
|
||||
create(@Body() body: Record<string, unknown>) {
|
||||
return { status: 'todo', action: 'create-channel', received: body };
|
||||
}
|
||||
}
|
||||
7
Fabric.Backend.Guild/src/channels/channels.module.ts
Normal file
7
Fabric.Backend.Guild/src/channels/channels.module.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ChannelsController } from './channels.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [ChannelsController],
|
||||
})
|
||||
export class ChannelsModule {}
|
||||
9
Fabric.Backend.Guild/src/common/health.controller.ts
Normal file
9
Fabric.Backend.Guild/src/common/health.controller.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
|
||||
@Controller('healthz')
|
||||
export class HealthController {
|
||||
@Get()
|
||||
get() {
|
||||
return { ok: true, service: 'guild' };
|
||||
}
|
||||
}
|
||||
9
Fabric.Backend.Guild/src/guilds/guilds.controller.ts
Normal file
9
Fabric.Backend.Guild/src/guilds/guilds.controller.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Body, Controller, Post } from '@nestjs/common';
|
||||
|
||||
@Controller('guilds')
|
||||
export class GuildsController {
|
||||
@Post()
|
||||
create(@Body() body: Record<string, unknown>) {
|
||||
return { status: 'todo', action: 'create-guild', received: body };
|
||||
}
|
||||
}
|
||||
7
Fabric.Backend.Guild/src/guilds/guilds.module.ts
Normal file
7
Fabric.Backend.Guild/src/guilds/guilds.module.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { GuildsController } from './guilds.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [GuildsController],
|
||||
})
|
||||
export class GuildsModule {}
|
||||
13
Fabric.Backend.Guild/src/main.ts
Normal file
13
Fabric.Backend.Guild/src/main.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import 'reflect-metadata';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { AppModule } from './app.module';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create(AppModule);
|
||||
app.setGlobalPrefix('api');
|
||||
const port = process.env.PORT ? Number(process.env.PORT) : 7002;
|
||||
await app.listen(port);
|
||||
console.log(`Fabric.Backend.Guild listening on :${port}`);
|
||||
}
|
||||
|
||||
void bootstrap();
|
||||
62
Fabric.Backend.Guild/src/messaging/messaging.controller.ts
Normal file
62
Fabric.Backend.Guild/src/messaging/messaging.controller.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
|
||||
|
||||
type Message = {
|
||||
messageId: string;
|
||||
seq: number;
|
||||
content: string;
|
||||
};
|
||||
|
||||
@Controller('channels/:id/messages')
|
||||
export class MessagingController {
|
||||
private seqByChannel = new Map<string, number>();
|
||||
private messagesByChannel = new Map<string, Message[]>();
|
||||
|
||||
@Post()
|
||||
create(@Param('id') channelId: string, @Body() body: { content?: string; messageId?: string }) {
|
||||
const next = (this.seqByChannel.get(channelId) ?? 0) + 1;
|
||||
this.seqByChannel.set(channelId, next);
|
||||
|
||||
const message: Message = {
|
||||
messageId: body.messageId ?? `m-${channelId}-${next}`,
|
||||
seq: next,
|
||||
content: body.content ?? '',
|
||||
};
|
||||
|
||||
const arr = this.messagesByChannel.get(channelId) ?? [];
|
||||
arr.push(message);
|
||||
this.messagesByChannel.set(channelId, arr);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
@Patch(':messageId')
|
||||
edit(@Param('id') channelId: string, @Param('messageId') messageId: string, @Body() body: { content?: string }) {
|
||||
const arr = this.messagesByChannel.get(channelId) ?? [];
|
||||
const item = arr.find((m) => m.messageId === messageId);
|
||||
if (!item) return { status: 'not_found' };
|
||||
item.content = body.content ?? item.content;
|
||||
return item;
|
||||
}
|
||||
|
||||
@Delete(':messageId')
|
||||
remove(@Param('id') channelId: string, @Param('messageId') messageId: string) {
|
||||
const arr = this.messagesByChannel.get(channelId) ?? [];
|
||||
const next = arr.filter((m) => m.messageId !== messageId);
|
||||
this.messagesByChannel.set(channelId, next);
|
||||
return { status: 'deleted', messageId };
|
||||
}
|
||||
|
||||
@Get()
|
||||
listBySeq(
|
||||
@Param('id') channelId: string,
|
||||
@Query('seq_from') seqFrom?: string,
|
||||
@Query('seq_to') seqTo?: string,
|
||||
) {
|
||||
const from = seqFrom ? Number(seqFrom) : 1;
|
||||
const to = seqTo ? Number(seqTo) : Number.MAX_SAFE_INTEGER;
|
||||
const arr = this.messagesByChannel.get(channelId) ?? [];
|
||||
return {
|
||||
items: arr.filter((m) => m.seq >= from && m.seq <= to),
|
||||
};
|
||||
}
|
||||
}
|
||||
7
Fabric.Backend.Guild/src/messaging/messaging.module.ts
Normal file
7
Fabric.Backend.Guild/src/messaging/messaging.module.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { MessagingController } from './messaging.controller';
|
||||
|
||||
@Module({
|
||||
controllers: [MessagingController],
|
||||
})
|
||||
export class MessagingModule {}
|
||||
Reference in New Issue
Block a user