144 lines
4.7 KiB
V
144 lines
4.7 KiB
V
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, departure string) veb.Result {
|
|
fields := {
|
|
'to': to
|
|
'from': from
|
|
'name': name
|
|
'price': price.str()
|
|
'departure': departure
|
|
}
|
|
|
|
// Check for empty fields
|
|
mut empty_fields := []string{}
|
|
for field_name, value in fields {
|
|
if value == '' {
|
|
empty_fields << field_name
|
|
}
|
|
}
|
|
|
|
// If any fields are empty, return field-specific error messages
|
|
if empty_fields.len > 0 {
|
|
mut response := '<script>'
|
|
|
|
// Reset all field errors first
|
|
response += 'document.querySelectorAll(".text-danger").forEach(el => el.textContent = "");'
|
|
response += 'document.querySelectorAll("input, select").forEach(el => el.classList.remove("input-error"));'
|
|
|
|
// Set error for each empty field
|
|
for field in empty_fields {
|
|
response += 'document.querySelector(".' + field + '-error").textContent = "(Required)";'
|
|
response += 'document.querySelector("[name=' + field +
|
|
']").classList.add("input-error");'
|
|
}
|
|
|
|
response += '</script>'
|
|
response += '<div class="alert alert-danger">Please fill in all required fields</div>'
|
|
|
|
return ctx.html(response)
|
|
}
|
|
|
|
user_token := ctx.get_cookie('token') or { '' }
|
|
token := app.auth.find_token(user_token) or {
|
|
return ctx.html('<div class="alert alert-danger">User not logged in. Please <a href="/login">login</a> to continue.</div>')
|
|
}
|
|
|
|
if token.user_id == 0 {
|
|
return ctx.html('<div class="alert alert-danger">User not logged in. Please <a href="/login">login</a> to continue.</div>')
|
|
}
|
|
|
|
// Parse with specific format
|
|
departure_time := time.parse_format(departure, 'YYYY-MM-DD') or {
|
|
println('Parse error: ${err}')
|
|
return ctx.html('<div class="alert alert-danger">Invalid departure date format. Please use YYYY-MM-DD format.</div>')
|
|
}
|
|
|
|
flight := app.service_add_flight(from, to, departure_time, price) or {
|
|
return ctx.html('<div class="alert alert-danger">Error creating flight: ${err}</div>')
|
|
}
|
|
|
|
app.service_add_booking(name, token.user_id, flight.id) or {
|
|
return ctx.html('<div class="alert alert-danger">Error creating booking: ${err}</div>')
|
|
}
|
|
|
|
// Return success message with HTML
|
|
success_html := '<div class="alert alert-success">
|
|
<h4>Booking created successfully!</h4>
|
|
<p>Your flight from ${from} to ${to} has been booked.</p>
|
|
<p>Passenger: ${name}</p>
|
|
<a href="/" class="btn btn-primary mt-3">Return to Home</a>
|
|
</div>'
|
|
|
|
return ctx.html(success_html)
|
|
}
|
|
|
|
@['/controller/booking/user']
|
|
pub fn (app &App) controller_get_user_bookings(mut ctx Context) veb.Result {
|
|
user_token := ctx.get_cookie('token') or { '' }
|
|
token := app.auth.find_token(user_token) or {
|
|
return ctx.html('<div class="alert alert-danger">User not logged in. Please <a href="/login">login</a> to continue.</div>')
|
|
}
|
|
|
|
if token.user_id == 0 {
|
|
return ctx.html('<div class="alert alert-danger">User not logged in. Please <a href="/login">login</a> to continue.</div>')
|
|
}
|
|
|
|
bookings := app.service_get_user_bookings(token.user_id) or {
|
|
return ctx.html('<div class="alert alert-danger">Error fetching bookings: ${err}</div>')
|
|
}
|
|
|
|
mut html := ''
|
|
for booking in bookings {
|
|
flight := app.service_get_flight(booking.flight_id) or { continue }
|
|
|
|
html += '<div class="alert alert-info">
|
|
<h4>Booking ID: ${booking.id}</h4>
|
|
<p>Flight from ${flight.from} to ${flight.to}</p>
|
|
<p>Departure: ${flight.departure.format()}</p>
|
|
<p>Passenger: ${booking.name}</p>
|
|
<p>Status: ${booking.status}</p>
|
|
<a href="/controller/booking/delete/${booking.id}" class="btn btn-danger">Delete</a>
|
|
</div>'
|
|
}
|
|
|
|
if html == '' {
|
|
html = '<div class="alert alert-info">No bookings found</div>'
|
|
}
|
|
|
|
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('<div class="alert alert-danger">User not logged in. Please <a href="/login">login</a> to continue.</div>')
|
|
}
|
|
|
|
if token.user_id == 0 {
|
|
return ctx.html('<div class="alert alert-danger">User not logged in. Please <a href="/login">login</a> to continue.</div>')
|
|
}
|
|
|
|
if id == 0 {
|
|
return ctx.html('<div class="alert alert-danger">Invalid booking ID</div>')
|
|
}
|
|
|
|
booking := app.service_get_booking(id) or {
|
|
return ctx.html('<div class="alert alert-danger">Booking not found</div>')
|
|
}
|
|
|
|
if booking.user_id != token.user_id {
|
|
return ctx.html('<div class="alert alert-danger">Unauthorized</div>')
|
|
}
|
|
|
|
app.service_cancel_booking(id) or {
|
|
return ctx.html('<div class="alert alert-danger">Error canceling booking: ${err}</div>')
|
|
}
|
|
|
|
return ctx.html('<div class="alert alert-success">Booking canceled successfully!</div>')
|
|
}
|