From 76e4f763b6644d8f5d009ab180e719d6f1da0676 Mon Sep 17 00:00:00 2001 From: portakal Date: Mon, 25 May 2026 22:29:21 +0300 Subject: [PATCH] adnan is born --- backend/databaseDirect.go | 67 ++++++++++++++- backend/db/config/queries.sql | 23 +++++- backend/db/queries.sql.go | 149 ++++++++++++++++++++++++++++++++-- 3 files changed, 228 insertions(+), 11 deletions(-) diff --git a/backend/databaseDirect.go b/backend/databaseDirect.go index a7d79e9..1efd6c0 100644 --- a/backend/databaseDirect.go +++ b/backend/databaseDirect.go @@ -38,10 +38,66 @@ func update_user(queries *db.Queries, ctx context.Context, username string, pass fmt.Printf("Error at operation : %v\n", err) os.Exit(1) } + fmt.Printf("updated user : %s with %s \n", username, string(hashed_pass)) fmt.Println("successfully updated user") } +func add_airport(queries *db.Queries, ctx context.Context, airportName string, userName string, countryCode uint16) { + res, err := queries.CreateAirportByUserName(ctx, db.CreateAirportByUserNameParams{ + Name: airportName, + Username: userName, + OriginCountryID: countryCode, + }) + if err != nil { + fmt.Printf("Error at operation : %v\n", err) + os.Exit(1) + } + + fmt.Println(res.LastInsertId()) + fmt.Printf("added airport : %s ", airportName) +} + +func add_products(queries *db.Queries, ctx context.Context, name string) { + + res, err := queries.Addproduct(ctx, name) + if err != nil { + fmt.Printf("Error at operation : %v\n", err) + os.Exit(1) + } + + fmt.Println(res.LastInsertId()) + fmt.Printf("added product : %s ", name) +} + +func add_country(queries *db.Queries, ctx context.Context, countryname string, continent db.CountriesContinent, produce_1, produce_2, produce_3 uint16) { + + res, err := queries.AddCountry(ctx, db.AddCountryParams{ + Name: countryname, + Continent: continent, + Produce1ID: produce_1, + Produce2ID: produce_2, + Produce3ID: produce_3, + }) + if err != nil { + fmt.Printf("Error at operation : %v\n", err) + os.Exit(1) + } + + fmt.Println(res.LastInsertId()) + fmt.Printf("added country : %s ", countryname) +} +func get_country_byCountryName(queries *db.Queries, ctx context.Context, countryname string) db.GetCountryByNameRow { + res, err := queries.GetCountryByName(ctx, countryname) + if err != nil { + fmt.Printf("Error at operation : %v\n", err) + os.Exit(1) + } + + fmt.Println(res) + fmt.Printf("added country : %s ", countryname) + return res +} func main() { dsn := "skyramaUser:Skyrama1234.@tcp(127.0.0.1:3306)/skyrama_clone?parseTime=true" @@ -57,6 +113,15 @@ func main() { } ctx := context.Background() queries := db.New(dbConn) - create_user(queries, ctx, "tester_adnan", "a") + + // user opps + //create_user(queries, ctx, "tester_adnan", "a") //update_user(queries, ctx, "tester_adnan", "a") + + //product + //add_products(queries, ctx, "nothing") + //airport opps + //add_country(queries, ctx, "void", db.CountriesContinentVoid, 0, 0, 0) + add_airport(queries, ctx, "adnan's AirPort", "tester_adnan", get_country_byCountryName(queries, ctx, "void").ID) + } diff --git a/backend/db/config/queries.sql b/backend/db/config/queries.sql index 29e807a..383a811 100644 --- a/backend/db/config/queries.sql +++ b/backend/db/config/queries.sql @@ -126,14 +126,35 @@ WHERE id = ?; -- ============================================================================ --- 3. WAREHOUSE & INVENTORY SYSTEM +-- 3. country -- ============================================================================ +-- name: GetAllCountries :many +SELECT id, name FROM countries; +-- name: GetCountryById :one +SELECT id, name FROM countries WHERE id = ?; +-- name: GetCountryByName :one +SELECT id, name FROM countries WHERE name = ?; + +-- name: AddCountry :execresult +INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id) +VALUES (?, ?, ?, ?, ?); + -- ============================================================================ -- 4. BUILDINGS & PRODUCTION ENGINE -- ============================================================================ +-- name: Addproduct :execresult +insert into products (name) values (?); + +-- name: GetAllProducts :many +select id, name from products; + +-- name: GetProductById :one +select id, name from products where id = ?; + + -- name: addStorageBuilding :execresult INSERT INTO buildings (name,min_level,construction_cost_cash,capacity,storage_type,storage_subtype) VALUES (?, ?, ?, ?, ?, ?); diff --git a/backend/db/queries.sql.go b/backend/db/queries.sql.go index d00bb9d..9b49b5f 100644 --- a/backend/db/queries.sql.go +++ b/backend/db/queries.sql.go @@ -25,6 +25,41 @@ func (q *Queries) AddBuildingToAirportByUsername(ctx context.Context, arg AddBui return q.db.ExecContext(ctx, addBuildingToAirportByUsername, arg.Username, arg.BuildingID) } +const addCountry = `-- name: AddCountry :execresult +INSERT INTO countries (name,continent,produce_1_id,produce_2_id,produce_3_id) +VALUES (?, ?, ?, ?, ?) +` + +type AddCountryParams struct { + Name string + Continent CountriesContinent + Produce1ID uint16 + Produce2ID uint16 + Produce3ID uint16 +} + +func (q *Queries) AddCountry(ctx context.Context, arg AddCountryParams) (sql.Result, error) { + return q.db.ExecContext(ctx, addCountry, + arg.Name, + arg.Continent, + arg.Produce1ID, + arg.Produce2ID, + arg.Produce3ID, + ) +} + +const addproduct = `-- name: Addproduct :execresult + +insert into products (name) values (?) +` + +// ============================================================================ +// 4. BUILDINGS & PRODUCTION ENGINE +// ============================================================================ +func (q *Queries) Addproduct(ctx context.Context, name string) (sql.Result, error) { + return q.db.ExecContext(ctx, addproduct, name) +} + const buyPlaneForPlayer = `-- name: BuyPlaneForPlayer :execresult INSERT INTO player_planes (airport_id, plane_id, assigned_hangar_id, current_building_id, status) @@ -447,6 +482,41 @@ func (q *Queries) GetAirportByUserName(ctx context.Context, username string) (Ge return i, err } +const getAllCountries = `-- name: GetAllCountries :many +SELECT id, name FROM countries +` + +type GetAllCountriesRow struct { + ID uint16 + Name string +} + +// ============================================================================ +// 3. country +// ============================================================================ +func (q *Queries) GetAllCountries(ctx context.Context) ([]GetAllCountriesRow, error) { + rows, err := q.db.QueryContext(ctx, getAllCountries) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetAllCountriesRow + for rows.Next() { + var i GetAllCountriesRow + if err := rows.Scan(&i.ID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getAllPlanes = `-- name: GetAllPlanes :many SELECT id, name, min_level, operation_cost_fuel, operation_cost_cash, max_passengers, travel_time_inseconds, operation_type, aircraft_size, aircraft_type, buying_cost_cash, completed_flight_experience, completed_flight_cash_reward, completed_flight_cargo_reward FROM planes ` @@ -489,6 +559,65 @@ func (q *Queries) GetAllPlanes(ctx context.Context) ([]Plane, error) { return items, nil } +const getAllProducts = `-- name: GetAllProducts :many +select id, name from products +` + +func (q *Queries) GetAllProducts(ctx context.Context) ([]Product, error) { + rows, err := q.db.QueryContext(ctx, getAllProducts) + if err != nil { + return nil, err + } + defer rows.Close() + var items []Product + for rows.Next() { + var i Product + if err := rows.Scan(&i.ID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const getCountryById = `-- name: GetCountryById :one +SELECT id, name FROM countries WHERE id = ? +` + +type GetCountryByIdRow struct { + ID uint16 + Name string +} + +func (q *Queries) GetCountryById(ctx context.Context, id uint16) (GetCountryByIdRow, error) { + row := q.db.QueryRowContext(ctx, getCountryById, id) + var i GetCountryByIdRow + err := row.Scan(&i.ID, &i.Name) + return i, err +} + +const getCountryByName = `-- name: GetCountryByName :one +SELECT id, name FROM countries WHERE name = ? +` + +type GetCountryByNameRow struct { + ID uint16 + Name string +} + +func (q *Queries) GetCountryByName(ctx context.Context, name string) (GetCountryByNameRow, error) { + row := q.db.QueryRowContext(ctx, getCountryByName, name) + var i GetCountryByNameRow + err := row.Scan(&i.ID, &i.Name) + return i, err +} + const getPlaneById = `-- name: GetPlaneById :one SELECT id, name, min_level, operation_cost_fuel, operation_cost_cash, max_passengers, travel_time_inseconds, operation_type, aircraft_size, aircraft_type, buying_cost_cash, completed_flight_experience, completed_flight_cash_reward, completed_flight_cargo_reward FROM planes WHERE id = ? ` @@ -917,6 +1046,17 @@ func (q *Queries) GetPlayerPlanesByUsername(ctx context.Context, username string return items, nil } +const getProductById = `-- name: GetProductById :one +select id, name from products where id = ? +` + +func (q *Queries) GetProductById(ctx context.Context, id uint16) (Product, error) { + row := q.db.QueryRowContext(ctx, getProductById, id) + var i Product + err := row.Scan(&i.ID, &i.Name) + return i, err +} + const getUserByUsername = `-- name: GetUserByUsername :one SELECT id, username, password_hash FROM users @@ -1370,9 +1510,6 @@ func (q *Queries) addPlaneToAirportByUsername(ctx context.Context, arg addPlaneT } const addStorageBuilding = `-- name: addStorageBuilding :execresult - - - INSERT INTO buildings (name,min_level,construction_cost_cash,capacity,storage_type,storage_subtype) VALUES (?, ?, ?, ?, ?, ?) ` @@ -1386,12 +1523,6 @@ type addStorageBuildingParams struct { StorageSubtype NullBuildingsStorageSubtype } -// ============================================================================ -// 3. WAREHOUSE & INVENTORY SYSTEM -// ============================================================================ -// ============================================================================ -// 4. BUILDINGS & PRODUCTION ENGINE -// ============================================================================ func (q *Queries) addStorageBuilding(ctx context.Context, arg addStorageBuildingParams) (sql.Result, error) { return q.db.ExecContext(ctx, addStorageBuilding, arg.Name,