Skip to content

Commit 62e88fe

Browse files
committed
add module config
1 parent 06f5f70 commit 62e88fe

File tree

6 files changed

+136
-1
lines changed

6 files changed

+136
-1
lines changed

public/openapi.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@ tags:
1414
description: "Management user account"
1515

1616
paths:
17+
/config:
18+
get:
19+
tags:
20+
- "Misc"
21+
parameters:
22+
- in: query
23+
name: key
24+
required: false
25+
schema:
26+
type: string
27+
responses:
28+
200:
29+
description: "Ok"
1730
/file/upload:
1831
post:
1932
tags:

src/apps/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { AuthModule } from './auth/auth.module';
99
import { FileModule } from './file/file.module';
1010
import { I18nModule, QueryResolver, HeaderResolver, AcceptLanguageResolver } from 'nestjs-i18n'
1111
import { join } from 'path';
12+
import { ConfigModule as AppConfigModule } from './config/config.module';
1213
@Module({
1314
imports: [
1415
ConfigModule.forRoot({
@@ -36,7 +37,8 @@ import { join } from 'path';
3637
}
3738
}),
3839
AuthModule,
39-
FileModule
40+
FileModule,
41+
AppConfigModule
4042
],
4143
controllers: [AppController],
4244
providers: [AppService],

src/apps/config/config.controller.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Controller, Get, Query } from '@nestjs/common';
2+
import { Response } from 'src/utils';
3+
import { ConfigService } from './config.service';
4+
5+
@Controller('config')
6+
export class ConfigController {
7+
8+
9+
constructor(private configService: ConfigService) { }
10+
11+
@Get()
12+
async getConfig(@Query('key') key: string) {
13+
14+
const response = await this.configService.getConfig(key);
15+
16+
return Response.success({
17+
message: "",
18+
data: response,
19+
})
20+
}
21+
}

src/apps/config/config.module.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Module } from '@nestjs/common';
2+
import { ConfigService } from './config.service';
3+
import { ConfigController } from './config.controller';
4+
import { TypeOrmModule } from '@nestjs/typeorm';
5+
import { AppConfig } from 'src/entities/app-config.entity';
6+
7+
@Module({
8+
imports: [
9+
TypeOrmModule.forFeature([AppConfig]),
10+
],
11+
providers: [ConfigService],
12+
controllers: [ConfigController]
13+
})
14+
export class ConfigModule {}

src/apps/config/config.service.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Injectable } from '@nestjs/common';
2+
import { InjectRepository } from '@nestjs/typeorm';
3+
import { AppConfig } from 'src/entities/app-config.entity';
4+
import { Repository } from 'typeorm';
5+
6+
@Injectable()
7+
export class ConfigService {
8+
9+
constructor(
10+
@InjectRepository(AppConfig) private appConfigService: Repository<AppConfig>
11+
) { }
12+
13+
async getConfig(configKey: string) {
14+
//
15+
16+
let query = this.appConfigService.createQueryBuilder().from('setting_apps', 'conf').select('*')
17+
18+
if (configKey != '' && configKey.indexOf('.') == -1) {
19+
query = query.where('conf.group = :group', {
20+
group: configKey
21+
})
22+
}
23+
const config = await query.execute();
24+
const results = {}
25+
26+
config.forEach((item: { group: any, key: any, value: any }) => {
27+
let isAdded = false;
28+
const [primaryGroup, paramGroup] = configKey.split('.');
29+
30+
if (paramGroup == undefined && configKey == item.group) {
31+
isAdded = true;
32+
}
33+
34+
if (paramGroup != undefined) {
35+
if (primaryGroup == item.group) {
36+
if (paramGroup == item.key) {
37+
isAdded = true;
38+
}
39+
}
40+
}
41+
42+
if (configKey == '') isAdded = true;
43+
44+
if (isAdded) {
45+
46+
if (results[item.group] == undefined) {
47+
results[item.group] = {};
48+
}
49+
50+
if (results[item.group][item.key] == undefined) {
51+
results[item.group][item.key] = {}
52+
}
53+
54+
try {
55+
results[item.group][item.key] = JSON.parse(item.value);
56+
} catch (error) {
57+
results[item.group][item.key] = item.value;
58+
}
59+
}
60+
61+
62+
})
63+
64+
return results;
65+
}
66+
}

src/entities/app-config.entity.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
2+
3+
@Entity({
4+
name: "setting_apps"
5+
})
6+
export class AppConfig {
7+
8+
@PrimaryGeneratedColumn()
9+
id: number;
10+
11+
@Column()
12+
name: string;
13+
14+
@Column()
15+
value: string;
16+
17+
@Column()
18+
group: string
19+
}

0 commit comments

Comments
 (0)