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
6d82378f
Commit
6d82378f
authored
1 year ago
by
Torbjørn Halland
Browse files
Options
Downloads
Plain Diff
Merge branch 'main' of git.gvk.idi.ntnu.no:steffemar/idatg2204-project
parents
cfdffb5f
11a168ce
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Backend/main.py
+8
-8
8 additions, 8 deletions
Backend/main.py
Backend/routes/category.py
+89
-10
89 additions, 10 deletions
Backend/routes/category.py
with
97 additions
and
18 deletions
Backend/main.py
+
8
−
8
View file @
6d82378f
from
utils.application
import
app
from
utils.application
import
app
#
from routes.category import get_category
from
routes.category
import
get_category
#from routes.order import get_order
#from routes.order import get_order
from
routes.home
import
get_home
from
routes.home
import
get_home
#from routes.cart import get_cart
#from routes.cart import get_cart
...
@@ -14,24 +14,24 @@ def home():
...
@@ -14,24 +14,24 @@ def home():
#def order(order_id):
#def order(order_id):
# return get_order(order_id)
# return get_order(order_id)
#
@app.route('/category', methods=['GET'])
@app.route
(
'
/category
/
'
,
methods
=
[
'
GET
'
])
#
@app.route('/category/<string:category_name>', methods=['GET'])
@app.route
(
'
/category/<string:category_name>
'
,
methods
=
[
'
GET
'
])
#
def category(category_name):
def
category
(
category_name
=
None
):
#
return
routes.category.
get_category(category_name)
return
get_category
(
category_name
)
#@app.route('/cart', methods=['GET'])
#@app.route('/cart', methods=['GET'])
#def cart():
#def cart():
# return get_cart()
# return get_cart()
@app.route
(
'
/logout
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/logout
/
'
,
methods
=
[
'
POST
'
])
def
logout
():
def
logout
():
return
post_logout
()
return
post_logout
()
@app.route
(
'
/login
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/login
/
'
,
methods
=
[
'
POST
'
])
def
login
():
def
login
():
return
post_login
()
return
post_login
()
@app.route
(
'
/register
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/register
/
'
,
methods
=
[
'
POST
'
])
def
register
():
def
register
():
return
post_register
()
return
post_register
()
...
...
This diff is collapsed.
Click to expand it.
Backend/routes/category.py
+
89
−
10
View file @
6d82378f
from
mai
n
import
mysql
from
utils.applicatio
n
import
mysql
from
flask
import
jsonify
from
flask
import
jsonify
def
get_category
(
category_name
=
None
):
def
get_category
(
category_name
=
None
):
if
category_name
is
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
:
cur
=
mysql
.
connection
.
cursor
()
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT category.name FROM category
'''
)
cur
.
execute
(
'''
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
LEFT JOIN sub_category_in_category ON sub_category_in_category.parent_category_id = category.category_id
LEFT JOIN category AS sub_category ON sub_category.category_id = sub_category_in_category.sub_category_id
'''
)
data
=
cur
.
fetchall
()
data
=
cur
.
fetchall
()
cur
.
close
()
cur
.
close
()
return
jsonify
(
data
)
else
:
# If no data is found, return an empty list
categories
=
[]
for
row
in
data
:
#Extract data from row
categoryName
,
categoryDescription
,
subCategoryName
,
subCategoryDescription
=
row
# Check if category exists already in category
category
=
next
((
x
for
x
in
categories
if
x
[
"
categoryName
"
]
==
categoryName
),
None
)
# If it does not exist, create it
if
category
is
None
:
category
=
{
"
categoryName
"
:
categoryName
,
"
categoryDescription
"
:
categoryDescription
,
"
subCategories
"
:
[]
}
categories
.
append
(
category
)
#Always append subcategories extracted from row
if
(
subCategoryName
is
not
None
):
category
[
"
subCategories
"
].
append
({
"
subCategoryName
"
:
subCategoryName
,
"
subCategoryDescription
"
:
subCategoryDescription
})
return
jsonify
(
categories
)
def
get_category_by_name
(
category_name
):
cur
=
mysql
.
connection
.
cursor
()
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT category.name FROM category WHERE name = %s
'''
,
(
category_name
,))
cur
.
execute
(
'''
data
=
cur
.
fetchall
()
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
LEFT JOIN sub_category_in_category ON sub_category_in_category.parent_category_id = category.category_id
LEFT JOIN category AS sub_category ON sub_category.category_id = sub_category_in_category.sub_category_id
WHERE category.name = %s
'''
,
(
category_name
,))
categoryData
=
cur
.
fetchall
()
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
WHERE category.name = %s
'''
,
(
category_name
,))
productData
=
cur
.
fetchall
()
cur
.
close
()
cur
.
close
()
return
jsonify
(
data
)
\ No newline at end of file
if
len
(
categoryData
)
==
0
:
return
jsonify
({
"
error
"
:
"
Category not found
"
}),
404
# Extract data from first row to create category object
categoryName
,
categoryDescription
,
subCategoryName
,
subCategoryDescription
=
categoryData
[
0
]
category
=
{
"
categoryName
"
:
categoryName
,
"
categoryDescription
"
:
categoryDescription
,
"
subCategories
"
:
[],
"
products
"
:
[]
}
for
row
in
categoryData
:
#Extract data from row to create subcategory object
categoryName
,
categoryDescription
,
subCategoryName
,
subCategoryDescription
=
row
if
(
subCategoryName
is
not
None
):
category
[
"
subCategories
"
].
append
({
"
subCategoryName
"
:
subCategoryName
,
"
subCategoryDescription
"
:
subCategoryDescription
})
for
row
in
productData
:
productId
,
productName
,
productDescription
,
productPrice
,
productStockQuantity
=
row
category
[
"
products
"
].
append
({
"
productId
"
:
productId
,
"
productName
"
:
productName
,
"
productDescription
"
:
productDescription
,
"
productPrice
"
:
productPrice
,
"
productStockQuantity
"
:
productStockQuantity
})
return
jsonify
(
category
)
\ 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