add user management functionality with OTP verification and consent handling, DI introduced

This commit is contained in:
2025-08-10 10:38:49 +03:00
parent 6a9061a3de
commit 5d80a68b44
30 changed files with 828 additions and 528 deletions

24
internal/db/database.go Normal file
View File

@ -0,0 +1,24 @@
package db
import (
"context"
"strconv"
"git.logidex.ru/fakz9/logidex-id/internal/config"
"github.com/jackc/pgx/v5/pgxpool"
)
func NewDatabasePool(cfg config.Config) *pgxpool.Pool {
ctx := context.Background()
connUrl := "postgresql://" + cfg.DB.User + ":" + cfg.DB.Password + "@" + cfg.DB.Host + ":" + strconv.Itoa(cfg.DB.Port) + "/" + cfg.DB.Dbname
pool, err := pgxpool.New(ctx, connUrl)
if err != nil {
panic("Failed to connect to database: " + err.Error())
}
err = pool.Ping(ctx)
if err != nil {
panic("Failed to ping database: " + err.Error())
}
return pool
}

15
internal/db/fx.go Normal file
View File

@ -0,0 +1,15 @@
package db
import (
sqlcdb "git.logidex.ru/fakz9/logidex-id/internal/db/generated"
"go.uber.org/fx"
)
var Module = fx.Options(
fx.Provide(
fx.Annotate(
NewDatabasePool,
fx.As(new(sqlcdb.DBTX)),
),
),
)

View File

@ -0,0 +1,9 @@
CREATE TABLE IF NOT EXISTS users
(
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
phone_number VARCHAR(20) NOT NULL CHECK (phone_number ~ '^\+[0-9]{10,15}$'),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
verified BOOLEAN DEFAULT FALSE NOT NULL,
verified_at TIMESTAMP NULL
);

View File

@ -0,0 +1,23 @@
-- name: GetByPhoneNumber :one
SELECT *
FROM users
WHERE phone_number = $1
LIMIT 1;
-- name: GetUserByUUID :one
SELECT *
FROM users
WHERE uuid = $1
LIMIT 1;
-- name: CreateUser :one
INSERT INTO users (phone_number)
VALUES ($1)
RETURNING *;
-- name: UpdateUserVerified :one
UPDATE users
SET verified = TRUE,
verified_at = CURRENT_TIMESTAMP
WHERE uuid = $1
RETURNING *;