Files
Mini-ERP-app/core-service/internal/repository/organizations.go
Andrey Epifantsev ae84ce74a7 feat: завершён этап 2 - Аутентификация Core Service
- Реализована JWT аутентификация с organization-scope
- Добавлено хеширование паролей через bcrypt
- Созданы репозитории для организаций и пользователей
- Реализован AuthService с бизнес-логикой
- Добавлен AuthMiddleware для проверки токенов
- Созданы handlers для регистрации и входа
- Обновлён API сервер для использования аутентификации

Готово для этапа 3 - API структура
2025-08-27 14:56:33 +04:00

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
}