This commit is contained in:
fitel17112 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')
}

View File

@ -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']

17
src/booking_service.v Normal file
View File

@ -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
}!
}

View File

@ -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
}

25
src/flight_service.v Normal file
View File

@ -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]
}

View File

@ -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)

View File

@ -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
}