Добавлен поиск статей по дате публикации.
This commit is contained in:
parent
810599c718
commit
d9ab710be4
@ -4,9 +4,24 @@ import { AuthorRole, Prisma } from '@prisma/client';
|
||||
|
||||
export async function searchArticles(req: Request, res: Response) {
|
||||
try {
|
||||
const { q, author, role, page = 1, limit = 9 } = req.query;
|
||||
const { q, author, role, date, page = 1, limit = 9 } = req.query;
|
||||
const skip = ((Number(page) || 1) - 1) * (Number(limit) || 9);
|
||||
|
||||
// Подготовка условия по дате
|
||||
let dateCondition: Prisma.ArticleWhereInput = {};
|
||||
if (typeof date === 'string' && date.trim()) {
|
||||
const selectedDate = new Date(date);
|
||||
const nextDate = new Date(selectedDate);
|
||||
nextDate.setDate(selectedDate.getDate() + 1);
|
||||
|
||||
dateCondition = {
|
||||
publishedAt: {
|
||||
gte: selectedDate,
|
||||
lt: nextDate,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Формируем where-условие
|
||||
const where: Prisma.ArticleWhereInput = {
|
||||
...(typeof q === 'string' && q.trim()
|
||||
@ -30,6 +45,7 @@ export async function searchArticles(req: Request, res: Response) {
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
...dateCondition,
|
||||
};
|
||||
|
||||
// Выполнение запроса
|
||||
|
Loading…
x
Reference in New Issue
Block a user