From b9b2adb804ef8c55a084ab1b0371c37f711bb1bf Mon Sep 17 00:00:00 2001 From: Mathis Date: Mon, 15 Jul 2024 12:16:25 +0200 Subject: [PATCH] feat(products): update products service, controller and module Updated products.service.ts to improve product fetching with added console logs and response modifications. Endpoint name in products.controller.ts has been changed from 'new' to 'add' with parameter adjustments for fetching. In products.module.ts, CredentialsModule was imported for enhanced functionality. --- src/products/products.controller.ts | 4 ++-- src/products/products.module.ts | 3 ++- src/products/products.service.ts | 7 +++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/products/products.controller.ts b/src/products/products.controller.ts index 75fca83..c294fc4 100644 --- a/src/products/products.controller.ts +++ b/src/products/products.controller.ts @@ -21,7 +21,7 @@ export class ProductsController { @HttpCode(HttpStatus.CREATED) @UseGuards(AdminGuard) - @Post('new') + @Post('add') async newProduct(@Body() dto: CreateProductDto) { const result = await this.productsService.add(dto) if (result.count !== 1) { @@ -33,7 +33,7 @@ export class ProductsController { @HttpCode(HttpStatus.OK) @Get("all") async getAllProducts(@Query('page') page: number) { - return this.productsService.findAll(page || 0, 20); + return this.productsService.findAll(Number(page) || 1, 2); } @HttpCode(HttpStatus.OK) diff --git a/src/products/products.module.ts b/src/products/products.module.ts index 99f61cf..0815b30 100644 --- a/src/products/products.module.ts +++ b/src/products/products.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { ProductsController } from './products.controller'; import { ProductsService } from './products.service'; import { DrizzleModule } from "src/drizzle/drizzle.module"; +import { CredentialsModule } from "src/credentials/credentials.module"; @Module({ - imports: [DrizzleModule], + imports: [DrizzleModule, CredentialsModule], controllers: [ProductsController], providers: [ProductsService] }) diff --git a/src/products/products.service.ts b/src/products/products.service.ts index de22626..80730b5 100644 --- a/src/products/products.service.ts +++ b/src/products/products.service.ts @@ -73,6 +73,8 @@ export class ProductsService { .prepare("countProducts") .execute(); + console.log(count, {page: page}, {limit: limit}) + const res = await this.db.use() .select() .from(ProductsTable) @@ -82,8 +84,9 @@ export class ProductsService { .execute(); console.log(`Fetching products (page ${page}, limit ${limit}) ...\n`) const response = { - total: count[0].total, - page: page, + maxPage: (Math.round(count[0].total / limit)), + currentPage: page, + totalProducts: count[0].total, productsPerPage: limit, products: res }