feat(guild): add guild and channel list APIs for frontend browser

This commit is contained in:
nav
2026-05-12 15:25:26 +00:00
parent 271e712804
commit 9d2a330f69
6 changed files with 91 additions and 4 deletions

View File

@@ -1,9 +1,18 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Get, Post, Query } from '@nestjs/common';
import { ChannelsService } from './channels.service';
@Controller('channels')
export class ChannelsController {
constructor(private readonly channelsService: ChannelsService) {}
@Get()
list(@Query('guildId') guildId?: string) {
if (!guildId) return [];
return this.channelsService.listByGuild(guildId);
}
@Post()
create(@Body() body: Record<string, unknown>) {
return { status: 'todo', action: 'create-channel', received: body };
return this.channelsService.create(body);
}
}

View File

@@ -1,7 +1,13 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ChannelsController } from './channels.controller';
import { Channel } from '../entities/channel.entity';
import { ChannelsService } from './channels.service';
@Module({
imports: [TypeOrmModule.forFeature([Channel])],
controllers: [ChannelsController],
providers: [ChannelsService],
exports: [ChannelsService],
})
export class ChannelsModule {}

View File

@@ -0,0 +1,31 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Channel } from '../entities/channel.entity';
@Injectable()
export class ChannelsService {
constructor(
@InjectRepository(Channel)
private readonly channelRepo: Repository<Channel>,
) {}
listByGuild(guildId: string) {
return this.channelRepo.find({
where: { guildId },
order: { createdAt: 'ASC' },
take: 200,
});
}
create(input: Partial<Channel>) {
const channel = this.channelRepo.create({
guildId: String(input.guildId ?? ''),
name: String(input.name ?? ''),
kind: input.kind === 'announcement' ? 'announcement' : 'text',
isPrivate: Boolean(input.isPrivate),
lastSeq: 0,
});
return this.channelRepo.save(channel);
}
}

View File

@@ -1,9 +1,17 @@
import { Body, Controller, Post } from '@nestjs/common';
import { Body, Controller, Get, Post } from '@nestjs/common';
import { GuildsService } from './guilds.service';
@Controller('guilds')
export class GuildsController {
constructor(private readonly guildsService: GuildsService) {}
@Get()
list() {
return this.guildsService.list();
}
@Post()
create(@Body() body: Record<string, unknown>) {
return { status: 'todo', action: 'create-guild', received: body };
return this.guildsService.create(body);
}
}

View File

@@ -1,7 +1,13 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { GuildsController } from './guilds.controller';
import { Guild } from '../entities/guild.entity';
import { GuildsService } from './guilds.service';
@Module({
imports: [TypeOrmModule.forFeature([Guild])],
controllers: [GuildsController],
providers: [GuildsService],
exports: [GuildsService],
})
export class GuildsModule {}

View File

@@ -0,0 +1,27 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Guild } from '../entities/guild.entity';
@Injectable()
export class GuildsService {
constructor(
@InjectRepository(Guild)
private readonly guildRepo: Repository<Guild>,
) {}
list() {
return this.guildRepo.find({
order: { createdAt: 'DESC' },
take: 100,
});
}
create(input: Partial<Guild>) {
const slug = String(input.slug ?? '').trim();
const name = String(input.name ?? '').trim();
const ownerUserId = input.ownerUserId ? String(input.ownerUserId) : null;
const guild = this.guildRepo.create({ slug, name, ownerUserId });
return this.guildRepo.save(guild);
}
}