gui version
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, OneToMany } from 'typeorm';
|
||||
import { Inbound } from '../../inbounds/entities/inbound.entity';
|
||||
|
||||
@Entity()
|
||||
export class Subscription {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column({ unique: true })
|
||||
uuid: string;
|
||||
|
||||
@Column({ default: true })
|
||||
isEnabled: boolean;
|
||||
|
||||
@OneToMany(() => Inbound, (inbound) => inbound.subscription)
|
||||
inbounds: Inbound[];
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Controller, Get, Post, Delete, Body, Param } from '@nestjs/common';
|
||||
import { SubscriptionsService } from './subscriptions.service';
|
||||
|
||||
@Controller('subscriptions')
|
||||
export class SubscriptionsController {
|
||||
constructor(private readonly subscriptionsService: SubscriptionsService) {}
|
||||
|
||||
@Get()
|
||||
findAll() {
|
||||
return this.subscriptionsService.findAll();
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body('name') name: string) {
|
||||
return this.subscriptionsService.create(name);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('id') id: string) {
|
||||
return this.subscriptionsService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { SubscriptionsService } from './subscriptions.service';
|
||||
import { SubscriptionsController } from './subscriptions.controller';
|
||||
import { Subscription } from './entities/subscription.entity';
|
||||
import { Inbound } from '../inbounds/entities/inbound.entity';
|
||||
import { XuiModule } from '../xui/xui.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Subscription, Inbound]), XuiModule],
|
||||
controllers: [SubscriptionsController],
|
||||
providers: [SubscriptionsService],
|
||||
exports: [SubscriptionsService],
|
||||
})
|
||||
export class SubscriptionsModule {}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Subscription } from './entities/subscription.entity';
|
||||
import { Inbound } from '../inbounds/entities/inbound.entity';
|
||||
import { XuiService } from '../xui/xui.service';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
@Injectable()
|
||||
export class SubscriptionsService {
|
||||
constructor(
|
||||
@InjectRepository(Subscription)
|
||||
private subRepo: Repository<Subscription>,
|
||||
@InjectRepository(Inbound)
|
||||
private inboundRepo: Repository<Inbound>,
|
||||
private xuiService: XuiService,
|
||||
) {}
|
||||
|
||||
findAll() {
|
||||
return this.subRepo.find({ relations: ['inbounds'], order: { createdAt: 'DESC' } });
|
||||
}
|
||||
|
||||
async create(name: string) {
|
||||
const sub = this.subRepo.create({
|
||||
name,
|
||||
uuid: uuidv4(),
|
||||
});
|
||||
return this.subRepo.save(sub);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
const sub = await this.subRepo.findOne({ where: { id }, relations: ['inbounds'] });
|
||||
if (!sub) return;
|
||||
|
||||
if (sub.inbounds) {
|
||||
for (const inbound of sub.inbounds) {
|
||||
await this.xuiService.deleteInbound(inbound.xuiId);
|
||||
}
|
||||
}
|
||||
|
||||
return this.subRepo.remove(sub);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user