send all files
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"api/libs/logger"
|
||||
"api/libs/postgres"
|
||||
"api/models/users"
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/alexedwards/argon2id"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
|
||||
ensureTableUsers()
|
||||
ensureTableProducts()
|
||||
|
||||
usersRepository := users.New(postgres.Pool)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
hash, err := argon2id.CreateHash("admin", argon2id.DefaultParams)
|
||||
if err != nil {
|
||||
logger.Production.Error("error generate password hash")
|
||||
}
|
||||
|
||||
_, err = usersRepository.GetUser(ctx, "admin")
|
||||
if err != nil {
|
||||
if err.Error() == pgx.ErrNoRows.Error() {
|
||||
usersRepository.CreateUser(ctx, users.CreateUserParams{
|
||||
User: uuid.New().String(),
|
||||
CreatedAt: uint64(time.Now().Unix()),
|
||||
Username: "admin",
|
||||
Password: hash,
|
||||
})
|
||||
logger.Production.Info("Create user admin into database")
|
||||
} else {
|
||||
logger.Production.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func ensureTableUsers() {
|
||||
if _, err := postgres.Pool.Exec(context.Background(), CreateUsersTableIfNotExists); err != nil {
|
||||
logger.Production.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Production.Info("users exists")
|
||||
}
|
||||
|
||||
func ensureTableProducts() {
|
||||
if _, err := postgres.Pool.Exec(context.Background(), CreateProductsTableIfNotExists); err != nil {
|
||||
logger.Production.Error(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Production.Info("products exists")
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package database
|
||||
|
||||
var CreateUsersTableIfNotExists = `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
"user" UUID NOT NULL,
|
||||
created_at BIGINT NOT NULL,
|
||||
updated_at BIGINT,
|
||||
username text NOT NULL,
|
||||
"password" text NOT NULL,
|
||||
PRIMARY KEY ("user")
|
||||
)`
|
||||
|
||||
var CreateProductsTableIfNotExists = `
|
||||
CREATE TABLE IF NOT EXISTS products (
|
||||
created_by UUID NOT NULL,
|
||||
created_at BIGINT NOT NULL,
|
||||
updated_by UUID,
|
||||
updated_at BIGINT,
|
||||
|
||||
nome text NOT NULL,
|
||||
codigo text NOT NULL,
|
||||
estoque_total BIGINT NOT NULL,
|
||||
estoque_corte BIGINT NOT NULL,
|
||||
estoque_disponivel BIGINT NOT NULL,
|
||||
preco_de NUMERIC NOT NULL,
|
||||
preco_por NUMERIC NOT NULL,
|
||||
|
||||
PRIMARY KEY (codigo)
|
||||
)`
|
||||
@@ -0,0 +1,22 @@
|
||||
package variable
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// use godot package to load/read the .env file and
|
||||
// return the value of the key
|
||||
func GetEnvVariable(key string) string {
|
||||
|
||||
// load .env file
|
||||
err := godotenv.Load(".env")
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Error loading .env file")
|
||||
}
|
||||
|
||||
return os.Getenv(key)
|
||||
}
|
||||
Reference in New Issue
Block a user