Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
idatg2204-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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Steffen Martinsen
idatg2204-project
Commits
2585fdea
Commit
2585fdea
authored
May 7, 2024
by
Gisli Nielsen
Browse files
Options
Downloads
Patches
Plain Diff
Added routes to get products and added comments
parent
190e05d3
No related branches found
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
Backend/main.py
+18
-0
18 additions, 0 deletions
Backend/main.py
Backend/routes/category.py
+6
-0
6 additions, 0 deletions
Backend/routes/category.py
Backend/routes/product.py
+51
-0
51 additions, 0 deletions
Backend/routes/product.py
Backend/utils/helpers.py
+13
-0
13 additions, 0 deletions
Backend/utils/helpers.py
with
88 additions
and
0 deletions
Backend/main.py
+
18
−
0
View file @
2585fdea
...
...
@@ -4,8 +4,11 @@ from routes.category import get_category
from
routes.home
import
get_home
#from routes.cart import get_cart
from
routes.login
import
post_login
,
post_logout
,
post_register
from
routes.product
import
get_product_by_id
,
get_product_all
,
get_products_by_search
# Routing
# Routing for homepage
@app.route
(
'
/
'
,
methods
=
[
'
GET
'
])
def
home
():
return
get_home
()
...
...
@@ -14,6 +17,7 @@ def home():
#def order(order_id):
# return get_order(order_id)
# Routes for getting the different categories
@app.route
(
'
/category/
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/category/<string:category_name>
'
,
methods
=
[
'
GET
'
])
def
category
(
category_name
=
None
):
...
...
@@ -35,5 +39,19 @@ def login():
def
register
():
return
post_register
()
# Route for one specific product, or product page
@app.route
(
'
/product/<int:product_id>
'
,
methods
=
[
'
GET
'
])
def
get_product
(
product_id
):
return
get_product_by_id
(
product_id
)
# Route for getting all products
@app.route
(
'
/product/
'
,
methods
=
[
'
GET
'
])
def
get_products
():
return
get_product_all
()
@app.route
(
'
/product/search/<string:search>
'
,
methods
=
[
'
GET
'
])
def
search_products
(
search
):
return
get_products_by_search
(
search
)
if
__name__
==
'
__main__
'
:
app
.
run
(
debug
=
True
,
port
=
8080
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Backend/routes/category.py
+
6
−
0
View file @
2585fdea
from
utils.application
import
mysql
from
flask
import
jsonify
from
utils.helpers
import
sql_product_to_json
def
get_category
(
category_name
=
None
):
# If category_name is not None, get one category by name
if
(
category_name
is
not
None
):
return
get_category_by_name
(
category_name
)
else
:
# Construct the query to get the category
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
...
...
@@ -44,6 +46,7 @@ def get_category(category_name=None):
def
get_category_by_name
(
category_name
):
# Get the category by a name
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
...
...
@@ -54,6 +57,7 @@ def get_category_by_name(category_name):
categoryData
=
cur
.
fetchall
()
# Get all of the products for that one category.
cur
.
execute
(
'''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
INNER JOIN category ON category.category_id = product.category_id
...
...
@@ -62,6 +66,7 @@ def get_category_by_name(category_name):
productData
=
cur
.
fetchall
()
cur
.
close
()
# Make sure we have a category
if
len
(
categoryData
)
==
0
:
return
jsonify
({
"
error
"
:
"
Category not found
"
}),
404
...
...
@@ -84,6 +89,7 @@ def get_category_by_name(category_name):
for
row
in
productData
:
# Extract product-info for each product
productId
,
productName
,
productDescription
,
productPrice
,
productStockQuantity
=
row
category
[
"
products
"
].
append
({
"
productId
"
:
productId
,
...
...
This diff is collapsed.
Click to expand it.
Backend/routes/product.py
0 → 100644
+
51
−
0
View file @
2585fdea
from
utils.application
import
mysql
from
flask
import
jsonify
from
utils.helpers
import
sql_product_to_json
def
get_product_by_id
(
product_id
):
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
WHERE product.product_id = %s
'''
,
(
product_id
,))
products
=
cur
.
fetchall
()
cur
.
close
()
if
len
(
products
)
<=
0
:
return
jsonify
({
"
message
"
:
"
Product not found
"
}),
404
if
len
(
products
)
>
1
:
return
jsonify
({
"
message
"
:
"
Database error
"
}),
500
jsonProduct
=
sql_product_to_json
(
products
)[
0
]
return
jsonify
(
jsonProduct
)
def
get_product_all
():
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
'''
)
products
=
cur
.
fetchall
()
cur
.
close
()
jsonProducts
=
sql_product_to_json
(
products
)
return
jsonProducts
def
get_products_by_search
(
search
):
cur
=
mysql
.
connection
.
cursor
()
# Search in product name and description
cur
.
execute
(
'''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
WHERE product.name LIKE %s OR product.description LIKE %s
'''
,
(
'
%
'
+
search
+
'
%
'
,
'
%
'
+
search
+
'
%
'
))
products
=
cur
.
fetchall
()
cur
.
close
()
jsonProducts
=
sql_product_to_json
(
products
)
return
jsonProducts
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Backend/utils/helpers.py
0 → 100644
+
13
−
0
View file @
2585fdea
def
sql_product_to_json
(
products
):
jsonProducts
=
[]
for
product
in
products
:
product_id
,
name
,
description
,
price
,
stock_quantity
=
product
jsonProducts
.
append
({
"
product_id
"
:
product_id
,
"
name
"
:
name
,
"
description
"
:
description
,
"
price
"
:
price
,
"
stock_quantity
"
:
stock_quantity
})
return
jsonProducts
\ No newline at end of file
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