feat: scaffold center and guild backend NestJS skeletons
This commit is contained in:
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),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user