Skip to content

Commit bb8df68

Browse files
authored
feat(server): impl application query api (#423)
* feat(server): impl application query api Signed-off-by: maslow <[email protected]> * chore: delete redundant files Signed-off-by: maslow <[email protected]>
1 parent 261ebfb commit bb8df68

13 files changed

+314
-93
lines changed

.vscode/settings.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"moby",
6262
"MONOG",
6363
"mycrd",
64+
"nestjs",
6465
"objs",
6566
"openebs",
6667
"passw",
@@ -77,9 +78,9 @@
7778
"uids",
7879
"upsert",
7980
"upserted",
81+
"userid",
8082
"vitepress",
8183
"withs",
82-
"zcube",
83-
"nestjs"
84+
"zcube"
8485
]
8586
}

server/package-lock.json

+104
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"dotenv": "^16.0.3",
3939
"nanoid": "^3.3.4",
4040
"passport-jwt": "^4.0.0",
41+
"passport-local": "^1.0.0",
4142
"reflect-metadata": "^0.1.13",
4243
"rimraf": "^3.0.2",
4344
"rxjs": "^7.2.0"
@@ -50,6 +51,8 @@
5051
"@types/express": "^4.17.13",
5152
"@types/jest": "28.1.8",
5253
"@types/node": "^16.0.0",
54+
"@types/passport-jwt": "^3.0.7",
55+
"@types/passport-local": "^1.0.34",
5356
"@types/supertest": "^2.0.11",
5457
"@typescript-eslint/eslint-plugin": "^5.0.0",
5558
"@typescript-eslint/parser": "^5.0.0",

server/src/app.controller.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Controller, Get, Query, Req, Res, UseGuards } from '@nestjs/common'
2-
import { Response } from 'express'
2+
import { User } from '@prisma/client'
3+
import { Request, Response } from 'express'
34
import { AuthService } from './auth/auth.service'
45
import { JwtAuthGuard } from './auth/jwt-auth.guard'
56
import { ResponseUtil } from './utils/response'
@@ -44,7 +45,8 @@ export class AppController {
4445

4546
@UseGuards(JwtAuthGuard)
4647
@Get('profile')
47-
async getProfile(@Req() request) {
48-
return request.user
48+
async getProfile(@Req() request: Request) {
49+
const user = request.user as User
50+
return ResponseUtil.ok(user)
4951
}
5052
}

server/src/applications/applications.controller.ts

+30-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ import {
66
Patch,
77
Param,
88
Delete,
9+
UseGuards,
10+
Req,
11+
Res,
912
} from '@nestjs/common'
10-
import { ResponseUtil } from 'src/utils/response'
13+
import { User } from '@prisma/client'
14+
import { Request, Response } from 'express'
15+
import { JwtAuthGuard } from '../auth/jwt-auth.guard'
16+
import { ResponseUtil } from '../utils/response'
1117
import { ApplicationsService } from './applications.service'
1218
import { CreateApplicationDto } from './dto/create-application.dto'
1319
import { UpdateApplicationDto } from './dto/update-application.dto'
@@ -20,36 +26,51 @@ export class ApplicationsController {
2026
* Create application
2127
* @returns
2228
*/
29+
@UseGuards(JwtAuthGuard)
2330
@Post()
24-
async create(@Body() dto: CreateApplicationDto) {
31+
async create(@Body() dto: CreateApplicationDto, @Req() req: Request) {
32+
const user = req.user as User
2533
const error = dto.validate()
2634
if (error) {
2735
return ResponseUtil.error(error)
2836
}
2937

3038
// create namespace
3139
const appid = this.appService.generateAppid(6)
32-
const namespace = await this.appService.createAppNamespace(appid)
40+
const namespace = await this.appService.createAppNamespace(user.id, appid)
3341
if (!namespace) {
3442
return ResponseUtil.error('create app namespace error')
3543
}
3644

3745
// create app
38-
const app = await this.appService.create(appid, dto)
46+
const app = await this.appService.create(user.id, appid, dto)
3947
if (!app) {
4048
return ResponseUtil.error('create app error')
4149
}
4250
return ResponseUtil.ok(app)
4351
}
4452

53+
@UseGuards(JwtAuthGuard)
4554
@Get()
46-
findAll() {
47-
return this.appService.findAll()
55+
async findAll(@Req() req: Request) {
56+
const user = req.user as User
57+
const data = this.appService.findAllByUser(user.id)
58+
return ResponseUtil.ok(data)
4859
}
4960

50-
@Get(':id')
51-
findOne(@Param('id') id: string) {
52-
return this.appService.findOne(+id)
61+
@UseGuards(JwtAuthGuard)
62+
@Get(':appid')
63+
findOne(
64+
@Param('appid') appid: string,
65+
@Req() req: Request,
66+
@Res() res: Response,
67+
) {
68+
const user = req.user as User
69+
const data = this.appService.findOne(user.id, appid)
70+
if (null === data) {
71+
return res.status(404).send('Application not found with appid: ' + appid)
72+
}
73+
return ResponseUtil.ok(data)
5374
}
5475

5576
@Patch(':id')

server/src/applications/applications.service.spec.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ describe('AppsService', () => {
2121
})
2222
})
2323

24+
const userid = 'test-user-id'
25+
2426
describe('AppService create app', () => {
2527
const timeout = 60 * 1000
2628
let service: ApplicationsService
@@ -46,22 +48,22 @@ describe('AppService create app', () => {
4648
}
4749

4850
it('should create app', async () => {
51+
appid = service.generateAppid(6)
4952
const dto = new CreateApplicationDto()
50-
dto.name = 'test-for-create-app'
53+
dto.name = appid
5154
dto.state = ApplicationState.ApplicationStateRunning
5255
dto.region = 'default'
5356
dto.bundleName = 'mini'
5457
dto.runtimeName = 'node-laf'
5558

5659
// create namespace
57-
appid = service.generateAppid(6)
58-
const ns = await service.createAppNamespace(appid)
60+
const ns = await service.createAppNamespace(appid, userid)
5961
expect(ns).toBeDefined()
6062
expect(ns.kind).toEqual('Namespace')
6163
expect(ns.metadata.name).toEqual(appid)
6264

6365
// create app
64-
const res = await service.create(appid, dto)
66+
const res = await service.create(userid, appid, dto)
6567
expect(res).not.toBeNull()
6668
expect(res.kind).toBe('Application')
6769
expect(res.metadata.name).toBe(dto.name)
@@ -72,3 +74,28 @@ describe('AppService create app', () => {
7274
await cleanup()
7375
}, 20000)
7476
})
77+
78+
describe.skip('AppService find app by appid', () => {
79+
const timeout = 60 * 1000
80+
let service: ApplicationsService
81+
let appid: string
82+
beforeAll(async () => {
83+
const module: TestingModule = await Test.createTestingModule({
84+
imports: [CoreModule],
85+
providers: [ApplicationsService],
86+
}).compile()
87+
88+
service = module.get<ApplicationsService>(ApplicationsService)
89+
}, timeout)
90+
91+
jest.setTimeout(timeout)
92+
93+
it('should find app by appid', async () => {
94+
appid = '1i43zq'
95+
const res = await service.findOne(userid, appid)
96+
expect(res).not.toBeNull()
97+
expect(res.kind).toBe('Application')
98+
expect(res.metadata.name).toBe(appid)
99+
console.log(res)
100+
})
101+
})

0 commit comments

Comments
 (0)