Skip to content
Snippets Groups Projects
Verified Commit 2ba701f8 authored by Jonas Leder's avatar Jonas Leder
Browse files

add filter function

parent 005d87a7
No related branches found
No related tags found
No related merge requests found
......@@ -20,6 +20,25 @@ $queryType = new ObjectType([
]
],
'resolve' => fn ($rootValue, $args) => products($rootValue["db"], $args["limit"], $args["offset"]),
],
"search" => [
'type' => Type::listOf($product),
"args" => [
"limit" => [
"type" => Type::int(),
"defaultValue" => 100
],
"offset" => [
"type" => Type::int(),
"defaultValue" => 0
],
"column" => [
"type" => Type::string(),
"defaultValue" => "name"
],
"search" => Type::nonNull(Type::string())
],
'resolve' => fn ($rootValue, $args) => filterProducts($rootValue["db"], $args["column"], $args["search"], $args["limit"], $args["offset"]),
]
],
]);
......@@ -25,6 +25,31 @@ function products($conn, $limit = -1, $offset = 0)
if ($limit > 0) {
$query .= " LIMIT $limit OFFSET $offset";
}
$result = $conn->query($query);
return $result->fetch_all(MYSQLI_ASSOC);
}
function filterProducts($conn, $column, $search, $limit, $offset) {
$column = $conn->real_escape_string($column);
$search = $conn->real_escape_string($search);
$limit = $conn->real_escape_string($limit);
$offset = $conn->real_escape_string($offset);
$query = "SELECT products.id,
manufacturer.id AS manufacturerid,
`name`,
amount,
position.id AS positionid,
lastUpdate
FROM products
INNER JOIN position ON positionID = position.id
INNER JOIN manufacturer ON manufacturerID = manufacturer.id
WHERE `$column` LIKE '%$search%'
ORDER BY lastUpdate DESC";
if ($limit > 0) {
$query .= " LIMIT $limit OFFSET $offset";
}
$result = $conn->query($query);
return $result->fetch_all(MYSQLI_ASSOC);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment