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.
101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import { Injectable, InternalServerErrorException } from "@nestjs/common";
|
|
import { DrizzleService } from "src/drizzle/drizzle.service";
|
|
import { ProductsTable } from "src/schema";
|
|
import { CreateProductDto, EditProductDto } from "src/products/products.dto";
|
|
import { countDistinct, eq } from "drizzle-orm";
|
|
|
|
@Injectable()
|
|
export class ProductsService {
|
|
|
|
constructor(
|
|
private db: DrizzleService,
|
|
) {}
|
|
|
|
async add(data: CreateProductDto) {
|
|
try {
|
|
const res = await this.db.use()
|
|
.insert(ProductsTable)
|
|
.values({
|
|
slugName: data.slugName,
|
|
displayName: data.displayName,
|
|
price: String(data.price),
|
|
imagePath: "placeholder"
|
|
})
|
|
console.log(`Adding new product "${data.slugName}" ...\n`, res)
|
|
return res;
|
|
} catch (err) {
|
|
throw new InternalServerErrorException(err);
|
|
}
|
|
}
|
|
|
|
async edit(productId: string, data: EditProductDto) {
|
|
try {
|
|
const res = await this.db.use()
|
|
.update(ProductsTable)
|
|
.set({
|
|
slugName: data.slugName,
|
|
displayName: data.displayName,
|
|
price: String(data.price),
|
|
})
|
|
.where(eq(ProductsTable.uuid, productId))
|
|
.prepare("editProductById")
|
|
.execute();
|
|
console.log(`Editing product n°${productId} ...\n`, res)
|
|
return res;
|
|
} catch (err) {
|
|
throw new InternalServerErrorException(err);
|
|
}
|
|
}
|
|
|
|
async delete(productId: string) {
|
|
try {
|
|
const res = await this.db.use()
|
|
.delete(ProductsTable)
|
|
.where(eq(ProductsTable.uuid, productId))
|
|
.prepare("deleteProductById")
|
|
.execute();
|
|
console.log(`Deleting product n°${productId} ...\n`, res)
|
|
return res;
|
|
} catch (err) {
|
|
throw new InternalServerErrorException(err);
|
|
}
|
|
}
|
|
|
|
//TODO list all product with pagination.
|
|
async findAll(page: number, limit: number) {
|
|
try {
|
|
//get the number of row first
|
|
const count = await this.db.use()
|
|
.select({
|
|
total: countDistinct(ProductsTable.uuid)
|
|
})
|
|
.from(ProductsTable)
|
|
.prepare("countProducts")
|
|
.execute();
|
|
|
|
console.log(count, {page: page}, {limit: limit})
|
|
|
|
const res = await this.db.use()
|
|
.select()
|
|
.from(ProductsTable)
|
|
.limit(limit)
|
|
.offset((page - 1) * limit)
|
|
.prepare("findAllProducts")
|
|
.execute();
|
|
console.log(`Fetching products (page ${page}, limit ${limit}) ...\n`)
|
|
const response = {
|
|
maxPage: (Math.round(count[0].total / limit)),
|
|
currentPage: page,
|
|
totalProducts: count[0].total,
|
|
productsPerPage: limit,
|
|
products: res
|
|
}
|
|
console.log(response)
|
|
return response;
|
|
} catch (err) {
|
|
throw new InternalServerErrorException(err);
|
|
}
|
|
}
|
|
|
|
}
|