11import { Image , Prisma } from "@prisma/client"
22
33import prisma from "../prisma"
4- import { DEFAULT_LIMIT , DEFAULT_PAGE , IActionReturn , IGetListResponse } from "../shared/type"
5- import { imageSelect , TImageItem } from "./selects"
4+ import { DEFAULT_LIMIT , DEFAULT_PAGE , IActionReturn } from "../shared/type"
5+ import { imageSelect , TImage } from "./selects"
66import { IImageFilter , IListImageResponse } from "./type"
77
88export const getImages = async ( options : IImageFilter ) : Promise < IListImageResponse > => {
9- const {
10- page = DEFAULT_PAGE ,
11- limit = DEFAULT_LIMIT ,
12- userId,
13- orderBy : orderByKey ,
14- order,
15- search,
16- } = options
9+ const { page = DEFAULT_PAGE , limit = DEFAULT_LIMIT , userId, order } = options
1710
1811 try {
1912 let where = { }
@@ -23,33 +16,10 @@ export const getImages = async (options: IImageFilter): Promise<IListImageRespon
2316 userId : userId ,
2417 }
2518 }
26-
27- if ( search ) {
28- where = {
29- ...where ,
30- name : {
31- contains : search ,
32- mode : "insensitive" ,
33- } ,
34- }
35- }
36-
37- let orderBy = {
38- createdAt : Prisma . SortOrder . desc ,
39- }
40-
41- if ( orderByKey && order ) {
42- orderBy = {
43- ...orderBy ,
44- [ orderByKey ] : order ,
45- }
46- }
47-
4819 const [ total , data ] = await Promise . all ( [
4920 prisma . image . count ( { where } ) ,
5021 prisma . image . findMany ( {
5122 where,
52- orderBy,
5323 take : limit ,
5424 skip : ( page - 1 ) * limit ,
5525 select : imageSelect ,
@@ -103,8 +73,8 @@ export const getImage = async (id: string): Promise<IActionReturn<Image>> => {
10373}
10474
10575export const createImage = async (
106- data : Omit < Image , "id" | "createdAt" | "updatedAt" >
107- ) : Promise < IActionReturn < TImageItem > > => {
76+ data : Prisma . ImageCreateInput
77+ ) : Promise < IActionReturn < TImage > > => {
10878 try {
10979 const image = await prisma . image . create ( {
11080 data,
@@ -122,8 +92,8 @@ export const createImage = async (
12292
12393export const updateImage = async (
12494 id : string ,
125- data : Partial < Omit < Image , "id" | "createdAt" | "updatedAt" > >
126- ) : Promise < IActionReturn < TImageItem > > => {
95+ data : Prisma . ImageUpdateInput
96+ ) : Promise < IActionReturn < TImage > > => {
12797 try {
12898 const image = await prisma . image . update ( {
12999 where : { id } ,
@@ -142,7 +112,6 @@ export const updateImage = async (
142112
143113export const deleteImage = async ( id : string , userId : string ) : Promise < IActionReturn < Image > > => {
144114 try {
145- // only owner can delete
146115 const deleteImage = await prisma . image . delete ( {
147116 where : { id, userId } ,
148117 } )
0 commit comments