Files
air-bookings/src/booking_service.v
2025-03-15 20:16:58 +05:30

54 lines
1.3 KiB
V

module main
import time
// service_add_booking is a function that adds a new booking to the database.
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: 'conformed'
created_at: time.now()
}
sql app.db {
insert booking into Booking
}!
}
// service_get_user_bookings is a function that retrieves all bookings for a given user.
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
}
// service_cancel_booking is a function that cancels a booking.
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
}!
}
// service_get_booking is a function that retrieves a booking by its 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]
}