-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.js
More file actions
158 lines (126 loc) · 4.35 KB
/
string.js
File metadata and controls
158 lines (126 loc) · 4.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import StringModel from "../models/string.js";
import { StatusCodes } from "http-status-codes";
import {
ConflictError,
NotFoundError,
BadRequestError,
UnprocessableEntityError,
} from "../errors/index.js";
const findStringOrFail = async value => {
const string = await StringModel.findOne({ value: value.toLowerCase() });
if (!string) throw new NotFoundError("String does not exist in the system!");
return string;
};
export const getStringByNL = async (req, res) => {
const original = req.query.query;
const cleaned = original.replace(/[^\w\s]/g, "");
const words = cleaned.trim().split(" ");
const keywords = [
"word",
"than",
"words",
"contain",
"containing",
"palindrome",
"palindromic",
];
const hasNoKeyword = words.every(
word => !keywords.includes(word.toLowerCase())
);
if (!words.length || hasNoKeyword)
throw new BadRequestError("Unable to parse natural language query!");
const filters = {};
const queryObjects = {};
if (words.includes("palindrome") || words.includes("palindromic")) {
filters.palindrome = !words.includes("not");
queryObjects["properties.is_palindrome"] = !words.includes("not");
}
if (words.includes("word") || words.includes("words")) {
if (words.includes("single")) {
filters.word_count = 1;
} else if (words.includes("double")) {
filters.word_count = 2;
} else if (words.includes("triple")) {
filters.word_count = 3;
}
queryObjects["properties.word_count"] = filters.word_count;
}
if (words.includes("than")) {
const longerIndex = words.indexOf("longer");
const shorterIndex = words.indexOf("shorter");
if (longerIndex !== -1) {
filters.min_length = Number(words[longerIndex + 2]) + 1;
queryObjects["properties.length"] = { $gte: filters.min_length };
}
if (shorterIndex !== -1) {
filters.max_length = Number(words[shorterIndex + 2]) - 1;
queryObjects["properties.length"] = {
...queryObjects["properties.length"],
$lte: filters.max_length,
};
}
}
if (words.includes("contain") || words.includes("containing")) {
const letter = words[words.indexOf("letter") + 1];
filters.contains_character = letter;
queryObjects.value = { $regex: letter, $options: "i" };
}
console.log("queryObjects:", queryObjects);
const data = await StringModel.find(queryObjects).sort("created_at");
res.status(StatusCodes.OK).json({
data,
count: data.length,
interpreted_query: { original, parsed_filters: filters },
});
};
export const getStrings = async (req, res) => {
const queryObjects = {};
const {
min_length,
max_length,
word_count,
is_palindrome,
contains_character,
} = req.query;
if (min_length)
queryObjects["properties.length"] = { $gte: Number(min_length) };
if (max_length)
queryObjects["properties.length"] = {
...queryObjects["properties.length"],
$lte: Number(max_length),
};
if (word_count) queryObjects["properties.word_count"] = Number(word_count);
if (is_palindrome)
queryObjects["properties.is_palindrome"] = is_palindrome === "true";
if (contains_character)
queryObjects.value = { $regex: contains_character, $options: "i" };
const data = await StringModel.find(queryObjects).sort("created_at");
res
.status(StatusCodes.OK)
.json({ data, count: data.length, filters_applied: req.query });
};
export const createString = async (req, res) => {
let { value } = req.body;
if (typeof value !== "string") {
throw new UnprocessableEntityError(
'Invalid data type for "value" (must be string)'
);
}
if (!value.trim() || !isNaN(value))
throw new BadRequestError('Invalid request body or missing "value" field');
value = value.toLowerCase();
const existingValue = await StringModel.findOne({ value });
if (existingValue)
throw new ConflictError("String already exists in the system!");
const string = await StringModel.create({ value });
res.status(StatusCodes.CREATED).json(string);
};
export const getString = async (req, res) => {
const string = await findStringOrFail(req.params.value);
res.status(StatusCodes.OK).json(string);
};
export const deleteString = async (req, res) => {
const string = await findStringOrFail(req.params.value);
await StringModel.deleteOne({ _id: string._id });
res.status(StatusCodes.NO_CONTENT).send();
};