Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/supply/supply.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Param,
Post,
Put,
Query,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';

Expand All @@ -31,6 +32,23 @@ export class SupplyController {
}
}

@Get('top')
async top(@Query('limit') limit: number = 10, @Query('skip') skip: number) {
try {
if (limit && typeof limit === 'string') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

é só usar o pipe ParseIntPipe

see: https://docs.nestjs.com/pipes#built-in-pipes

Copy link
Author

@balsemao balsemao May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boa, ajustado!

limit = Number.parseInt(limit);
}
if (skip && typeof skip === 'string') {
skip = Number.parseInt(skip);
}
const data = await this.supplyServices.top(limit, skip);
return new ServerResponse(200, 'Successfully get top supplies', data);
} catch (err: any) {
this.logger.error(`Failed to get supplies: ${err}`);
throw new HttpException(err?.code ?? err?.name ?? `${err}`, 400);
}
}

@Post('')
async store(@Body() body) {
try {
Expand Down
18 changes: 18 additions & 0 deletions src/supply/supply.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,22 @@ export class SupplyService {

return data;
}

async top(top: number = 10, skip: number = 0) {
const data = await this.prismaService.supply.groupBy({
by: ['name'],
_count: {
name: true,
},
orderBy: {
_count: {
name: 'desc',
},
},
skip: skip ?? 0,
take: top ?? 10,
});

return data.map((row) => ({ name: row.name, amount: row._count.name }));
}
}