Initial commit

This commit is contained in:
2025-10-27 14:22:19 +03:00
commit c6e7f13800
23 changed files with 1164 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
package db
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"go.uber.org/fx"
)
func NewDBConnection(lc fx.Lifecycle) *pgxpool.Pool {
pool, err := pgxpool.New(context.Background(), "postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable");
if err != nil {
panic(err.Error())
}
if err := pool.Ping(context.Background()); err != nil {
pool.Close()
panic(err.Error())
}
lc.Append(
fx.StopHook(func() {
pool.Close()
}),
)
return pool
}

32
backend/internal/db/db.go Normal file
View File

@@ -0,0 +1,32 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package db
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
}
func New(db DBTX) *Queries {
return &Queries{db: db}
}
type Queries struct {
db DBTX
}
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
return &Queries{
db: tx,
}
}

View File

@@ -0,0 +1,15 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
package db
import (
"github.com/jackc/pgx/v5/pgtype"
)
type FoodItem struct {
Guid pgtype.UUID
Title string
Description string
}

View File

@@ -0,0 +1,28 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: query.sql
package db
import (
"context"
)
const createFoodItem = `-- name: CreateFoodItem :one
INSERT INTO food_items(title, description)
VALUES ($1, $2)
RETURNING guid, title, description
`
type CreateFoodItemParams struct {
Title string
Description string
}
func (q *Queries) CreateFoodItem(ctx context.Context, arg CreateFoodItemParams) (FoodItem, error) {
row := q.db.QueryRow(ctx, createFoodItem, arg.Title, arg.Description)
var i FoodItem
err := row.Scan(&i.Guid, &i.Title, &i.Description)
return i, err
}