From d7e43b2302a19fc2df381d938070700f60c3ffaa Mon Sep 17 00:00:00 2001 From: Azat Kashapov Date: Fri, 19 Jan 2024 14:36:49 +0100 Subject: [PATCH] rework getallstorageimages --- .gitignore | 1 + src/services/imageService.ts | 28 ++++++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index a6167d8..ea050b4 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,4 @@ dist +docker-compose.yml diff --git a/src/services/imageService.ts b/src/services/imageService.ts index 32fca17..c58330e 100644 --- a/src/services/imageService.ts +++ b/src/services/imageService.ts @@ -77,7 +77,9 @@ async function viewImage(id: number) { } async function viewImageByName(bookID: number, imageName: string) { - const image = await prisma.image.findFirst({ where: { book_id: bookID, file_name: imageName } }); + const image = await prisma.image.findFirst({ + where: { book_id: bookID, file_name: imageName }, + }); if (image) { return image; } @@ -151,17 +153,27 @@ async function searchImage(params: QueryImages) { } async function totalImageBooks() { - // const result = await prisma.image.groupBy({ - // by: ['book_id'], - // _count: { - // book_id: true, - // }, - // }); + // return result.map((item) => ({ // book_id: item.book_id, // images_count: item._count.book_id, // })); - return await getAllStorageImages() + const storageImages = await getAllStorageImages(); + const books = await prisma.book.findMany({ + select: { + id: true, + name: true, + }, + where: { + id: { in: storageImages.map((item) => item.bookID) }, + }, + }); + const result = storageImages.map(item => { + const book = books.find(elem => elem.id === item.bookID) + return {bookID: item.bookID, bookName: book?.name, images: item.images} + }) + + return result; } async function removeImage(id: number) {