diff --git a/src/booking_controller.v b/src/booking_controller.v index f40dcd1..4bf416d 100644 --- a/src/booking_controller.v +++ b/src/booking_controller.v @@ -111,3 +111,33 @@ pub fn (app &App) controller_get_user_bookings(mut ctx Context) veb.Result { return ctx.html(html) } + +@['/controller/booking/user/cancel'; post] +pub fn (app &App) controller_cancel_booking(mut ctx Context, id int) veb.Result { + user_token := ctx.get_cookie('token') or { '' } + token := app.auth.find_token(user_token) or { + return ctx.html('
User not logged in. Please login to continue.
') + } + + if token.user_id == 0 { + return ctx.html('
User not logged in. Please login to continue.
') + } + + if id == 0 { + return ctx.html('
Invalid booking ID
') + } + + booking := app.service_get_booking(id) or { + return ctx.html('
Booking not found
') + } + + if booking.user_id != token.user_id { + return ctx.html('
Unauthorized
') + } + + app.service_cancel_booking(id) or { + return ctx.html('
Error canceling booking: ${err}
') + } + + return ctx.html('
Booking canceled successfully!
') +} diff --git a/src/booking_service.v b/src/booking_service.v index f31c1c1..e001cc0 100644 --- a/src/booking_service.v +++ b/src/booking_service.v @@ -23,3 +23,27 @@ fn (app &App) service_get_user_bookings(user_id int) ![]Booking { return bookings } + +fn (app &App) service_cancel_booking(id int) ! { + booking := app.service_get_booking(id) or { return error('Booking not found') } + + if booking.status == 'cancelled' { + return error('Booking already cancelled') + } + + sql app.db { + update Booking set status = 'cancelled' where id == id + }! +} + +fn (app &App) service_get_booking(id int) !Booking { + booking := sql app.db { + select from Booking where id == id + }! + + if booking.len == 0 { + return error('Booking not found') + } + + return booking[0] +} diff --git a/src/templates/profile.html b/src/templates/profile.html index 7f1d8dc..dc00963 100644 --- a/src/templates/profile.html +++ b/src/templates/profile.html @@ -156,6 +156,8 @@ @if flight.bookings[0].status == 'confirmed' Confirmed + @else if flight.bookings[0].status == 'cancelled' + Cancelled @else Pending @end