- Реализована JWT аутентификация с organization-scope - Добавлено хеширование паролей через bcrypt - Созданы репозитории для организаций и пользователей - Реализован AuthService с бизнес-логикой - Добавлен AuthMiddleware для проверки токенов - Созданы handlers для регистрации и входа - Обновлён API сервер для использования аутентификации Готово для этапа 3 - API структура
89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"erp-mvp/core-service/internal/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type OrganizationRepository interface {
|
|
Create(ctx context.Context, org *models.Organization) error
|
|
GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error)
|
|
Update(ctx context.Context, org *models.Organization) error
|
|
}
|
|
|
|
type organizationRepository struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewOrganizationRepository(db *sql.DB) OrganizationRepository {
|
|
return &organizationRepository{db: db}
|
|
}
|
|
|
|
func (r *organizationRepository) Create(ctx context.Context, org *models.Organization) error {
|
|
query := `
|
|
INSERT INTO organizations (id, name, type, settings, created_at)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
`
|
|
|
|
_, err := r.db.ExecContext(ctx, query, org.ID, org.Name, org.Type, org.Settings, org.CreatedAt)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create organization: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *organizationRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error) {
|
|
query := `
|
|
SELECT id, name, type, settings, created_at
|
|
FROM organizations
|
|
WHERE id = $1
|
|
`
|
|
|
|
org := &models.Organization{}
|
|
err := r.db.QueryRowContext(ctx, query, id).Scan(
|
|
&org.ID,
|
|
&org.Name,
|
|
&org.Type,
|
|
&org.Settings,
|
|
&org.CreatedAt,
|
|
)
|
|
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
return nil, fmt.Errorf("organization not found")
|
|
}
|
|
return nil, fmt.Errorf("failed to get organization: %w", err)
|
|
}
|
|
|
|
return org, nil
|
|
}
|
|
|
|
func (r *organizationRepository) Update(ctx context.Context, org *models.Organization) error {
|
|
query := `
|
|
UPDATE organizations
|
|
SET name = $2, type = $3, settings = $4
|
|
WHERE id = $1
|
|
`
|
|
|
|
result, err := r.db.ExecContext(ctx, query, org.ID, org.Name, org.Type, org.Settings)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update organization: %w", err)
|
|
}
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get rows affected: %w", err)
|
|
}
|
|
|
|
if rowsAffected == 0 {
|
|
return fmt.Errorf("organization not found")
|
|
}
|
|
|
|
return nil
|
|
}
|