Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/modules/admin/goods/goods.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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[]) {
Expand Down
11 changes: 6 additions & 5 deletions src/modules/admin/goods/goods.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" })
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/services/goods.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class GoodsService extends BaseService<IGoods> {
cids = [ cids ];
}
if (cids.length === 0) {
return [ ];
return Promise.resolve(0);
}
const conditions = this.getConditionsByCids(cids);
return this.total(conditions);
Expand Down
50 changes: 50 additions & 0 deletions test/api/goods/get_by_category.e2e.ts
Original file line number Diff line number Diff line change
@@ -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);
});

});