Skip to content

Commit

Permalink
feat(server): add site setting api (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow authored May 12, 2023
1 parent 31764cf commit 9911a5d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 0 deletions.
8 changes: 8 additions & 0 deletions server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -735,3 +735,11 @@ model SmsVerifyCode {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}

model Setting {
id String @id @default(auto()) @map("_id") @db.ObjectId
key String @unique
value String
desc String
metadata Json? // extra meta data
}
2 changes: 2 additions & 0 deletions server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { GatewayModule } from './gateway/gateway.module'
import { PrismaModule } from './prisma/prisma.module'
import { SubscriptionModule } from './subscription/subscription.module'
import { AccountModule } from './account/account.module'
import { SettingModule } from './setting/setting.module'

@Module({
imports: [
Expand All @@ -45,6 +46,7 @@ import { AccountModule } from './account/account.module'
PrismaModule,
SubscriptionModule,
AccountModule,
SettingModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
35 changes: 35 additions & 0 deletions server/src/setting/setting.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Controller, Get, Logger, Param } from '@nestjs/common'
import { ApiOperation, ApiTags } from '@nestjs/swagger'
import { ResponseUtil } from 'src/utils/response'
import { SettingService } from './setting.service'

@ApiTags('Public')
@Controller('settings')
export class SettingController {
private readonly logger = new Logger(SettingController.name)

constructor(private readonly settingService: SettingService) {}

/**
* Get site settings
*/
@ApiOperation({ summary: 'Get site settings' })
@Get()
async getSettings() {
const data = await this.settingService.findAll()
return ResponseUtil.ok(data)
}

/**
* Get one site setting by key
*/
@ApiOperation({ summary: 'Get one site setting by key' })
@Get(':key')
async getSettingByKey(@Param('key') key: string) {
const data = await this.settingService.findOne(key)
if (!data) {
return ResponseUtil.error('Setting not found')
}
return ResponseUtil.ok(data)
}
}
9 changes: 9 additions & 0 deletions server/src/setting/setting.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common'
import { SettingService } from './setting.service'
import { SettingController } from './setting.controller'

@Module({
providers: [SettingService],
controllers: [SettingController],
})
export class SettingModule {}
19 changes: 19 additions & 0 deletions server/src/setting/setting.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Injectable, Logger } from '@nestjs/common'
import { PrismaService } from 'src/prisma/prisma.service'

@Injectable()
export class SettingService {
private readonly logger = new Logger(SettingService.name)

constructor(private readonly prisma: PrismaService) {}

async findAll() {
return await this.prisma.setting.findMany()
}

async findOne(key: string) {
return await this.prisma.setting.findUnique({
where: { key },
})
}
}

0 comments on commit 9911a5d

Please sign in to comment.