update sentences API
All checks were successful
Deploy to Production / deploy (push) Successful in 22s
All checks were successful
Deploy to Production / deploy (push) Successful in 22s
This commit is contained in:
@@ -387,6 +387,97 @@ class ContextController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search contexts with partial match on title/context + filter by type_image
|
||||
*
|
||||
* POST /api/contexts/search
|
||||
* Body:
|
||||
* {
|
||||
* search : String - tìm kiếm một phần trong title HOẶC context (OR)
|
||||
* title : String - tìm riêng trong title (LIKE)
|
||||
* context_text : String - tìm riêng trong context (LIKE)
|
||||
* type_image : String - exact match (e.g., 'small', 'square', 'normal')
|
||||
* type : String - exact match
|
||||
* status : Number - exact match
|
||||
* grade : Number - exact match
|
||||
* page : Number (default: 1)
|
||||
* limit : Number (default: 50)
|
||||
* }
|
||||
*/
|
||||
async searchContexts(req, res, next) {
|
||||
try {
|
||||
const {
|
||||
search,
|
||||
title,
|
||||
context_text,
|
||||
type_image,
|
||||
type,
|
||||
status,
|
||||
grade,
|
||||
page = 1,
|
||||
limit = 50
|
||||
} = req.body;
|
||||
|
||||
const { Op } = require('sequelize');
|
||||
const offset = (page - 1) * limit;
|
||||
const where = {};
|
||||
|
||||
// ── Exact-match filters ──────────────────────────────────────────────
|
||||
if (type_image !== undefined && type_image !== null && type_image !== '') {
|
||||
where.type_image = type_image;
|
||||
}
|
||||
if (type) where.type = type;
|
||||
if (status !== undefined && status !== null) where.status = parseInt(status);
|
||||
if (grade !== undefined && grade !== null) where.grade = parseInt(grade);
|
||||
|
||||
// ── Text search ──────────────────────────────────────────────────────
|
||||
// `search` → title OR context (cả hai cùng lúc)
|
||||
// `title` → chỉ title
|
||||
// `context_text` → chỉ context
|
||||
const textConditions = [];
|
||||
|
||||
if (search) {
|
||||
textConditions.push(
|
||||
{ title: { [Op.like]: `%${search}%` } },
|
||||
{ context: { [Op.like]: `%${search}%` } }
|
||||
);
|
||||
}
|
||||
if (title) {
|
||||
textConditions.push({ title: { [Op.like]: `%${title}%` } });
|
||||
}
|
||||
if (context_text) {
|
||||
textConditions.push({ context: { [Op.like]: `%${context_text}%` } });
|
||||
}
|
||||
|
||||
if (textConditions.length > 0) {
|
||||
where[Op.or] = textConditions;
|
||||
}
|
||||
|
||||
const { count, rows } = await Context.findAndCountAll({
|
||||
where,
|
||||
limit: parseInt(limit),
|
||||
offset: parseInt(offset),
|
||||
order: [['created_at', 'DESC']]
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Search completed successfully',
|
||||
data: {
|
||||
contexts: rows,
|
||||
pagination: {
|
||||
total: count,
|
||||
page: parseInt(page),
|
||||
limit: parseInt(limit),
|
||||
totalPages: Math.ceil(count / limit)
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update context (general update - use with caution)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user