diff --git a/src/modules/admin/goods/goods.controller.ts b/src/modules/admin/goods/goods.controller.ts index 13252fa..6174fae 100644 --- a/src/modules/admin/goods/goods.controller.ts +++ b/src/modules/admin/goods/goods.controller.ts @@ -16,7 +16,7 @@ import { Roles } from "@decorators/roles"; import { User } from "@decorators/route"; import { GidDto } from "@dtos/ids"; import { IReqUser } from "@dtos/req"; -import { PerPageDto, ListResponse } from "@dtos/page"; +import { ListResponse } from "@dtos/page"; import { DefResDto } from "@dtos/res"; import { CreateValueDto, EditValueDto } from "@dtos/values"; import { ParseIntPipe } from "@pipes/parse-int"; @@ -33,7 +33,7 @@ import multer = require("multer"); import { isArray } from "util"; import { - GoodAttributeParamDto, UploadQueryDto, EditBodyDto + GoodAttributeParamDto, UploadQueryDto, EditBodyDto, GetGoodsDto } from "./goods.dto"; import { RegexpCountCheckInterceptor } from "@interceptors/regexp-count-check"; import { LogsService } from "@services/logs"; @@ -72,13 +72,24 @@ export class GoodsAdminController { type: ListResponse }) // endregion Swagger Docs - public async getGoods(@Query(new ParseIntPipe()) query: PerPageDto) { - const arr = await this.goodsSvr.getByUids( - [ ], query - ); - return UtilService.toListRespone(arr, Object.assign({ - total: await this.goodsSvr.countByUids([ ]) - }, query)); + public async getGoods( + @Query(new ParseIntPipe()) query: GetGoodsDto + ) { + let arr: any[], total: number; + if (query.cid) { + arr = await this.goodsSvr.listByCategoryId(query.cid, query); + total = await this.goodsSvr.countByCids(query.cid); + } else { + arr = await this.goodsSvr.getByUids([ ], query); + total = await this.goodsSvr.countByUids([ ]); + } + for (let i = 0; i < arr.length; i++) { + const good = arr[i]; + good.downloaded = + await this.logsSvr.goodDownloadCount(good._id); + arr[i] = good; + } + return UtilService.toListRespone(arr, Object.assign({ total }, query)); } private async getCategoriesIds(names: string[]) { diff --git a/src/modules/admin/goods/goods.dto.ts b/src/modules/admin/goods/goods.dto.ts index 6d8b18d..ea5c84c 100644 --- a/src/modules/admin/goods/goods.dto.ts +++ b/src/modules/admin/goods/goods.dto.ts @@ -3,6 +3,7 @@ import { ApiModelProperty, ApiModelPropertyOptional } from "@nestjs/swagger"; import { IsMongoId, IsString, IsOptional } from "class-validator"; import { ObjectId } from "@models/common"; import { IGoods } from "@models/Good"; +import { PerPageDto } from "@dtos/page"; export class GoodAttributeParamDto implements IGidDto, IAidDto { @ApiModelProperty({ type: String, description: "Good ID" }) @@ -13,11 +14,11 @@ export class GoodAttributeParamDto implements IGidDto, IAidDto { public readonly aid: ObjectId; } -export class GoodsDto { - @ApiModelProperty({ type: String, description: "Collection Name" }) - public readonly name: string; - @ApiModelProperty({ type: Object, description: "Goods", isArray: true }) - public readonly goods: IGoods[]; +export class GetGoodsDto extends PerPageDto { + @ApiModelPropertyOptional({type: String, description: "Category ID"}) + @IsMongoId() + @IsOptional() + public readonly cid: ObjectId; } export class UploadQueryDto { diff --git a/src/services/goods.service.ts b/src/services/goods.service.ts index ee8ed80..4d0b4db 100644 --- a/src/services/goods.service.ts +++ b/src/services/goods.service.ts @@ -49,7 +49,7 @@ export class GoodsService extends BaseService { cids = [ cids ]; } if (cids.length === 0) { - return [ ]; + return Promise.resolve(0); } const conditions = this.getConditionsByCids(cids); return this.total(conditions); diff --git a/test/api/goods/get_by_category.e2e.ts b/test/api/goods/get_by_category.e2e.ts new file mode 100644 index 0000000..c6dcb3f --- /dev/null +++ b/test/api/goods/get_by_category.e2e.ts @@ -0,0 +1,50 @@ +import { connect, drop } from "../../helpers/database"; +import { init } from "../../helpers/server"; +import { newIds } from "../../helpers/utils"; +import { GuestRequest, AdminRequest } from "../../helpers/request"; +import * as files from "../../helpers/files"; + +describe("Get Good List By Category ID", () => { + + let request: AdminRequest; + + before(() => { + return connect(); + }); + + const ids = newIds(); + + after(() => { + return drop(ids); + }); + + const filepaths = [ ]; + + after(() => { + return files.remove(filepaths); + }); + + before("login", async () => { + const req = await new GuestRequest(await init(), ids, filepaths); + request = await req.login(); + }); + + step(("Init Env"), async () => { + await request.newFile(); + await request.addCategoryWithRegexp(); + await request.uploadFile(); + await request.newFile(); + await request.addCategoryWithRegexp(); + await request.uploadFile(); + }); + + step("Checker Number", async () => { + const url = "/api/v1/goods"; + const { body: result0 } = await request.get(url).then(); + result0.should.have.property("total", 2); + const cid = ids.categories[ids.categories.length - 1]; + const { body: result1 } = await request.get(`${url}?cid=${cid}`).then(); + result1.should.have.property("total", 1); + }); + +});