skyrama-clone/backend/databaseDirect.go

62 lines
1.6 KiB
Go
Raw Normal View History

2026-05-25 20:07:14 +03:00
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 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)
create_user(queries, ctx, "tester_adnan", "a")
//update_user(queries, ctx, "tester_adnan", "a")
}