Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PizzaPalace
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
TheJoeCoder
PizzaPalace
Commits
87e2f77a
Verified
Commit
87e2f77a
authored
Jun 13, 2023
by
TheJoeCoder
Browse files
Options
Downloads
Patches
Plain Diff
Add add to cart functionality
parent
fd317caf
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/app.py
+76
-2
76 additions, 2 deletions
server/app.py
with
76 additions
and
2 deletions
server/app.py
+
76
−
2
View file @
87e2f77a
...
@@ -12,6 +12,15 @@ with open("pizza_types.json","r") as f:
...
@@ -12,6 +12,15 @@ with open("pizza_types.json","r") as f:
open_carts
=
{}
open_carts
=
{}
def
get_cart
(
uid
,
key
):
if
uid
==
None
or
key
==
None
:
return
None
if
uid
not
in
open_carts
:
return
None
if
open_carts
[
uid
][
"
key
"
]
!=
key
:
return
None
return
open_carts
[
uid
]
@app.route
(
"
/
"
)
@app.route
(
"
/
"
)
def
hello_world
():
def
hello_world
():
# Return OK message
# Return OK message
...
@@ -30,9 +39,74 @@ def open_cart():
...
@@ -30,9 +39,74 @@ def open_cart():
cart_id
=
str
(
uuid
.
uuid4
())
cart_id
=
str
(
uuid
.
uuid4
())
if
cart_id
in
open_carts
:
if
cart_id
in
open_carts
:
logger
.
warn
(
"
Regenerating UUID for new cart - prev
"
+
cart_id
)
logger
.
warn
(
"
Regenerating UUID for new cart - prev
"
+
cart_id
)
else
:
foundunique
=
True
cart_key
=
str
(
uuid
.
uuid4
())
cart_key
=
str
(
uuid
.
uuid4
())
# TODO append cart to list and send to user
# Append cart to list of open carts
return
"
{
\"
status
\"
:200,
\"
id
\"
:
\"
not implemented
\"
,
\"
key
\"
:
\"
not implemented
\"
}
"
,
200
open_carts
[
cart_id
]
=
{
"
key
"
:
cart_key
,
"
items
"
:
[]
}
logger
.
debug
(
"
Cart created with ID
"
+
cart_id
+
"
and key
"
+
cart_key
)
return
"
{
\"
status
\"
:200,
\"
id
\"
:
\"
"
+
cart_id
+
"
\"
,
\"
key
\"
:
\"
"
+
cart_key
+
"
\"
}
"
,
200
@app.route
(
"
/cart/items
"
,
methods
=
[
"
GET
"
])
def
get_cart_items
():
# Get cart id and key from request
cart_id
=
request
.
args
.
get
(
"
id
"
)
cart_key
=
request
.
args
.
get
(
"
key
"
)
# Get cart and verify not none
cart
=
get_cart
(
cart_id
,
cart_key
)
if
cart
==
None
:
return
"
{
\"
status
\"
:403,
\"
message
\"
:
\"
invalid cart credentials
\"
}
"
,
403
return
"
{
\"
status
\"
:200,
\"
items
\"
:
"
+
json
.
dumps
(
cart
[
"
items
"
])
+
"
}
"
,
200
@app.route
(
"
/cart/add
"
,
methods
=
[
"
POST
"
])
def
add_cart_item
():
# Get cart id and key from request
cart_id
=
request
.
args
.
get
(
"
id
"
)
cart_key
=
request
.
args
.
get
(
"
key
"
)
# Get cart and verify not none
cart
=
get_cart
(
cart_id
,
cart_key
)
if
cart
==
None
:
return
"
{
\"
status
\"
:403,
\"
message
\"
:
\"
invalid cart credentials
\"
}
"
,
403
# Get pizza type from request
pizza_type
=
request
.
args
.
get
(
"
type
"
)
if
pizza_type
==
None
:
return
"
{
\"
status
\"
:400,
\"
message
\"
:
\"
missing pizza type
\"
}
"
,
400
# Get pizza type from pizza types
pizza_type_obj
=
None
for
type
in
pizza_types
:
if
type
[
"
id
"
]
==
pizza_type
:
pizza_type_obj
=
type
break
if
pizza_type_obj
==
None
:
return
"
{
\"
status
\"
:400,
\"
message
\"
:
\"
invalid pizza type
\"
}
"
,
400
# Get number of people from request
num_people
=
request
.
args
.
get
(
"
num_people
"
)
if
num_people
==
None
:
return
"
{
\"
status
\"
:400,
\"
message
\"
:
\"
missing number of people
\"
}
"
,
400
# Test for invalid number of people and convert to integer
try
:
num_people
=
int
(
num_people
)
except
:
return
"
{
\"
status
\"
:400,
\"
message
\"
:
\"
invalid number of people
\"
}
"
,
400
# Get stuffed crust from request
stuffed_crust
=
request
.
args
.
get
(
"
stuffed_crust
"
)
if
stuffed_crust
==
None
:
return
"
{
\"
status
\"
:400,
\"
message
\"
:
\"
missing stuffed crust
\"
}
"
,
400
# Test for invalid stuffed crust and convert to boolean
if
stuffed_crust
==
"
true
"
:
stuffed_crust
=
True
elif
stuffed_crust
==
"
false
"
:
stuffed_crust
=
False
else
:
return
"
{
\"
status
\"
:400,
\"
message
\"
:
\"
invalid stuffed crust
\"
}
"
,
400
cart
[
"
items
"
].
append
({
"
type
"
:
pizza_type
,
"
num_people
"
:
num_people
,
"
stuffed
"
:
stuffed_crust
})
return
"
{
\"
status
\"
:200,
\"
message
\"
:
\"
ok
\"
,
\"
items
\"
:
"
+
cart
[
"
items
"
]
+
"
}
"
,
200
if
__name__
==
"
__main__
"
:
if
__name__
==
"
__main__
"
:
app
.
run
()
app
.
run
()
\ 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
sign in
to comment