Restructured project
- backend moved to backend directory - added and initialized frontend with vue - moved infrastructure files to infra directory
This commit is contained in:
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user