added tons of features
This commit is contained in:
27
lib/data/mapResponseToDTO.ts
Normal file
27
lib/data/mapResponseToDTO.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
// https://medium.com/@mokremiz/building-a-flexible-dto-mapper-for-react-and-next-js-projects-3ee77055f05d
|
||||
export const mapResponseToDTO = <T, U>(
|
||||
responseDTO: U,
|
||||
propertyMappings?: Record<string, keyof T>
|
||||
): T => {
|
||||
// Create an empty object that will hold the mapped DTO
|
||||
const mappedDTO: Partial<T> = {}
|
||||
|
||||
// Loop through each property in the responseDTO
|
||||
for (const key in responseDTO) {
|
||||
// Check if propertyMappings exist and if the current key is in propertyMappings
|
||||
if (propertyMappings && key in propertyMappings) {
|
||||
// If there is a mapping for the current key, use it to set the property in the mappedDTO
|
||||
mappedDTO[propertyMappings[key] as keyof T] = responseDTO[
|
||||
key
|
||||
] as unknown as T[keyof T]
|
||||
} else {
|
||||
// If there is no mapping for the current key, use the key as is to set the property in the mappedDTO
|
||||
mappedDTO[key as unknown as keyof T] = responseDTO[
|
||||
key
|
||||
] as unknown as T[keyof T]
|
||||
}
|
||||
}
|
||||
|
||||
// Return the mappedDTO as a type T
|
||||
return mappedDTO as T
|
||||
}
|
||||
50
lib/data/models/product.ts
Normal file
50
lib/data/models/product.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
'use server'
|
||||
|
||||
import {Lang, Product, ProductLocale} from '@prisma/client'
|
||||
import internal from 'node:stream'
|
||||
|
||||
import {db, dbQueryLog} from '@/lib/db/prisma/client'
|
||||
|
||||
export const getProductBySlug = async (data: {
|
||||
slug: string
|
||||
lang: string
|
||||
}): Promise<ProductLocale | null> => {
|
||||
return db.productLocale.findFirst({
|
||||
where: {
|
||||
slug: data.slug,
|
||||
lang: data.lang as Lang
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const getProductById = async (id: unknown): Promise<Product | null> => {
|
||||
return db.product.findUnique({
|
||||
where: {id: id as number},
|
||||
include: {
|
||||
locales: {
|
||||
include: {
|
||||
meta: true
|
||||
}
|
||||
},
|
||||
toStore: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export const getProducts = async (): Promise<Product[] | null> => {
|
||||
return db.product.findMany({
|
||||
include: {
|
||||
locales: {
|
||||
omit: {
|
||||
description: true,
|
||||
content: true,
|
||||
instruction: true
|
||||
},
|
||||
include: {
|
||||
meta: true
|
||||
}
|
||||
},
|
||||
toStore: true
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user