Commit 294fe7dc authored by Phạm Quang Bảo's avatar Phạm Quang Bảo

feat(challenge_8): add operator

parent 66487b83
...@@ -19,7 +19,7 @@ export default function queryModifier(req: Req, _res: Res, next: NextFunction) { ...@@ -19,7 +19,7 @@ export default function queryModifier(req: Req, _res: Res, next: NextFunction) {
arrFilters.forEach((element) => { arrFilters.forEach((element) => {
// Mỗi phần tử sẽ có dạng: "status == published" hoặc "title @= NodeJS" // Mỗi phần tử sẽ có dạng: "status == published" hoặc "title @= NodeJS"
const trimmedElement = element.trim(); const trimmedElement = element.trim();
const parts = trimmedElement.match(/^(\S+)\s*(==|@=|>=|<=|>|<)\s*(.+)$/i); const parts = trimmedElement.match(/^(\S+)\s*(==|@=|>=|<=|>|<|\||&)\s*(.+)$/i);
// Phải đủ 3 thành phần: [Tên cột, Toán tử, Giá trị] // Phải đủ 3 thành phần: [Tên cột, Toán tử, Giá trị]
if (parts) { if (parts) {
...@@ -53,6 +53,18 @@ export default function queryModifier(req: Req, _res: Res, next: NextFunction) { ...@@ -53,6 +53,18 @@ export default function queryModifier(req: Req, _res: Res, next: NextFunction) {
case "<=": case "<=":
condition[field] = { [Op.lte]: value }; condition[field] = { [Op.lte]: value };
break; break;
case "|":
const orValues = value.split("/").map(v => v.trim());
condition[field] = { [Op.or]: orValues };
break;
case "&":
const andValues = value.split("/").map(v => v.trim());
const andConditions = andValues.map(v => ({
[Op.iLike]: `%${v}%`
}));
condition[field] = { [Op.and]: andConditions };
break;
default: default:
condition[field] = { [Op.eq]: value }; condition[field] = { [Op.eq]: value };
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment