diff --git a/src/booking_controller.v b/src/booking_controller.v
index 955386c..f40dcd1 100644
--- a/src/booking_controller.v
+++ b/src/booking_controller.v
@@ -4,13 +4,12 @@ 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 {
+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()
- 'status': status
'departure': departure
}
@@ -22,26 +21,93 @@ pub fn (mut app App) controller_create_booking(mut ctx Context, to string, from
}
}
+ // If any fields are empty, return field-specific error messages
+ if empty_fields.len > 0 {
+ mut response := ''
+ response += '
Please fill in all required fields
'
+
+ return ctx.html(response)
+ }
+
user_token := ctx.get_cookie('token') or { '' }
- token := app.auth.find_token(user_token) or { return ctx.text('User not logged in') }
+ 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.text('User not logged in')
+ return ctx.html('User not logged in. Please
login to continue.
')
}
// 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.')
+ return ctx.html('Invalid departure date format. Please use YYYY-MM-DD format.
')
}
flight := app.service_add_flight(from, to, departure_time, price) or {
- return ctx.text('Flight not added')
+ return ctx.html('Error creating flight: ${err}
')
}
- app.service_add_booking(name, token.user_id, flight.id, status) or {
- return ctx.text('Booking not added')
+ app.service_add_booking(name, token.user_id, flight.id) or {
+ return ctx.html('Error creating booking: ${err}
')
}
- return ctx.text('Booking created')
+ // Return success message with HTML
+ success_html := '
+
Booking created successfully!
+
Your flight from ${from} to ${to} has been booked.
+
Passenger: ${name}
+
Return to Home
+
'
+
+ 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('User not logged in. Please
login to continue.
')
+ }
+
+ if token.user_id == 0 {
+ return ctx.html('User not logged in. Please
login to continue.
')
+ }
+
+ bookings := app.service_get_user_bookings(token.user_id) or {
+ return ctx.html('Error fetching bookings: ${err}
')
+ }
+
+ mut html := ''
+ for booking in bookings {
+ flight := app.service_get_flight(booking.flight_id) or { continue }
+
+ html += '
+
Booking ID: ${booking.id}
+
Flight from ${flight.from} to ${flight.to}
+
Departure: ${flight.departure.format()}
+
Passenger: ${booking.name}
+
Status: ${booking.status}
+
Delete
+
'
+ }
+
+ if html == '' {
+ html = 'No bookings found
'
+ }
+
+ return ctx.html(html)
}
diff --git a/src/booking_service.v b/src/booking_service.v
index 66b6d13..f31c1c1 100644
--- a/src/booking_service.v
+++ b/src/booking_service.v
@@ -2,12 +2,12 @@ module main
import time
-fn (app &App) service_add_booking(name string, user_id int, flight_id int, status string) ! {
+fn (app &App) service_add_booking(name string, user_id int, flight_id int) ! {
booking := Booking{
name: name
user_id: user_id
flight_id: flight_id
- status: status
+ status: 'conformed'
created_at: time.now()
}
@@ -15,3 +15,11 @@ fn (app &App) service_add_booking(name string, user_id int, flight_id int, statu
insert booking into Booking
}!
}
+
+fn (app &App) service_get_user_bookings(user_id int) ![]Booking {
+ bookings := sql app.db {
+ select from Booking where user_id == user_id order by created_at desc
+ }!
+
+ return bookings
+}
diff --git a/src/booking_view.v b/src/booking_view.v
new file mode 100644
index 0000000..d671703
--- /dev/null
+++ b/src/booking_view.v
@@ -0,0 +1,7 @@
+module main
+
+import veb
+
+pub fn (app &App) booking(mut ctx Context) veb.Result {
+ return $veb.html()
+}
diff --git a/src/flight_service.v b/src/flight_service.v
index 9f48df6..2e2a95a 100644
--- a/src/flight_service.v
+++ b/src/flight_service.v
@@ -23,3 +23,11 @@ fn (app &App) service_add_flight(from string, to string, departure time.Time, pr
return flight[0]
}
+
+fn (app &App) service_get_flight(id int) !Flight {
+ flight := sql app.db {
+ select from Flight where id == id
+ }!
+
+ return flight[0]
+}
diff --git a/src/templates/booking.html b/src/templates/booking.html
new file mode 100644
index 0000000..771c4eb
--- /dev/null
+++ b/src/templates/booking.html
@@ -0,0 +1,147 @@
+@include 'header.html'
+
+
+
+ Create New Booking
+ Book your next flight with us
+
+
+
+
+
+
+
+@include 'footer.html'
diff --git a/src/templates/header.html b/src/templates/header.html
index 4a408b3..38607c7 100644
--- a/src/templates/header.html
+++ b/src/templates/header.html
@@ -1,35 +1,46 @@
-
+
-
-
-
- Air Bookings
- @css '/static/css/bootstrap.css'
-
-
-
-
-
-
Air Bookings
-
-
-
-
-
+
+
+
+
Air Bookings
+ @css '/static/css/bootstrap.css'
+
+ @js 'https://unpkg.com/htmx.org@2.0.4'
+
+
+
+
-
-
-
\ No newline at end of file
+
+
+
+
diff --git a/src/templates/profile.html b/src/templates/profile.html
index 2e757ba..7f1d8dc 100644
--- a/src/templates/profile.html
+++ b/src/templates/profile.html
@@ -11,7 +11,10 @@
@{user.first_name} @{user.last_name}
@{user.email}
-
@{user.gender}
+
+
+ @{user.gender}
+
@@ -19,29 +22,71 @@
Update Profile
@@ -53,7 +98,9 @@
@@ -75,11 +122,53 @@
method: "POST",
body: formData,
})
- .then((response) => response.text())
- .then((html) => {
- document.getElementById("response-container").innerHTML = html;
- });
+ .then((response) => response.text())
+ .then((html) => {
+ document.getElementById("response-container").innerHTML = html;
+ });
});
-@include 'footer.html'
\ No newline at end of file
+
+
+
+ @if bookings.len > 0
+
+
+
+
+ From
+ To
+ Departure
+ Price
+ Status
+
+
+
+ @for flight in flights
+
+ @{flight.from}
+ @{flight.to}
+ @{flight.departure}
+ $@{flight.price}
+
+ @if flight.bookings[0].status == 'confirmed'
+ Confirmed
+ @else
+ Pending
+ @end
+
+
+ @end
+
+
+
+ @else
+
No bookings found
+ @end
+
+
+
+@include 'footer.html'
diff --git a/src/user_view.v b/src/user_view.v
index 4cd251f..02239b0 100644
--- a/src/user_view.v
+++ b/src/user_view.v
@@ -33,6 +33,13 @@ pub fn (app &App) profile(mut ctx Context) veb.Result {
}
user := app.service_get_user(token.user_id) or { return ctx.redirect('/login') }
+ bookings := app.service_get_user_bookings(token.user_id) or { []Booking{} }
+
+ mut flights := []Flight{}
+ for booking in bookings {
+ flight := app.service_get_flight(booking.flight_id) or { continue }
+ flights << flight
+ }
return $veb.html()
}
diff --git a/v.mod b/v.mod
index 469a399..c3d52f0 100644
--- a/v.mod
+++ b/v.mod
@@ -1,6 +1,6 @@
Module {
name: 'air_bookings'
- description: 'An Air Plane Booking Web Application'
+ description: 'An Flight Booking Web Application'
version: '0.0.0'
license: 'MIT'
dependencies: []