Skip to content

Commit

Permalink
fix filter and sort books
Browse files Browse the repository at this point in the history
  • Loading branch information
ufaboy committed Jan 26, 2024
1 parent b3c9aa1 commit cd716bb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ FROM node:20-alpine as dev-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npx prisma generate
USER root
RUN apk add --no-cache mc mysql-client
Expand Down
19 changes: 13 additions & 6 deletions src/services/bookService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Book, BaseBook, QueryBooks, BookFromDB } from '../types/book.js';
import { BookTagShrink, Tag } from '../types/tag.js';

const prisma = new PrismaClient({
// log: ['query'],
log: ['query'],
});

async function createBook(data: BaseBook) {
Expand Down Expand Up @@ -231,7 +231,8 @@ async function searchBook(params: QueryBooks) {
},
});
}
const whereQuery = whereConditions.length ? { OR: whereConditions } : {};
const whereQuery = whereConditions.length ? { AND: whereConditions } : {};
console.log('whereQuery', whereQuery)
const books = await prisma.book.findMany({
select: {
id: true,
Expand All @@ -248,16 +249,22 @@ async function searchBook(params: QueryBooks) {
last_read: true,
book_tag: {
select: {

tag: true,
},
},
},
where: whereQuery,
orderBy: [
{
[sort]: sortWay,
},
{
id: 'desc'
}
],
skip: (page - 1) * perPage,
take: Number(perPage),
orderBy: {
[sort]: sortWay,
},
});
const total = await prisma.book.count({ where: whereQuery });
return {
Expand Down Expand Up @@ -325,7 +332,7 @@ function prepareBook(book: BookFromDB): Book {
: [],
text_length,
view_count,
updated_at,
updated_at: updated_at ?? new Date(),
last_read,
tags: convertTags(book.book_tag),
};
Expand Down
2 changes: 1 addition & 1 deletion src/types/book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ interface BookFromDB {
image?: Array<ImageFromDB> | null;
text_length: number | null;
view_count: number;
updated_at: Date;
updated_at: Date | null;
last_read: Date | null;
}

Expand Down

0 comments on commit cd716bb

Please sign in to comment.