53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
import { db } from '../../db/db_index'
|
|
import { storeInfo, productInfo } from '../../db/schema'
|
|
import { eq, inArray } from 'drizzle-orm'
|
|
import { IStoreDbService, Store, NewStore } from '../../../../backend/src/trpc/apis/admin-apis/dataAccessors/interfaces/store-db-service.interface'
|
|
|
|
export class StoreDbService implements IStoreDbService {
|
|
async getAllStores(): Promise<Store[]> {
|
|
return db.query.storeInfo.findMany({
|
|
with: { owner: true },
|
|
})
|
|
}
|
|
|
|
async getStoreById(id: number): Promise<Store | undefined> {
|
|
return db.query.storeInfo.findFirst({
|
|
where: eq(storeInfo.id, id),
|
|
with: { owner: true },
|
|
})
|
|
}
|
|
|
|
async createStore(data: NewStore): Promise<Store> {
|
|
const [store] = await db.insert(storeInfo).values(data).returning()
|
|
return store
|
|
}
|
|
|
|
async updateStore(id: number, data: Partial<NewStore>): Promise<Store> {
|
|
const [store] = await db
|
|
.update(storeInfo)
|
|
.set(data)
|
|
.where(eq(storeInfo.id, id))
|
|
.returning()
|
|
return store
|
|
}
|
|
|
|
async deleteStore(id: number): Promise<void> {
|
|
await db.delete(storeInfo).where(eq(storeInfo.id, id))
|
|
}
|
|
|
|
async assignProductsToStore(storeId: number, productIds: number[]): Promise<void> {
|
|
await db
|
|
.update(productInfo)
|
|
.set({ storeId })
|
|
.where(inArray(productInfo.id, productIds))
|
|
}
|
|
|
|
async removeProductsFromStore(storeId: number): Promise<void> {
|
|
await db
|
|
.update(productInfo)
|
|
.set({ storeId: null })
|
|
.where(eq(productInfo.storeId, storeId))
|
|
}
|
|
}
|
|
|
|
export const storeDbService: IStoreDbService = new StoreDbService()
|