1- import { Image } from "@prisma/client"
1+ import { Image , Prisma } from "@prisma/client"
22
33import prisma from "../prisma"
4- import { DEFAULT_LIMIT , DEFAULT_PAGE , IActionReturn , IGetListResponse } from "../shared/type"
5- import { imageSelect } 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
17-
18- console . log ( ">>>options" , options )
9+ const { page = DEFAULT_PAGE , limit = DEFAULT_LIMIT , userId, order } = options
1910
2011 try {
2112 let where = { }
@@ -25,33 +16,10 @@ export const getImages = async (options: IImageFilter): Promise<IListImageRespon
2516 userId : userId ,
2617 }
2718 }
28-
29- if ( search ) {
30- where = {
31- ...where ,
32- name : {
33- contains : search ,
34- mode : "insensitive" ,
35- } ,
36- }
37- }
38-
39- let orderBy = {
40- createdAt : "desc" ,
41- }
42-
43- if ( orderByKey && order ) {
44- orderBy = {
45- ...orderBy ,
46- [ orderByKey ] : order ,
47- }
48- }
49-
5019 const [ total , data ] = await Promise . all ( [
5120 prisma . image . count ( { where } ) ,
5221 prisma . image . findMany ( {
5322 where,
54- orderBy,
5523 take : limit ,
5624 skip : ( page - 1 ) * limit ,
5725 select : imageSelect ,
@@ -88,6 +56,12 @@ export const getImage = async (id: string): Promise<IActionReturn<Image>> => {
8856 select : imageSelect ,
8957 } )
9058
59+ if ( ! image ) {
60+ return {
61+ error : "NOT_FOUND" ,
62+ }
63+ }
64+
9165 return {
9266 data : image ,
9367 }
@@ -99,8 +73,8 @@ export const getImage = async (id: string): Promise<IActionReturn<Image>> => {
9973}
10074
10175export const createImage = async (
102- data : Omit < Image , "id" | "createdAt" | "updatedAt" >
103- ) : Promise < IActionReturn < Image > > => {
76+ data : Prisma . ImageCreateInput
77+ ) : Promise < IActionReturn < TImage > > => {
10478 try {
10579 const image = await prisma . image . create ( {
10680 data,
@@ -118,8 +92,8 @@ export const createImage = async (
11892
11993export const updateImage = async (
12094 id : string ,
121- data : Partial < Omit < Image , "id" | "createdAt" | "updatedAt" > >
122- ) : Promise < IActionReturn < Image > > => {
95+ data : Prisma . ImageUpdateInput
96+ ) : Promise < IActionReturn < TImage > > => {
12397 try {
12498 const image = await prisma . image . update ( {
12599 where : { id } ,
@@ -136,16 +110,15 @@ export const updateImage = async (
136110 }
137111}
138112
139- export const deleteImage = async ( id : string , userId : string ) : Promise < IActionReturn < "" > > => {
113+ export const deleteImage = async ( id : string , userId : string ) : Promise < IActionReturn < Image > > => {
140114 try {
141- // only owner can delete
142115 const deleteImage = await prisma . image . delete ( {
143116 where : { id, userId } ,
144117 } )
145118
146119 if ( ! deleteImage ) {
147120 return {
148- error : "Unauthorized " ,
121+ error : "UNAUTHORIZED " ,
149122 }
150123 }
151124
0 commit comments