Restructured project

- backend moved to backend directory
- added and initialized frontend with vue
- moved infrastructure files to infra directory
This commit is contained in:
2026-04-01 22:27:26 +03:00
parent 48ef7217eb
commit 9d845c8899
96 changed files with 1591 additions and 118 deletions
+118
View File
@@ -0,0 +1,118 @@
package repositories
import (
"FamilyHub/src/domain"
"context"
"database/sql"
"errors"
)
type FamilyRepository interface {
Create(ctx context.Context, family *domain.Family) error
GetByID(ctx context.Context, id int64) (*domain.Family, error)
Update(ctx context.Context, family *domain.Family) error
Delete(ctx context.Context, id int64) error
}
type FamilySQLRepository struct {
db *sql.DB
}
func NewFamilySQLRepository(db *sql.DB) *FamilySQLRepository {
return &FamilySQLRepository{db: db}
}
func (r *FamilySQLRepository) Create(ctx context.Context, family *domain.Family) error {
query := `
INSERT INTO families
(name, owner_id, telegram_chat_id, telegram_chat_name, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, created_at, updated_at
`
return r.db.QueryRowContext(
ctx,
query,
family.Name,
family.OwnerID,
family.TelegramChatID,
family.TelegramChatName,
family.CreatedAt,
family.UpdatedAt,
).Scan(&family.ID, &family.CreatedAt, &family.UpdatedAt)
}
func (r *FamilySQLRepository) GetByID(ctx context.Context, id int64) (*domain.Family, error) {
query := `
SELECT
id,
name,
owner_id,
telegram_chat_id,
telegram_chat_name,
created_at,
updated_at
FROM families
WHERE id = $1
`
var family domain.Family
err := r.db.QueryRowContext(ctx, query, id).Scan(
&family.ID,
&family.Name,
&family.OwnerID,
&family.TelegramChatID,
&family.TelegramChatName,
&family.CreatedAt,
&family.UpdatedAt,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil // или кастомную ErrNotFound
}
return nil, err
}
return &family, nil
}
func (r *FamilySQLRepository) Update(ctx context.Context, family *domain.Family) error {
query := `
UPDATE families SET
name = $1,
telegram_chat_id = $2,
telegram_chat_name = $3,
updated_at = now()
WHERE id = $4
RETURNING updated_at
`
return r.db.QueryRowContext(
ctx,
query,
family.Name,
family.TelegramChatID,
family.TelegramChatName,
family.UpdatedAt,
family.ID,
).Scan(&family.UpdatedAt)
}
func (r *FamilySQLRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM families WHERE id = $1`
result, err := r.db.ExecContext(ctx, query, id)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return sql.ErrNoRows
}
return nil
}
+63
View File
@@ -0,0 +1,63 @@
package repositories
import (
"FamilyHub/src/domain"
"context"
"database/sql"
"errors"
)
type OTPRepository interface {
Create(ctx context.Context, otp *domain.OTP) error
Get(ctx context.Context, userID int64, code string) (*domain.OTP, error)
}
type OTPSQLRepository struct {
db *sql.DB
}
func NewOTPSQLRepository(db *sql.DB) *OTPSQLRepository {
return &OTPSQLRepository{db: db}
}
func (r *OTPSQLRepository) Create(ctx context.Context, otp *domain.OTP) error {
query := `
INSERT INTO otp (user_id, otp, expired_at)
VALUES ($1, $2, $3)
`
_, err := r.db.ExecContext(ctx, query, otp.UserID, otp.Code, otp.ExpiredAt)
return err
}
func (r *OTPSQLRepository) Get(ctx context.Context, userID int64, code string) (*domain.OTP, error) {
query := `
DELETE FROM otp
WHERE ctid IN (
SELECT ctid
FROM otp
WHERE user_id = $1
AND otp = $2
AND expired_at > NOW()
ORDER BY expired_at
LIMIT 1
)
RETURNING user_id, otp, expired_at
`
var otp domain.OTP
err := r.db.QueryRowContext(ctx, query, userID, code).Scan(
&otp.UserID,
&otp.Code,
&otp.ExpiredAt,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &otp, nil
}
+322
View File
@@ -0,0 +1,322 @@
package repositories
import (
"context"
"database/sql"
"errors"
"FamilyHub/src/domain"
)
type ReceiptsRepository interface {
Create(ctx context.Context, receipt *domain.Receipt) (int64, error)
GetByID(ctx context.Context, id int64) (*domain.Receipt, error)
GetAll(ctx context.Context, limit, offset int) ([]*domain.Receipt, error)
Update(ctx context.Context, receipt *domain.Receipt) error
Delete(ctx context.Context, id int64) error
}
type ReceiptsSQLRepository struct {
db *sql.DB
}
func NewReceiptsSQLRepository(db *sql.DB) *ReceiptsSQLRepository {
return &ReceiptsSQLRepository{db: db}
}
func (r *ReceiptsSQLRepository) Create(ctx context.Context, receipt *domain.Receipt) (int64, error) {
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return 0, err
}
defer tx.Rollback()
if receipt.ReceiptNumber != receipt.UI {
receipt.ReceiptNumber = receipt.UI
}
res, err := tx.ExecContext(ctx, `
INSERT INTO receipts (
receipt_number, ui, status, issued_at,
total_amount, payment_amount, cash_amount,
another_amount, clearing_amount, margin,
currency, payment_type,
cashbox_number, cashier,
name_spd, name_to, name_np, type_np,
street_to, house_to,
kod_soato, oblast_soato, rayon_soato, selsovet_soato,
doc_num, skno_number, unp,
success
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`,
receipt.ReceiptNumber,
receipt.UI,
receipt.Status,
receipt.IssuedAt,
receipt.TotalAmount,
receipt.PaymentAmount,
receipt.CashAmount,
receipt.AnotherAmount,
receipt.ClearingAmount,
receipt.Margin,
receipt.Currency,
receipt.PaymentType,
receipt.CashboxNumber,
receipt.Cashier,
receipt.NameSPD,
receipt.NameTO,
receipt.NameNP,
receipt.TypeNP,
receipt.StreetTo,
receipt.HouseTo,
receipt.KodSoato,
receipt.OblastSoato,
receipt.RayonSoato,
receipt.SelsovetSoato,
receipt.DocNum,
receipt.SknoNumber,
receipt.UNP,
receipt.Success,
)
if err != nil {
return 0, err
}
receiptID, err := res.LastInsertId()
if err != nil {
return 0, err
}
stmt, err := tx.PrepareContext(ctx, `
INSERT INTO positions (
receipt_id,
section_number,
gtin_code,
product_name,
product_count,
amount,
discount,
surcharge,
tag,
marking_code,
ukz_code
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`)
if err != nil {
return 0, err
}
defer stmt.Close()
for _, p := range receipt.Positions {
_, err = stmt.ExecContext(ctx,
receiptID,
p.SectionNumber,
p.GTINCode,
p.ProductName,
p.ProductCount,
p.Amount,
p.Discount,
p.Surcharge,
p.Tag,
p.MarkingCode,
p.UKZCode,
)
if err != nil {
return 0, err
}
}
return receiptID, tx.Commit()
}
func (r *ReceiptsSQLRepository) GetByID(ctx context.Context, id int64) (*domain.Receipt, error) {
var receipt domain.Receipt
err := r.db.QueryRowContext(ctx, `
SELECT
id,
receipt_number, ui, status, issued_at,
total_amount, payment_amount, cash_amount,
another_amount, clearing_amount, margin,
currency, payment_type,
cashbox_number, cashier,
name_spd, name_to, name_np, type_np,
street_to, house_to,
kod_soato, oblast_soato, rayon_soato, selsovet_soato,
doc_num, skno_number, unp,
success
FROM receipts
WHERE id = ?
`, id).Scan(
&receipt.ID,
&receipt.ReceiptNumber,
&receipt.UI,
&receipt.Status,
&receipt.IssuedAt,
&receipt.TotalAmount,
&receipt.PaymentAmount,
&receipt.CashAmount,
&receipt.AnotherAmount,
&receipt.ClearingAmount,
&receipt.Margin,
&receipt.Currency,
&receipt.PaymentType,
&receipt.CashboxNumber,
&receipt.Cashier,
&receipt.NameSPD,
&receipt.NameTO,
&receipt.NameNP,
&receipt.TypeNP,
&receipt.StreetTo,
&receipt.HouseTo,
&receipt.KodSoato,
&receipt.OblastSoato,
&receipt.RayonSoato,
&receipt.SelsovetSoato,
&receipt.DocNum,
&receipt.SknoNumber,
&receipt.UNP,
&receipt.Success,
)
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
if err != nil {
return nil, err
}
rows, err := r.db.QueryContext(ctx, `
SELECT
section_number, gtin_code, product_name,
product_count, amount,
discount, surcharge,
tag, marking_code, ukz_code
FROM positions WHERE receipt_id = ?
`, id)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var p domain.Position
if err := rows.Scan(
&p.SectionNumber,
&p.GTINCode,
&p.ProductName,
&p.ProductCount,
&p.Amount,
&p.Discount,
&p.Surcharge,
&p.Tag,
&p.MarkingCode,
&p.UKZCode,
); err != nil {
return nil, err
}
receipt.Positions = append(receipt.Positions, p)
}
return &receipt, nil
}
func (r *ReceiptsSQLRepository) GetAll(ctx context.Context, limit, offset int) ([]*domain.Receipt, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT id, receipt_number, issued_at, total_amount, currency
FROM receipts
ORDER BY issued_at DESC
LIMIT ? OFFSET ?
`, limit, offset)
if err != nil {
return nil, err
}
defer rows.Close()
var receipts []*domain.Receipt
for rows.Next() {
var rct domain.Receipt
if err := rows.Scan(
&rct.ID,
&rct.ReceiptNumber,
&rct.IssuedAt,
&rct.TotalAmount,
&rct.Currency,
); err != nil {
return nil, err
}
receipts = append(receipts, &rct)
}
return receipts, nil
}
func (r *ReceiptsSQLRepository) Update(ctx context.Context, receipt *domain.Receipt) error {
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
_, err = tx.ExecContext(ctx, `
UPDATE receipts SET
issued_at = ?,
total_amount = ?,
currency = ?
WHERE id = ?
`,
receipt.IssuedAt,
receipt.TotalAmount,
receipt.Currency,
receipt.ID,
)
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, `DELETE FROM positions WHERE receipt_id = ?`, receipt.ID)
if err != nil {
return err
}
for _, p := range receipt.Positions {
_, err = tx.ExecContext(ctx, `
INSERT INTO positions (
receipt_id, product_name, product_count, amount
) VALUES (?, ?, ?, ?)
`, receipt.ID, p.ProductName, p.ProductCount, p.Amount)
if err != nil {
return err
}
}
return tx.Commit()
}
func (r *ReceiptsSQLRepository) Delete(ctx context.Context, id int64) error {
_, err := r.db.ExecContext(ctx,
`DELETE FROM receipts WHERE id = ?`,
id,
)
return err
}
+158
View File
@@ -0,0 +1,158 @@
package repositories
import (
"FamilyHub/src/domain"
"context"
"database/sql"
"errors"
)
type UsersRepository interface {
Create(ctx context.Context, user *domain.UserModel) error
GetByID(ctx context.Context, id int64) (*domain.UserModel, error)
GetByTelegramID(ctx context.Context, telegramID int64) (*domain.UserModel, error)
Update(ctx context.Context, user *domain.UserModel) error
Delete(ctx context.Context, id int64) error
}
type UsersSQLRepository struct {
db *sql.DB
}
func NewUsersSQLRepository(db *sql.DB) *UsersSQLRepository {
return &UsersSQLRepository{db: db}
}
func (r *UsersSQLRepository) Create(ctx context.Context, user *domain.UserModel) error {
query := `
INSERT INTO users
(telegram_id, username, first_name, last_name, language_code)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, created_at, updated_at
`
return r.db.QueryRowContext(
ctx,
query,
user.TelegramID,
user.Username,
user.FirstName,
user.LastName,
user.LanguageCode,
).Scan(&user.ID, &user.CreatedAt, &user.UpdatedAt)
}
func (r *UsersSQLRepository) GetByID(ctx context.Context, id int64) (*domain.UserModel, error) {
query := `
SELECT
id,
telegram_id,
username,
first_name,
last_name,
language_code,
created_at,
updated_at
FROM users
WHERE id = $1
`
var user domain.UserModel
err := r.db.QueryRowContext(ctx, query, id).Scan(
&user.ID,
&user.TelegramID,
&user.Username,
&user.FirstName,
&user.LastName,
&user.LanguageCode,
&user.CreatedAt,
&user.UpdatedAt,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil // или кастомную ErrNotFound
}
return nil, err
}
return &user, nil
}
func (r *UsersSQLRepository) GetByTelegramID(ctx context.Context, telegramID int64) (*domain.UserModel, error) {
query := `
SELECT
id,
telegram_id,
username,
first_name,
last_name,
language_code,
created_at,
updated_at
FROM users
WHERE telegram_id = $1
`
var user domain.UserModel
err := r.db.QueryRowContext(ctx, query, telegramID).Scan(
&user.ID,
&user.TelegramID,
&user.Username,
&user.FirstName,
&user.LastName,
&user.LanguageCode,
&user.CreatedAt,
&user.UpdatedAt,
)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
}
return &user, nil
}
func (r *UsersSQLRepository) Update(ctx context.Context, user *domain.UserModel) error {
query := `
UPDATE users SET
username = $1,
first_name = $2,
last_name = $3,
language_code = $4,
updated_at = now()
WHERE id = $5
RETURNING updated_at
`
return r.db.QueryRowContext(
ctx,
query,
user.Username,
user.FirstName,
user.LastName,
user.LanguageCode,
user.ID,
).Scan(&user.UpdatedAt)
}
func (r *UsersSQLRepository) Delete(ctx context.Context, id int64) error {
query := `DELETE FROM users WHERE id = $1`
result, err := r.db.ExecContext(ctx, query, id)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows == 0 {
return sql.ErrNoRows
}
return nil
}