Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
group9_cw
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arya, Navin Chandra C (PG/T - Comp Sci & Elec Eng)
group9_cw
Commits
e823b334
Commit
e823b334
authored
1 year ago
by
Sahu, Pratyush K (PG/T - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
Fixed booking put
parent
1de44147
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
booking_service/main.py
+30
-44
30 additions, 44 deletions
booking_service/main.py
with
30 additions
and
44 deletions
booking_service/main.py
+
30
−
44
View file @
e823b334
...
@@ -10,7 +10,7 @@ from pathlib import Path
...
@@ -10,7 +10,7 @@ from pathlib import Path
from
typing
import
List
from
typing
import
List
import
mysql.connector
import
mysql.connector
from
dotenv
import
load_dotenv
from
dotenv
import
load_dotenv
from
decimal
import
Decimal
from
decimal
import
Decimal
,
getcontext
import
os
import
os
...
@@ -37,6 +37,7 @@ db_connection = mysql.connector.connect(
...
@@ -37,6 +37,7 @@ db_connection = mysql.connector.connect(
)
)
cursor
=
db_connection
.
cursor
()
cursor
=
db_connection
.
cursor
()
getcontext
().
prec
=
10
class
Booking
(
BaseModel
):
class
Booking
(
BaseModel
):
user_id
:
int
user_id
:
int
...
@@ -165,16 +166,6 @@ async def book_bike(booking: Booking):
...
@@ -165,16 +166,6 @@ async def book_bike(booking: Booking):
@app.put
(
"
/bookings/
"
)
@app.put
(
"
/bookings/
"
)
async
def
update_booking
(
update
:
UpdateBooking
):
async
def
update_booking
(
update
:
UpdateBooking
):
# Establish a database connection
db_connection
=
mysql
.
connector
.
connect
(
host
=
'
database-1.cz0ucmk42cu5.us-east-1.rds.amazonaws.com
'
,
port
=
'
3306
'
,
user
=
'
admin
'
,
password
=
'
Test#321
'
,
database
=
'
cycle_connect
'
)
cursor
=
db_connection
.
cursor
()
# Check if the booking with provided booking_id, user_id, and bike_id exists
# Check if the booking with provided booking_id, user_id, and bike_id exists
cursor
.
execute
(
"""
cursor
.
execute
(
"""
SELECT
SELECT
...
@@ -189,42 +180,37 @@ async def update_booking(update: UpdateBooking):
...
@@ -189,42 +180,37 @@ async def update_booking(update: UpdateBooking):
booking
=
cursor
.
fetchone
()
booking
=
cursor
.
fetchone
()
if
not
booking
:
if
not
booking
:
raise
HTTPException
(
status_code
=
404
,
detail
=
"
No matching booking
found
.
"
)
raise
HTTPException
(
status_code
=
404
,
detail
=
"
Booking not
found
"
)
# Unpack fetched details
# Unpack fetched details
start_time
,
blocked_amount
,
price_per_hour
,
current_wallet_balance
=
booking
start_time
,
blocked_amount
,
price_per_hour
,
wallet_balance
=
booking
start_time
=
datetime
.
fromisoformat
(
start_time
)
if
isinstance
(
start_time
,
str
)
else
start_time
# Handle start_time type appropriately
# Calculate the duration in hours and the final price
if
not
isinstance
(
start_time
,
datetime
):
start_time
=
datetime
.
fromisoformat
(
start_time
)
# Calculate total runtime in hours
end_time
=
datetime
.
now
()
end_time
=
datetime
.
now
()
total_runtime
=
(
end_time
-
start_time
).
total_seconds
()
/
3600
duration_hours
=
(
end_time
-
start_time
).
total_seconds
()
/
3600
duration_hours_decimal
=
Decimal
(
str
(
duration_hours
))
# Convert float to Decimal via string for accuracy
# Determine final price based on total runtime
if
total_runtime
<=
5
:
final_price
=
Decimal
(
blocked_amount
)
-
(
duration_hours_decimal
*
Decimal
(
price_per_hour
))
final_price
=
blocked_amount
-
(
total_runtime
*
price_per_hour
)
new_wallet_balance
=
Decimal
(
wallet_balance
)
+
final_price
else
:
final_price
=
blocked_amount
# Perform database updates within a transaction
try
:
# Update wallet balance
cursor
.
execute
(
"
UPDATE Bookings SET end_time = %s, payment_state =
'
Completed
'
WHERE booking_id = %s
"
,
new_wallet_balance
=
current_wallet_balance
+
final_price
(
end_time
.
strftime
(
'
%Y-%m-%d %H:%M:%S
'
),
update
.
booking_id
))
cursor
.
execute
(
"
UPDATE Users SET wallet_balance = %s WHERE user_id = %s
"
,
# Update Booking and Users records
(
new_wallet_balance
,
update
.
user_id
))
cursor
.
execute
(
"
UPDATE Bookings SET end_time = %s, payment_state =
'
Ride Completed
'
WHERE booking_id = %s
"
,
(
end_time
.
strftime
(
'
%Y-%m-%d %H:%M:%S
'
),
update
.
booking_id
))
db_connection
.
commit
()
cursor
.
execute
(
"
UPDATE Users SET wallet_balance = %s WHERE user_id = %s
"
,
(
new_wallet_balance
,
update
.
user_id
))
except
Exception
as
e
:
db_connection
.
rollback
()
db_connection
.
commit
()
raise
HTTPException
(
status_code
=
500
,
detail
=
f
"
An error occurred:
{
str
(
e
)
}
"
)
db_connection
.
close
()
return
{
return
{
"
message
"
:
"
Booking updated and charges applied successfully
"
,
"
message
"
:
"
Booking updated successfully
"
,
"
booking_id
"
:
update
.
booking_id
,
"
details
"
:
{
"
user_id
"
:
update
.
user_id
,
"
booking_id
"
:
update
.
booking_id
,
"
bike_id
"
:
update
.
bike_id
,
"
final_price
"
:
float
(
final_price
),
# Convert Decimal back to float for JSON serialization
"
end_time
"
:
end_time
.
strftime
(
'
%Y-%m-%d %H:%M:%S
'
),
"
new_wallet_balance
"
:
float
(
new_wallet_balance
),
"
final_price
"
:
final_price
,
"
end_time
"
:
end_time
.
strftime
(
'
%Y-%m-%d %H:%M:%S
'
)
"
new_wallet_balance
"
:
new_wallet_balance
,
}
"
payment_state
"
:
"
Ride Completed
"
}
}
\ 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