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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Steffen Martinsen
idatg2204-project
Commits
0381bb41
Commit
0381bb41
authored
1 year ago
by
Gisli Nielsen
Browse files
Options
Downloads
Patches
Plain Diff
Added comments
parent
788bd077
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Backend/routes/product.py
+8
-1
8 additions, 1 deletion
Backend/routes/product.py
with
8 additions
and
1 deletion
Backend/routes/product.py
+
8
−
1
View file @
0381bb41
...
@@ -3,26 +3,32 @@ from flask import jsonify
...
@@ -3,26 +3,32 @@ from flask import jsonify
from
utils.helpers
import
sql_product_to_json
from
utils.helpers
import
sql_product_to_json
def
get_product_by_id
(
product_id
):
def
get_product_by_id
(
product_id
):
# Get the specific product ID
cur
=
mysql
.
connection
.
cursor
()
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
cur
.
execute
(
'''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity, brand.name, brand.description
FROM product INNER JOIN brand ON product.brand_id = brand.brand_id
WHERE product.product_id = %s
WHERE product.product_id = %s
'''
,
(
product_id
,))
'''
,
(
product_id
,))
products
=
cur
.
fetchall
()
products
=
cur
.
fetchall
()
cur
.
close
()
cur
.
close
()
# Check that the product exists
if
len
(
products
)
<=
0
:
if
len
(
products
)
<=
0
:
return
jsonify
({
"
message
"
:
"
Product not found
"
}),
404
return
jsonify
({
"
message
"
:
"
Product not found
"
}),
404
# Check that we only have one product.
if
len
(
products
)
>
1
:
if
len
(
products
)
>
1
:
return
jsonify
({
"
message
"
:
"
Database error
"
}),
500
return
jsonify
({
"
message
"
:
"
Database error
"
}),
500
# Use function to turn it into a json style object
jsonProduct
=
sql_product_to_json
(
products
)[
0
]
jsonProduct
=
sql_product_to_json
(
products
)[
0
]
return
jsonify
(
jsonProduct
)
return
jsonify
(
jsonProduct
)
def
get_product_all
():
def
get_product_all
():
# Get all the products
cur
=
mysql
.
connection
.
cursor
()
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
cur
.
execute
(
'''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
...
@@ -31,6 +37,7 @@ def get_product_all():
...
@@ -31,6 +37,7 @@ def get_product_all():
products
=
cur
.
fetchall
()
products
=
cur
.
fetchall
()
cur
.
close
()
cur
.
close
()
# Turn them all into json products
jsonProducts
=
sql_product_to_json
(
products
)
jsonProducts
=
sql_product_to_json
(
products
)
return
jsonProducts
return
jsonProducts
...
...
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