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.
This commit is contained in:
Mathis H (Avnyr) 2024-07-15 12:16:25 +02:00
parent 5ee51daf82
commit b9b2adb804
Signed by: Mathis
GPG Key ID: DD9E0666A747D126
3 changed files with 9 additions and 5 deletions

View File

@ -21,7 +21,7 @@ export class ProductsController {
@HttpCode(HttpStatus.CREATED) @HttpCode(HttpStatus.CREATED)
@UseGuards(AdminGuard) @UseGuards(AdminGuard)
@Post('new') @Post('add')
async newProduct(@Body() dto: CreateProductDto) { async newProduct(@Body() dto: CreateProductDto) {
const result = await this.productsService.add(dto) const result = await this.productsService.add(dto)
if (result.count !== 1) { if (result.count !== 1) {
@ -33,7 +33,7 @@ export class ProductsController {
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@Get("all") @Get("all")
async getAllProducts(@Query('page') page: number) { async getAllProducts(@Query('page') page: number) {
return this.productsService.findAll(page || 0, 20); return this.productsService.findAll(Number(page) || 1, 2);
} }
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)

View File

@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
import { ProductsController } from './products.controller'; import { ProductsController } from './products.controller';
import { ProductsService } from './products.service'; import { ProductsService } from './products.service';
import { DrizzleModule } from "src/drizzle/drizzle.module"; import { DrizzleModule } from "src/drizzle/drizzle.module";
import { CredentialsModule } from "src/credentials/credentials.module";
@Module({ @Module({
imports: [DrizzleModule], imports: [DrizzleModule, CredentialsModule],
controllers: [ProductsController], controllers: [ProductsController],
providers: [ProductsService] providers: [ProductsService]
}) })

View File

@ -73,6 +73,8 @@ export class ProductsService {
.prepare("countProducts") .prepare("countProducts")
.execute(); .execute();
console.log(count, {page: page}, {limit: limit})
const res = await this.db.use() const res = await this.db.use()
.select() .select()
.from(ProductsTable) .from(ProductsTable)
@ -82,8 +84,9 @@ export class ProductsService {
.execute(); .execute();
console.log(`Fetching products (page ${page}, limit ${limit}) ...\n`) console.log(`Fetching products (page ${page}, limit ${limit}) ...\n`)
const response = { const response = {
total: count[0].total, maxPage: (Math.round(count[0].total / limit)),
page: page, currentPage: page,
totalProducts: count[0].total,
productsPerPage: limit, productsPerPage: limit,
products: res products: res
} }