diff --git a/src/booking_controller.v b/src/booking_controller.v new file mode 100644 index 0000000..955386c --- /dev/null +++ b/src/booking_controller.v @@ -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') +} diff --git a/src/booking_entities.v b/src/booking_entities.v index 237447c..bcc4cbf 100644 --- a/src/booking_entities.v +++ b/src/booking_entities.v @@ -6,6 +6,7 @@ import time pub struct Booking { mut: id int @[primary; sql: serial] + name string user_id int flight_id int status string @[sql_type: 'TEXT'] diff --git a/src/booking_service.v b/src/booking_service.v new file mode 100644 index 0000000..66b6d13 --- /dev/null +++ b/src/booking_service.v @@ -0,0 +1,17 @@ +module main + +import time + +fn (app &App) service_add_booking(name string, user_id int, flight_id int, status string) ! { + booking := Booking{ + name: name + user_id: user_id + flight_id: flight_id + status: status + created_at: time.now() + } + + sql app.db { + insert booking into Booking + }! +} diff --git a/src/flight_entities.v b/src/flight_entities.v index 82dcb90..a7e95ef 100644 --- a/src/flight_entities.v +++ b/src/flight_entities.v @@ -8,9 +8,8 @@ mut: id int @[primary; sql: serial] from string @[sql_type: 'TEXT'] to string @[sql_type: 'TEXT'] - date string @[sql_type: 'TEXT'] + departure time.Time @[sql_type: 'DATETIME'] // Changed from string to time.Time price int @[sql_type: 'INT'] - status string @[sql_type: 'TEXT'] bookings []Booking @[fkey: 'flight_id'] created_at time.Time } diff --git a/src/flight_service.v b/src/flight_service.v new file mode 100644 index 0000000..9f48df6 --- /dev/null +++ b/src/flight_service.v @@ -0,0 +1,25 @@ +module main + +import time + +fn (app &App) service_add_flight(from string, to string, departure time.Time, price int) !Flight { + flight_model := Flight{ + from: from + to: to + departure: departure + price: price + created_at: time.now() + } + + sql app.db { + insert flight_model into Flight + }! + + flight := sql app.db { + select from Flight where from == flight_model.from && to == flight_model.to + && departure == flight_model.departure && price == flight_model.price + && created_at == flight_model.created_at + }! + + return flight[0] +} diff --git a/src/main.v b/src/main.v index 870339c..997a69f 100644 --- a/src/main.v +++ b/src/main.v @@ -29,8 +29,8 @@ fn main() { sql app.db { create table User - create table Plane - create table Ticket + create table Flight + create table Booking } or { panic('error on create table: ${err}') } app.auth = auth.new(app.db) diff --git a/src/user_entities.v b/src/user_entities.v index 20c4dfc..845df8a 100644 --- a/src/user_entities.v +++ b/src/user_entities.v @@ -3,12 +3,12 @@ module main @[table: 'users'] pub struct User { mut: - id int @[primary; sql: serial] - first_name string @[sql_type: 'TEXT'] - last_name string @[sql_type: 'TEXT'] - email string @[sql_type: 'TEXT'; unique] - password string @[sql_type: 'TEXT'] - gender string @[sql_type: 'TEXT'] - tickets []Ticket @[fkey: 'user_id'] + id int @[primary; sql: serial] + first_name string @[sql_type: 'TEXT'] + last_name string @[sql_type: 'TEXT'] + email string @[sql_type: 'TEXT'; unique] + password string @[sql_type: 'TEXT'] + gender string @[sql_type: 'TEXT'] + bookings []Booking @[fkey: 'user_id'] salt string }