From 6f0dce37ceaedbccfb3f5e852000a0890d925b62 Mon Sep 17 00:00:00 2001 From: hezhengxu Date: Sun, 14 Jul 2024 20:12:22 +0800 Subject: [PATCH] fix: set version default value to null --- app/port/controller/ProxyCacheController.ts | 12 ++++++------ app/repository/ProxyCacheRepository.ts | 7 ++++--- app/repository/model/ProxyCache.ts | 2 +- sql/3.47.0.sql | 2 +- .../controller/ProxyCacheController/index.test.ts | 5 +++-- test/repository/ProxyCachePepository.test.ts | 8 ++++---- 6 files changed, 19 insertions(+), 17 deletions(-) diff --git a/app/port/controller/ProxyCacheController.ts b/app/port/controller/ProxyCacheController.ts index 5ea90983a..db4dd3c4e 100644 --- a/app/port/controller/ProxyCacheController.ts +++ b/app/port/controller/ProxyCacheController.ts @@ -51,15 +51,15 @@ export class ProxyCacheController extends AbstractController { method: HTTPMethodEnum.GET, path: `/-/proxy-cache/:fullname(${FULLNAME_REG_STRING})`, }) - async showProxyCaches(@HTTPParam() fullname: string) { + async showProxyCaches(@HTTPQuery() pageSize: Static['pageSize'], + @HTTPQuery() pageIndex: Static['pageIndex'], @HTTPParam() fullname: string) { if (this.config.cnpmcore.syncMode !== SyncMode.proxy) { throw new ForbiddenError('proxy mode is not enabled'); } - const result = await this.proxyCacheRepository.findProxyCaches(fullname); - if (result.length === 0) { - throw new NotFoundError(); - } - return result; + return await this.proxyCacheRepository.listCachedFiles({ + pageSize, + pageIndex, + }, fullname); } @HTTPMethod({ diff --git a/app/repository/ProxyCacheRepository.ts b/app/repository/ProxyCacheRepository.ts index 168947b63..d1af53c0c 100644 --- a/app/repository/ProxyCacheRepository.ts +++ b/app/repository/ProxyCacheRepository.ts @@ -36,15 +36,16 @@ export class ProxyCacheRepository extends AbstractRepository { return null; } + // used by update & delete all cache async findProxyCaches(fullname: string, version?: string) { const models = version ? await this.ProxyCache.find({ fullname, version }) : await this.ProxyCache.find({ fullname }); return models; } - async listCachedFiles(page: PageOptions): Promise> { + async listCachedFiles(page: PageOptions, fullname?: string): Promise> { const { offset, limit } = EntityUtil.convertPageOptionsToLimitOption(page); - const count = await this.ProxyCache.find().count(); - const models = await this.ProxyCache.find().offset(offset).limit(limit); + const count = fullname ? await this.ProxyCache.find({ fullname }).count() : await this.ProxyCache.find().count(); + const models = fullname ? await this.ProxyCache.find({ fullname }).offset(offset).limit(limit) : await this.ProxyCache.find().offset(offset).limit(limit); return { count, data: models.map(model => ModelConvertor.convertModelToEntity(model, ProxyCacheEntity)), diff --git a/app/repository/model/ProxyCache.ts b/app/repository/model/ProxyCache.ts index 7e961739f..8a99e0d17 100644 --- a/app/repository/model/ProxyCache.ts +++ b/app/repository/model/ProxyCache.ts @@ -28,6 +28,6 @@ export class ProxyCache extends Bone { filePath: string; @Attribute(DataTypes.STRING(214)) - version: string; + version?: string; } diff --git a/sql/3.47.0.sql b/sql/3.47.0.sql index 613bef495..fb206426c 100644 --- a/sql/3.47.0.sql +++ b/sql/3.47.0.sql @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS `proxy_caches` ( `gmt_create` datetime(3) NOT NULL COMMENT 'create time', `gmt_modified` datetime(3) NOT NULL COMMENT 'modify time', `fullname` varchar(214) NOT NULL DEFAULT '' COMMENT '@scope/package name', - `version` varchar(214) NULL DEFAULT '' COMMENT 'package version', + `version` varchar(214) COMMENT 'package version', `file_type` varchar(30) NOT NULL DEFAULT '' COMMENT 'file type', `file_path` varchar(512) NOT NULL DEFAULT '' COMMENT 'nfs file path', PRIMARY KEY (`id`), diff --git a/test/port/controller/ProxyCacheController/index.test.ts b/test/port/controller/ProxyCacheController/index.test.ts index a4739b21d..ab3b58edd 100644 --- a/test/port/controller/ProxyCacheController/index.test.ts +++ b/test/port/controller/ProxyCacheController/index.test.ts @@ -76,13 +76,14 @@ describe('test/port/controller/PackageVersionFileController/listFiles.test.ts', mock(app.config.cnpmcore, 'syncMode', SyncMode.proxy); mock(app.config.cnpmcore, 'redirectNotFound', false); const res = await app.httpRequest().get('/-/proxy-cache/foo-bar').expect(200); - assert(res.body.length === 2); + assert(res.body.count === 2); }); it('should 404 when not found', async () => { mock(app.config.cnpmcore, 'syncMode', SyncMode.proxy); mock(app.config.cnpmcore, 'redirectNotFound', false); - await app.httpRequest().get('/-/proxy-cache/foo-bar-xxx').expect(404); + const res = await app.httpRequest().get('/-/proxy-cache/foo-bar-xxx').expect(200); + assert(res.body.count === 0); }); }); diff --git a/test/repository/ProxyCachePepository.test.ts b/test/repository/ProxyCachePepository.test.ts index 48e350eed..7ec93dd0f 100644 --- a/test/repository/ProxyCachePepository.test.ts +++ b/test/repository/ProxyCachePepository.test.ts @@ -53,8 +53,8 @@ describe('test/repository/ProxyCacheRepository.test.ts', () => { it('remove work', async () => { await proxyCacheRepository.removeProxyCache('foo-bar', DIST_NAMES.FULL_MANIFESTS); - const emptyRes = await proxyCacheRepository.listCachedFiles({}); - assert.deepEqual(emptyRes.data, []); + const { count } = await proxyCacheRepository.listCachedFiles({}); + assert.equal(count, 0); }); it('truncate work', async () => { @@ -63,8 +63,8 @@ describe('test/repository/ProxyCacheRepository.test.ts', () => { fileType: DIST_NAMES.FULL_MANIFESTS, })); await proxyCacheRepository.truncateProxyCache(); - const emptyRes = await proxyCacheRepository.listCachedFiles({}); - assert.deepEqual(emptyRes.data, []); + const { count } = await proxyCacheRepository.listCachedFiles({}); + assert.equal(count, 0); }); }); });