feat(guild): add guild and channel list APIs for frontend browser
This commit is contained in:
@@ -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')
|
@Controller('channels')
|
||||||
export class ChannelsController {
|
export class ChannelsController {
|
||||||
|
constructor(private readonly channelsService: ChannelsService) {}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
list(@Query('guildId') guildId?: string) {
|
||||||
|
if (!guildId) return [];
|
||||||
|
return this.channelsService.listByGuild(guildId);
|
||||||
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() body: Record<string, unknown>) {
|
create(@Body() body: Record<string, unknown>) {
|
||||||
return { status: 'todo', action: 'create-channel', received: body };
|
return this.channelsService.create(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { ChannelsController } from './channels.controller';
|
import { ChannelsController } from './channels.controller';
|
||||||
|
import { Channel } from '../entities/channel.entity';
|
||||||
|
import { ChannelsService } from './channels.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
imports: [TypeOrmModule.forFeature([Channel])],
|
||||||
controllers: [ChannelsController],
|
controllers: [ChannelsController],
|
||||||
|
providers: [ChannelsService],
|
||||||
|
exports: [ChannelsService],
|
||||||
})
|
})
|
||||||
export class ChannelsModule {}
|
export class ChannelsModule {}
|
||||||
|
|||||||
31
Fabric.Backend.Guild/src/channels/channels.service.ts
Normal file
31
Fabric.Backend.Guild/src/channels/channels.service.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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')
|
@Controller('guilds')
|
||||||
export class GuildsController {
|
export class GuildsController {
|
||||||
|
constructor(private readonly guildsService: GuildsService) {}
|
||||||
|
|
||||||
|
@Get()
|
||||||
|
list() {
|
||||||
|
return this.guildsService.list();
|
||||||
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
create(@Body() body: Record<string, unknown>) {
|
create(@Body() body: Record<string, unknown>) {
|
||||||
return { status: 'todo', action: 'create-guild', received: body };
|
return this.guildsService.create(body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { GuildsController } from './guilds.controller';
|
import { GuildsController } from './guilds.controller';
|
||||||
|
import { Guild } from '../entities/guild.entity';
|
||||||
|
import { GuildsService } from './guilds.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
imports: [TypeOrmModule.forFeature([Guild])],
|
||||||
controllers: [GuildsController],
|
controllers: [GuildsController],
|
||||||
|
providers: [GuildsService],
|
||||||
|
exports: [GuildsService],
|
||||||
})
|
})
|
||||||
export class GuildsModule {}
|
export class GuildsModule {}
|
||||||
|
|||||||
27
Fabric.Backend.Guild/src/guilds/guilds.service.ts
Normal file
27
Fabric.Backend.Guild/src/guilds/guilds.service.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user