Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
Database_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Phrot Vedal
Database_project
Commits
2f9c6f00
Commit
2f9c6f00
authored
1 year ago
by
Tiago Brito
Browse files
Options
Downloads
Patches
Plain Diff
Created get_products_by_category and search_products routes in product.py
parent
5edaf5e0
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
backend/product.py
+58
-0
58 additions, 0 deletions
backend/product.py
with
58 additions
and
0 deletions
backend/product.py
+
58
−
0
View file @
2f9c6f00
...
...
@@ -60,6 +60,64 @@ def get_products(product_id):
cursor
.
close
()
conn
.
close
()
@product_bp.route
(
'
/products/category/<string:category_name>
'
,
methods
=
[
'
GET
'
])
def
get_products_by_category
(
category_name
):
conn
=
db_connection
()
cursor
=
conn
.
cursor
(
dictionary
=
True
)
try
:
cursor
.
execute
(
"""
SELECT p.*, b.Name as BrandName, c.Name as CategoryName
FROM Product p
JOIN Category c ON p.CategoryID = c.CategoryID
JOIN Brand b ON p.BrandID = b.BrandID
WHERE c.Name = %s
"""
,
(
category_name
,))
products
=
cursor
.
fetchall
()
return
jsonify
(
products
),
200
except
Exception
as
e
:
print
(
e
)
return
jsonify
({
'
error
'
:
'
Database connection failed
'
}),
500
finally
:
cursor
.
close
()
conn
.
close
()
# New route for searching products by name or brand
@product_bp.route
(
'
/products/search
'
,
methods
=
[
'
GET
'
])
def
search_products
():
search_query
=
request
.
args
.
get
(
'
query
'
,
default
=
""
,
type
=
str
).
strip
()
selected_brand
=
request
.
args
.
get
(
'
brand
'
,
default
=
"
All Brands
"
,
type
=
str
).
strip
()
if
not
search_query
and
selected_brand
==
"
All Brands
"
:
return
jsonify
({
'
message
'
:
'
No search criteria provided
'
}),
400
conn
=
db_connection
()
if
not
conn
:
return
jsonify
({
'
error
'
:
'
Database connection failed
'
}),
500
cursor
=
conn
.
cursor
(
dictionary
=
True
)
try
:
sql_query
=
"""
SELECT p.*, b.Name AS BrandName, c.Name AS CategoryName
FROM Product p
JOIN Brand b ON p.BrandID = b.BrandID
JOIN Category c ON p.CategoryID = c.CategoryID
WHERE (p.Name LIKE %s OR b.Name LIKE %s)
"""
sql_params
=
[
'
%
'
+
search_query
+
'
%
'
,
'
%
'
+
search_query
+
'
%
'
]
# Filter by selected brand if not 'All Brands'
if
selected_brand
!=
"
All Brands
"
:
sql_query
+=
"
AND b.Name = %s
"
sql_params
.
append
(
selected_brand
)
cursor
.
execute
(
sql_query
,
sql_params
)
products
=
cursor
.
fetchall
()
return
jsonify
(
products
),
200
except
Exception
as
e
:
return
jsonify
({
'
error
'
:
'
Error processing your request
'
,
'
details
'
:
str
(
e
)}),
500
finally
:
cursor
.
close
()
conn
.
close
()
@product_bp.route
(
'
/products/<int:product_id>
'
,
methods
=
[
'
PUT
'
])
def
update_product
(
product_id
):
data
=
request
.
json
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment