From b6a15e0579a9c834a7c46925869ae1d41d4b7e11 Mon Sep 17 00:00:00 2001 From: behnam Date: Fri, 1 Nov 2024 16:06:43 +0300 Subject: [PATCH] Add fetch all customers module --- src/feature/customer/data/repo/customer-db-repo.ts | 5 +++++ src/feature/customer/domain/i-repo/customer-repo.ts | 1 + .../domain/usecase/fetch-customers-amount-usecase.ts | 8 ++++++++ 3 files changed, 14 insertions(+) create mode 100644 src/feature/customer/domain/usecase/fetch-customers-amount-usecase.ts diff --git a/src/feature/customer/data/repo/customer-db-repo.ts b/src/feature/customer/data/repo/customer-db-repo.ts index fec27e1..f69239a 100644 --- a/src/feature/customer/data/repo/customer-db-repo.ts +++ b/src/feature/customer/data/repo/customer-db-repo.ts @@ -46,6 +46,11 @@ export default class CustomerDbRepo implements CustomerRepo { } } + async fetchCustomersAmount(): Promise { + const data = await sql`SELECT COUNT(*) FROM customers`as postgres.RowList; + return Number(data.count ?? '0'); + } + private customersDto(dbCustomers: customerDbResponse[]): Customer[] { return dbCustomers.map((customer) => this.customerDto(customer)); diff --git a/src/feature/customer/domain/i-repo/customer-repo.ts b/src/feature/customer/domain/i-repo/customer-repo.ts index 5c9cbf2..08e8226 100644 --- a/src/feature/customer/domain/i-repo/customer-repo.ts +++ b/src/feature/customer/domain/i-repo/customer-repo.ts @@ -2,6 +2,7 @@ import Customer from "@/feature/customer/domain/entity/customer" export default interface CustomerRepo { fetchList(query: string): Promise + fetchCustomersAmount(): Promise } export const customerRepoKey = "customerRepoKey" \ No newline at end of file diff --git a/src/feature/customer/domain/usecase/fetch-customers-amount-usecase.ts b/src/feature/customer/domain/usecase/fetch-customers-amount-usecase.ts new file mode 100644 index 0000000..608ae37 --- /dev/null +++ b/src/feature/customer/domain/usecase/fetch-customers-amount-usecase.ts @@ -0,0 +1,8 @@ +import serverDi from "@/feature/common/server-di"; +import { customerKey } from "@/feature/customer/customer-key"; +import CustomerRepo, { customerRepoKey } from "@/feature/customer/domain/i-repo/customer-repo"; + +export default function fetchCustomersAmountUsecase(): Promise { + const repo = serverDi(customerKey).resolve(customerRepoKey) + return repo.fetchCustomersAmount() +} \ No newline at end of file