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
a55010f2
Commit
a55010f2
authored
1 year ago
by
Gisli
Browse files
Options
Downloads
Patches
Plain Diff
Added a category route
parent
618a3818
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide 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
+71
-10
71 additions, 10 deletions
Backend/routes/category.py
with
79 additions
and
18 deletions
Backend/main.py
+
8
−
8
View file @
a55010f2
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.home
import
get_home
#from routes.cart import get_cart
...
...
@@ -14,24 +14,24 @@ def home():
#def order(order_id):
# return get_order(order_id)
#
@app.route('/category', methods=['GET'])
#
@app.route('/category/<string:category_name>', methods=['GET'])
#
def category(category_name):
#
return
routes.category.
get_category(category_name)
@app.route
(
'
/category
/
'
,
methods
=
[
'
GET
'
])
@app.route
(
'
/category/<string:category_name>
'
,
methods
=
[
'
GET
'
])
def
category
(
category_name
=
None
):
return
get_category
(
category_name
)
#@app.route('/cart', methods=['GET'])
#def cart():
# return get_cart()
@app.route
(
'
/logout
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/logout
/
'
,
methods
=
[
'
POST
'
])
def
logout
():
return
post_logout
()
@app.route
(
'
/login
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/login
/
'
,
methods
=
[
'
POST
'
])
def
login
():
return
post_login
()
@app.route
(
'
/register
'
,
methods
=
[
'
POST
'
])
@app.route
(
'
/register
/
'
,
methods
=
[
'
POST
'
])
def
register
():
return
post_register
()
...
...
This diff is collapsed.
Click to expand it.
Backend/routes/category.py
+
71
−
10
View file @
a55010f2
from
mai
n
import
mysql
from
utils.applicatio
n
import
mysql
from
flask
import
jsonify
def
get_category
(
category_name
=
None
):
if
category_name
is
None
:
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT category.name FROM category
'''
)
data
=
cur
.
fetchall
()
cur
.
close
()
return
jsonify
(
data
)
# 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
.
execute
(
'''
SELECT category.name FROM category WHERE name = %s
'''
,
(
category_name
,))
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
()
cur
.
close
()
return
jsonify
(
data
)
\ No newline at end of file
# 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
.
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
WHERE category.name = %s
'''
,
(
category_name
,))
data
=
cur
.
fetchall
()
cur
.
close
()
if
len
(
data
)
==
0
:
return
jsonify
({
"
error
"
:
"
Category not found
"
}),
404
# Extract data from first row to create category object
categoryName
,
categoryDescription
,
subCategoryName
,
subCategoryDescription
=
data
[
0
]
category
=
{
"
categoryName
"
:
categoryName
,
"
categoryDescription
"
:
categoryDescription
,
"
subCategories
"
:
[]
}
for
row
in
data
:
#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
})
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