This commit is contained in:
2025-03-13 22:16:24 +05:30
parent d36fe45609
commit d7fa2d3cf8
7 changed files with 100 additions and 11 deletions

47
src/booking_controller.v Normal file
View File

@@ -0,0 +1,47 @@
module main
import veb
import time
@['/controller/booking/create'; post]
pub fn (mut app App) controller_create_booking(mut ctx Context, to string, from string, name string, price int, status string, departure string) veb.Result {
fields := {
'to': to
'from': from
'name': name
'price': price.str()
'status': status
'departure': departure
}
// Check for empty fields
mut empty_fields := []string{}
for field_name, value in fields {
if value == '' {
empty_fields << field_name
}
}
user_token := ctx.get_cookie('token') or { '' }
token := app.auth.find_token(user_token) or { return ctx.text('User not logged in') }
if token.user_id == 0 {
return ctx.text('User not logged in')
}
// Parse with specific format
departure_time := time.parse_format(departure, 'YYYY-MM-DD') or {
println('Parse error: ${err}')
return ctx.text('Invalid departure time format.')
}
flight := app.service_add_flight(from, to, departure_time, price) or {
return ctx.text('Flight not added')
}
app.service_add_booking(name, token.user_id, flight.id, status) or {
return ctx.text('Booking not added')
}
return ctx.text('Booking created')
}