skyrama-clone/backend/databaseDirect.go
2026-05-25 22:29:21 +03:00

127 lines
3.5 KiB
Go

package main
import (
"context"
"database/sql"
"fmt"
"os"
"skyrama_clone_backend/backend/db"
_ "github.com/go-sql-driver/mysql" // MySQL Driver
"golang.org/x/crypto/bcrypt"
)
func create_user(queries *db.Queries, ctx context.Context, username string, password string) {
hashedPass, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
resultt, err := queries.CreateUser(ctx, db.CreateUserParams{
Username: username,
PasswordHash: string(hashedPass),
})
if err != nil {
fmt.Printf("Error at operation : %v\n", err)
os.Exit(1)
}
fmt.Println(resultt)
fmt.Printf("added user : %s ", username)
}
func update_user(queries *db.Queries, ctx context.Context, username string, password string) {
hashed_pass, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
err := queries.UpdateUserPasswordByUserName(ctx, db.UpdateUserPasswordByUserNameParams{
PasswordHash: string(hashed_pass),
Username: username,
})
if err != nil {
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"
dbConn, err := sql.Open("mysql", dsn)
if err != nil {
fmt.Printf("Error opening database pool: %v\n", err)
os.Exit(1)
}
defer dbConn.Close()
if err := dbConn.Ping(); err != nil {
fmt.Printf("Database unreachable: %v\n", err)
os.Exit(1)
}
ctx := context.Background()
queries := db.New(dbConn)
// 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)
}